Skip to content

Latest commit

 

History

History
207 lines (175 loc) · 8.19 KB

README.md

File metadata and controls

207 lines (175 loc) · 8.19 KB

Sylius API PHP client library

Build status... Code Quality

PHP client for RESTful Sylius API!

Usage

Initialize client with OAuth2 authentication and mapping

use GuzzleHttp\Client;
use Sylius\Api\Client as SyliusClient;
use Sylius\Api\Map\ArrayUriMap;
use Sylius\Api\ApiResolver;
use CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken;
use CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber;

$oauth2Client = new Client([
    'base_url' => 'http://demo.sylius.org',
]);

$config = [
    'token_url' => 'oauth/v2/token',
    'client_id' => '1_demo_client',
    'client_secret' => 'secret_demo_client',
    'username' => 'api@example.com',
    'password' => 'api'
];

$token = new PasswordCredentials($oauth2Client, $config);
$refreshToken = new RefreshToken($oauth2Client, $config);

$oauth2 = new Oauth2Subscriber($token, $refreshToken);

// create sylius client
$syliusClient = SyliusClient::createFromUrl('http://demo.sylius.org/api/', $oauth2);

// initialize uri mapping for custom uris, all other uris are autogenerated (plural name of the resource, in accordance with RESTful API resource naming best practices)
$uriMap = new ArrayUriMap(
    [
        'product-variants' => 'products/{productId}/variants',
        'taxons'           => 'taxonomies/{taxonomyId}/taxons',
        'items'            => 'orders/{orderId}/items',
        'coupons'          => 'promotions/{promotionId}/coupons',
        'provinces'        => 'countries/{countryId}/provinces'
    ]
);
// initializer api resolver
$apiResolver = new ApiResolver($syliusClient, $uriMap);

If you use Symfony2 framework, you can also register some services, initialize Sylius API Client and inject it to other services.

parameters:
    app.http_client.sylius.base_url: 'http://local.sylius.dev/app_dev.php/'
    app.http_client.sylius.api_url: 'http://local.sylius.dev/app_dev.php/api/'
    app.http_client.sylius.token_url: 'oauth/v2/token'
    app.sylius_client.id: 1_demo_client
    app.sylius_client.secret: secret_demo_client
    app.sylius_client.username: api@example.com
    app.sylius_client.password: api
    app.sylius_taxonomy.uri: taxonomies

services:
    #Http client that is used to obtain OAuth2 token
    app.http_client.sylius_oauth:
        class: GuzzleHttp\Client
        arguments:
            - { base_url: %app.http_client.sylius.base_url% }
    #Password credentials that use http client to authenticate user with given data
    app.oauth2.grant_type.password_credentials:
        class: CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials
        arguments:
            - @app.http_client.sylius_oauth
            - { token_url: %app.http_client.sylius.token_url%, client_id: %app.sylius_client.id%, client_secret: %app.sylius_client.secret%, username: %app.sylius_client.username%, password: %app.sylius_client.password% }
    #Refresh token that use http client to refresh oauth2 token
    app.oauth2.grant_type.refresh_token:
        class: CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken
        arguments:
            - @app.http_client.sylius_oauth
            - { token_url: %app.http_client.sylius.token_url%, client_id: %app.sylius_client.id%, client_secret: %app.sylius_client.secret%, username: %app.sylius_client.username%, password: %app.sylius_client.password% }
    #Subscriber used by Sylius API Client to provide OAuth2 authentication
    app.oauth2.subscriber.sylius_api:
        class: CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber
        arguments: [@app.oauth2.grant_type.password_credentials, @app.oauth2.grant_type.refresh_token]
    #Http client that is used to connect with Sylius API
    app.http_client.sylius_api:
        class: GuzzleHttp\Client
        arguments:
            - { base_url: '%app.http_client.sylius.api_url%', defaults: { auth: 'oauth2', subscribers: [@app.oauth2.subscriber.sylius_api] } }
    #Sylius API Client
    app.api_client.sylius:
        class: Sylius\Api\Client
        arguments: [@app.http_client.sylius_api]

    #Example API
    app.sylius_taxonomy.api:
        class: Sylius\Api\GenericApi
        arguments: [@app.api_client.sylius, %app.sylius_taxonomy.uri%]

    #Uri map with custom uris
    app.api_resolver.uri_map.sylius:
        class: Sylius\Api\Map\ArrayUriMap
        arguments:
            - { product-variants: 'products/{productId}/variants', taxons: 'taxonomies/{taxonomyId}/taxons', items: 'orders/{orderId}/items', coupons: 'promotions/{promotionId}/coupons', provinces: 'countries/{countryId}/provinces' }
    #Initialize Api resolver with uri map
    app.api_resolver.sylius:
        class: Sylius\Api\ApiResolver
        arguments: [@app.api_client.sylius, @app.api_resolver.uri_map.sylius]

Get API for resource

#Obtain api using api resolver
$taxonomiesApi = $apiResolver->getApi('taxonomies');
#or register it as a service and obtain it from container
$taxonomiesApi = $container->get('app.sylius_taxonomy.api');
$taxonsApi = $apiResolver->getApi('taxons');

Create resource

// right now because of translations you have to do it this way (will be changed in the nearest future to simply allow for ['name' => 'My taxonomy'])
$taxonomy = $taxonomiesApi->create(['translations' => ['en' => ['name' => 'My taxonomy']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->create(['translations' => ['en' => ['name' => 'My taxon']]], ['taxonomyId' => $taxonomy['id']);

Update resource

$taxonomy = $taxonomiesApi->update($taxonomyId, ['translations' => ['en' => ['name' => 'My taxonomy updated']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->update($taxonId, $taxonomy['translations' => ['en' => ['name' => 'My taxon' updated]]], ['taxonomyId' => $taxonomyId]);

Get resource

$taxonomy = $taxonomiesApi->get($taxonomyId);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->get($taxonId, ['taxonomyId' => $taxonomyId]);

Get all resources

  • Get paginated resources
// you can getPaginated resources (it returns array)
$taxonomies = $taxonomiesApi->getPaginated($page, $limitPerPage);
$taxons = $taxonsApi->getPaginated($page, $limitPerPage, ['taxonomyId' => $taxonomyId]);
  • Create paginator for resource
// you can create taxonomies paginator
$taxonomiesPaginator = $taxonomiesApi->createPaginator($limitPerPage);
// for custom uris you have to specify uri parameters ('taxonomyId'), rest is the same
$taxonsPaginator = $taxonsApi->createPaginator($limitPerPage, ['taxonomyId' => $taxonomyId]);
// returns array of elements for current page
$taxonomies = $taxonomiesPaginator->getCurrentPageResults();
// ... (do something with current page taxonomies)
// and go through all results
while ($taxonomiesPaginator->hasNextPage()) {
    $taxonomiesPaginator->nextPage();
    $taxonomies = $taxonomiesPaginator->getCurrentPageResults();
    // ... (do something with current page taxonomies)
}
  • Get all resources
// getAll method gets all resources, so you have to be careful about the memory usage
$taxonomies = $taxonomiesApi->getAll();
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxons = $taxonsApi->getAll(['taxonomyId' => $taxonomyId]);

Delete resource

// returns whether the deletion was successful or not
// for custom uris you have to specify uri parameters ('taxonomyId')
$trueOrFalse = $taxonsApi->delete($taxonId, ['taxonomyId' => $taxonomyId]);
$trueOrFalse = $taxonomiesApi->delete($taxonomyId);

Bug tracking

This component uses GitHub issues. If you have found bug, please create an issue.

MIT License

License can be found here.

Authors

The library was originally created by:

See the list of contributors.