snieditions/api-wrapper

This package is abandoned and no longer maintained. No replacement package was suggested.

1.0.8 2016-05-19 14:37 UTC

This package is not auto-updated.

Last update: 2016-11-25 09:07:52 UTC


README

Wrapper to easily interface with an API created with snieditions/rest-api

Installation

composer require snieditions/api-wrapper

Configuration

Create wherever you want a .yml file. This will be our Wrapper's configuration file. It's configuration should look like below:

clients:
    your_api_main_endpoint:
        root: /dummy # the base of the endpoint (ex. /api/posts)
        classes: # the classes used by your API those will be returned by the client's calls
            single_resource: SNIEditions\ApiWrapper\Tests\Model\Resource # example
            collection_resource: SNIEditions\ApiWrapper\Tests\Model\ResourceCollection # example
        endpoints: # API endpoints
            list: # identifier. Will be used as a method name for your client
                method: GET # HTTP Method
                url: /{slug} # URL. Will be appended to root. can contain variable placeholder between brackets {}
                type: collection # references a class defined in "classes" above. no need to put _resource suffix
                child: single # ONLY for collections. Will be used to instanciate Collections' items. References a class defined in "classes" above. no need to put _resource suffix
            create:
                method: POST
                url: /
                type: single
            ...

Then you can create your various classes, by respecting the namespaces provided in classes IMPORTANT: Your classes HAVE TO inherit *SNIEditions\ApiWrapper\Model\ApiObjectInterface in order to work correctly

If you want to see a complete set of classes, you can look in the "Tests" directory of this library.

Usage (based on configuration above)

<?php
    use SNIEDITIONS\ApiWrapper\Config\Config;
    use SNIEDITIONS\ApiWrapper\Client\Wrapper;
    use SNIEditions\ApiWrapper\Tests\Model\ResourceCollection;
    use SNIEditions\ApiWrapper\Tests\Model\Resource;

    $wrapper = new Wrapper('http://localhost', new Config('api.yml'), 'user', 'pass') // pass is already encrypted by yourself

    /** @var ResourceCollection **/
    $resources = $wrapper['your_api_main_endpoint']->list(array('slug' => 'health'));

    echo get_class($resources['embedded']['posts'][0]); // outputs "SNIEditions\ApiWrapper\Tests\Model\Resource"

    ...