snieditions / api-wrapper
Installs: 1 157
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 11
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.9
- symfony/yaml: ~2.8
Requires (Dev)
- fabpot/php-cs-fixer: ^1.10
- phpunit/phpunit: ^5.1
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"
...