Code Monkey home page Code Monkey logo

Comments (12)

solocommerce avatar solocommerce commented on May 21, 2024 3

I have done the best example I can, it is hard to give examples without knowing the actual webservice and vice versa it is hard to give example if it is specific to a webservice. It gets really confusing as well if you want to call your cake files articles and your webservice files articles as well, I would advise calling them different things. Which is why I've created a fake webserivce called Newspaper, whereby you post your articles to the newspaper webservice.

/plugins/Example/Newspaper/src/Model/Endpoint/NewspaperArticleEndpoint.php

<?php

// Think of the endpoint like a entity

namespace Example\Newspaper\Webservice;

use Muffin\Webservice\Model\Endpoint;
use Muffin\Webservice\Schema;

class NewspaperArticlesEndpoint extends Endpoint
{
    /**
     * Initialise Method
     *
     * @param array $config Array of config
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        // The primary key in which data is handled
        $this->primaryKey('id');
        $this->displayField('name');

        // Just like an entity this is the data the remote webservice expects.
        $schema = new Schema(null, [
            'id' => [
                'type' => 'integer',
            ],
            'name' => [
                'type' => 'string',
            ],
            'contents' => [
                'type' => 'string',
            ],
        ]);
        $this->schema($schema);
    }
}

/plugins/Example/Newspaper/src/Model/Resource/NewspaperArticle.php

<?php

// I have not implemented any resources, I believe it is the response
// the endpoint may provide back?

namespace Example\Newspaper\Model\Resource;

use Muffin\Webservice\Model\Resource;

/**
 * Class NewspaperArticle
 *
 * @package Example\Newspaper\Model\Resource
 */
class NewspaperArticle extends Resource
{

}

/plugins/Example/Newspaper/src/Webservice/NewspaperWebserivce.php

<?php

namespace Example\Newspaper\Webservice;

use Muffin\Webservice\Query;
use Muffin\Webservice\Webservice\Webservice;
use Psr\Http\Message\ResponseInterface;

/**
 * Class NewspaperWebservice
 *
 * @package Example\Newspaper\Webservice
 */
class NewspaperWebservice extends Webservice
{
    /**
     * Execute Create Query Method
     */
    protected function _executeCreateQuery(Query $query, array $options = [])
    {
        // This is the main webservice endpoint, in my example I'm assuming that 
        // there is something already operating here and it is different structure
        // to the articles webservice. In other words check NewspaperArticlesWebservice.php
    }

    /**
     * Execute Read Query Method
     */
    protected function _executeReadQuery(Query $query, array $options = [])
    {
        // As above check NewspaperArticlesWebservice.php
    }

    /**
     * Check Response Method
     */
    protected function _checkResponse(ResponseInterface $response)
    {
        // Globally handle 404s etc
    }

    /**
     * Execute Update Query Method
     */
    protected function _executeUpdateQuery(Query $query, array $options = [])
    {
        // As above check NewspaperArticlesWebservice.php
    }

    /**
     * Execute Delete Query Method
     */
    protected function _executeDeleteQuery(Query $query, array $options = [])
    {
        // As above check NewspaperArticlesWebservice.php
    }
}

/plugins/Example/Newspaper/src/Webservice/NewspaperArticlesWebservice.php

<?php

namespace Example\Newspaper\Webservice;

use Cake\Network\Http\Response;
use Muffin\Webservice\Model\Endpoint;
use Muffin\Webservice\Query;

/**
 * Class NewspaperArticlesWebservice
 *
 * @package Example\Newspaper\Webservice
 */
class NewspaperArticlesWebservice extends NewspaperWebservice
{
    /**
     * Initialise Method
     *
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Execute Create Query Method
     */
    protected function _executeCreateQuery(Query $query, array $options = [])
    {
        // Query->set will initiate the schema created set out in the endpoint,
        // if missing wont map/create (just like entity)
        $parameters['article'] = $query->set();

        /* @var Response $response */
        $response = $this->driver()->client()->post($this->_baseUrl(), json_encode($parameters), ['type' => 'json']);

        $this->_checkResponse($response);

        return $this->_transformResource($query->endpoint(), $response->json);
    }

    /**
     * Base URL
     */
    protected function _baseUrl()
    {
        return '/articles';
    }

    /**
     * Transform Resource Method
     */
    protected function _transformResource(Endpoint $endpoint, array $result)
    {
        // Deal with the article data returned.
    }

    /**
     * Read Query Method
     */
    protected function _executeReadQuery(Query $query, array $options = [])
    {
        // ...
    }

    /**
     * Update Query Method
     */
    protected function _executeUpdateQuery(Query $query, array $options = [])
    {
        // Query->set will initiate the schema created set out in the endpoint,
        // if missing wont map/create (just like entity)
        $parameters['article'] = $query->set();
        $parameters['article'][$query->endpoint()->primaryKey()] = $query->where()['id'];

        /* @var Response $response */
        $response = $this->driver()->client()->post($this->_baseUrl(), json_encode($parameters), ['type' => 'json']);

        $this->_checkResponse($response);

        return $this->_transformResource($query->endpoint(), $response->json);
    }

    /**
     * Delete Query Method
     */
    protected function _executeDeleteQuery(Query $query, array $options = [])
    {
        // I have not tried a delete
    }
}

/src/Controller/NewspaperArticlesController.php

<?php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;

/**
 * Newspaper Articles Controller
 *
 * @property \Exammple\Newspaper\Model\Endpoint\NewspaperArticlesEndpoint $NewspaperArticles
 */
class NewspaperArticlesController extends AppController
{
    /**
     * @throws \Exception
     * @return void
     */
    public function initialize()
    {
        parent::initialize();

        $this->modelFactory('Endpoint', ['Muffin\Webservice\Model\EndpointRegistry', 'get']);
    }

