Geplaatst door Marlin Cremers op Fri, Oct 02 2015 01:08:20

We've been working on adding support for ORM like querying of data to the CakePHP Webservice plugin by UseMuffin. This allows you to work with webservices in the quick, easy and familiar way you're used to in CakePHP without having to dive into the details of a particular web API.

In this post I'll be explaining the parts of the ORM for webservices and what they're being used for.

The folder structure

Below is the folder structure that the ORM expects parts to be in. This applies to CakePHP applications and plugins.

Webservice
Contains one or more Webservices
Webservice/Driver
Contains the Driver
Model/Endpoint
Contains one or more Endpoints
Model/Resource
Contains one or more Resources

Webservice

The first and most important part is the Webservice. Its the bridge between the ORM and the actual API you're calling. This class is being called by the Query when data needs to be read from the service.

When data needs to be queried the Query will call the execute method which will go out to the API and fetch the necessary data. That data will then be parsed and formatted by the Webservice. When done it will return a ResultSet containing a set of Resources and the total number of results available (this will for example be used for pagination).

In the example below we create a Webservice to fetch articles from a webservice at example.com. Here you can see the articles being turned into a set of Resources and being returned by the Webservice.

<?php

namespace App\Webservice;

use App\Model\Resource\Article;
use Cake\Network\Http\Client;
use Muffin\Webservice\QueryLogger;
use Muffin\Webservice\ResultSet;
use Muffin\Webservice\WebserviceInterface;
use Muffin\Webservice\WebserviceQuery;

class ArticlesWebservice implements WebserviceInterface
{

    public function logger(QueryLogger $logger = null)
    {
        return new QueryLogger();
    }

    public function execute(WebserviceQuery $query)
    {
        switch ($query->action()) {
            case WebserviceQuery::ACTION_READ:
                $client = new Client();
                $response = $client->get('http://example.com/articles.json');

                if (!$response->isOk()) {
                    return false;
                }

                $resources = [];
                foreach ($response->articles as $article) {
                    $resources[] = new Article([
                        'id' => $article->id,
                        'title' => $article->title,
                        'body' => $article->body
                    ], [
                        'markClean' => true,
                        'markNew' => false,
                    ]);
                }

                return new ResultSet($resources, $response->total);
        }

        return false;
    }
}

Endpoint

The next part is the Endpoint, its essentially the Table equivalent of webservice ORM. This is what you use to get data out from your webservice using the ORM. For a comprehensive guide to Endpoints you can check out the CakePHP documentation about Tables.

The following methods are currently implemented in the base Endpoint:

  • find
  • findAll
  • findList
  • get

This list will increase in size as the ORM continues to be improved. This list will eventually be removed once the webservices ORM is on par with the CakePHP ORM.

Down here is an example of an Endpoint to get articles fromt the Webservice shown in the previous example. Note that it sets the Webservice which it gets from the Driver that'll have to create as well. For a post to get you started with a Driver check out this post by David Yell.

<?php

namespace App\Model\Endpoint;

use Cake\Datasource\ConnectionManager;
use Muffin\Webservice\Model\Endpoint;

class ArticlesEndpoint extends Endpoint
{

    public function initialize(array $config)
    {
        $this->webservice(ConnectionManager::get('ArticlesWebservice')->webservice());
    }
}

Resource

The last part is the Resource, this represents the actual results returned by the API.

The example below the Resource represents the articles returned when doing a find using on the Endpoint.

<?php

namespace App\Model\Resource;

use Muffin\Webservice\Model\Resource;

class Article extends Resource
{

}

More information

If you want some more information feel free to comment on this post.

 

0 Comments

Add new comment