    /**
     * Before Filter Method
     *
     * @param \Cake\Event\Event $event Event Object
     *
     * @return \Cake\Http\Response|null|void
     */
    public function beforeFilter(Event $event)
    {
        $this->loadModel('Example/Newspaper.NewspaperArticles', 'Endpoint');
    }

    /**
     * Add the article from Cake to Newspaper Article Web Service
     *
     * @param string|null $articleId The local article id
     *
     * @return \Cake\Http\Response|null
     */
    public function add($articleId = null)
    {
        // In my example, Im populating data from another table and saving (adding/pushing) 
        // that into a remote system using the webservice plugin. You may not wish to do 
        // that, in which case you could just post the data from your form to the remote 
        // webservice and skip loading data.

        // Pull in the data from the articles table (your local data in cake).
        $article = TableRegistry::getTableLocator()->get('Articles');
        $myArticle = $article->get($articleId);

        if (!$myArticle->id) {
            $this->Flash->error(__('This article doesn\'t exist'));

            return $this->redirect($this->referer());
        }

        // Initiate the new remote article (creates from endpoint schema)
        $newspaperArticle = $this->NewspaperArticles->newEntity();

        // The minimum to create an article on remote system.
        $articleData = [
            'name' => $myArticle->name,
            'contents' => $myArticle->contents,
        ];

        // Patch the endpoint with the data to be saved to remote system.
        $newspaperArticle = $this->NewspaperArticles->patchEntity($newspaperArticle, $articleData);

        $newspaperResponse = $this->NewspaperArticles->save($newspaperArticle);

        if ($newspaperResponse->id) {
            $this->Flash->success(__('The article has been created in the remote system.'));

            // here you may choose to save any of the data the remote system responded with
        } else {
            $this->Flash->error(__('The article has not been created in the remote system'));
        }

        return $this->redirect($this->referer());
    }

    public function edit($articleId = null)
    {
        // An edit would follow similar approach to above. Sorry once I found out that
        // magento didn't require me to separate and edit from an add, I just post everything 
        // to a create method and Magento handles if needs to create or update.
    }
}

I have done my best to provide as much information to help solve in an imaginary solution. The key things I found were the query->set and the schema. The guys have done a great job of keeping it very similar to the cake controllers. i.e. add > newEntity > patchEntity > save or edit > get > patchEntity > save. If the primary key exists then it is going to fire an _executeUpdateQuery, if it doesn't exist then going to fire _executeCreateQuery. Going through all of the example projects helped me gain insight into what I needed to do. I hope this

from webservice.

solocommerce avatar solocommerce commented on May 21, 2024 1

Sorry for the delay, happy to help. I'm not sure how to add to the documentation into the repo, so I'll type it here, give me an hour or so to type it out.

from webservice.

davidyell avatar davidyell commented on May 21, 2024 1

For pagination see #42 or my fork https://github.com/davidyell/Webservice/tree/develop/src

Once 2.0 is released we can start working on the pagination implementation. If you could try out the RC release, and report issues. That would be very helpful.

from webservice.

ADmad avatar ADmad commented on May 21, 2024

Yes there aren't any implementations which do create/update/delete yet AFAIK.

Basically your webservice class will need to implement the _executeCreateQuery(), _executeUpdateQuery(), _executeDeleteQuery() methods along with the _executeReadQuery() method.

from webservice.

solocommerce avatar solocommerce commented on May 21, 2024

Thank you, I have those functions ready to go in my MagentoWebservice.php. The bit I'm struggling with is in my controller how do I initiate _executeCreateQuery() and _executeUpdateQuery() methods? Normally I would do something like

public function add()
{
    $article = $this->Articles->newEntity();
    if ($this->request->is('post')) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());
        if ($this->Articles->save($article)) {
        ...

and

public function edit($id = null)
{
    $article = $this->Articles->get($id);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());
        if ($this->Articles->save($article)) {
        ...

and Cake handles the is new. In the case of a remote web service there is no Entity to patch.

I was hoping there might be a

$this->Articles->create($article)
or
$this->Articles->update($article)

from webservice.

ADmad avatar ADmad commented on May 21, 2024

The bit I'm struggling with is in my controller how do I initiate _executeCreateQuery() and _executeUpdateQuery() methods?

You don't have to access those methods directly. You have to use the newEntity(), patchEntity() and save() methods of your Endpoint class.

from webservice.

solocommerce avatar solocommerce commented on May 21, 2024

I ended up figuring it out, I was over complicating the process. The process is exactly how I showed examples for in the controller. The missing piece for me was to set up the schema in the EndPoint. This is a great plugin, thank you.

from webservice.

ADmad avatar ADmad commented on May 21, 2024

It would be great if you could submit an update to the readme adding some info on how to handle create/update/delete.

from webservice.

r0kawa avatar r0kawa commented on May 21, 2024

I ended up figuring it out, I was over complicating the process. The process is exactly how I showed examples for in the controller. The missing piece for me was to set up the schema in the EndPoint. This is a great plugin, thank you.

I would love an example that I can follow because I'm struggling to handle the edit , update , delete example.

from webservice.

r0kawa avatar r0kawa commented on May 21, 2024

Hi @solocommerce , can you share with us some example for edit, add code. ?

from webservice.

r0kawa avatar r0kawa commented on May 21, 2024

Thank you very much @solocommerce . Have you solve the pagination for the listing ?

from webservice.

solocommerce avatar solocommerce commented on May 21, 2024

No I haven't sorry, this plugin has code for page https://github.com/CVO-Technologies/cakephp-github/blob/43647e53fd87a3ab93fe2f24ce3686b594d36241/src/Webservice/GitHubWebservice.php

from webservice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.