2017 © Pedro Peláez
 

symfony-bundle rest-client-sdk-bundle

Rest Client SDK Bundle for hydra API

image

mapado/rest-client-sdk-bundle

Rest Client SDK Bundle for hydra API

  • Wednesday, April 25, 2018
  • by jdeniau
  • Repository
  • 4 Watchers
  • 5 Stars
  • 4,117 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 5 Open issues
  • 33 Versions
  • 6 % Grown

The README.md

Rest Client Sdk Bundle StyleCI

Symfony bundle for mapado/rest-client-sdk, (*1)

Installation

composer require mapado/rest-client-sdk-bundle

Symfony flex

Add it to your config/bundle.php, (*2)

return [
    // ...
    Mapado\RestClientSdkBundle\MapadoRestClientSdkBundle::class => ['all' => true],
];

Without flex

Add it to your AppKernel.php, (*3)

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Mapado\RestClientSdkBundle\MapadoRestClientSdkBundle(),
        )

        // ...

Usage

Add this in your configuration file :, (*4)

Symfony Flex: config/packages/mapado_rest_client_sdk.yaml, not flex: app/config/config.yml, (*5)

mapado_rest_client_sdk:
    # debug: %kernel.debug%
    # cache_dir: '%kernel.cache_dir%/mapado/rest_client_sdk_bundle/'
    entity_managers:
        foo:
            server_url: 'http://foo.com:8080'
            request_headers:
                MyHeader: 'MyValue'
            mappings:
                prefix: /v1
                configuration:
                    collectionKey: 'items' # default is "hydra:member"
                dir: '%kernel.root_dir%/../src/Foo/Bar/Entity/'
            cache:
                cache_item_pool: 'psr6_cache_provider' # default is null
                cache_prefix: 'my_prefix' # default is null

The bundle registers one service for each entity manager that you defined (in this case just one for foo)., (*6)

The name of the service will be: mapado.rest_client_sdk.<manager_name>., (*7)

As I named my entity manager foo, The service name here will be : mapado.rest_client_sdk.foo., (*8)

If you use Symfony 3.3+ autowiring feature, you may want to alias something like this:, (*9)

services:
    # ...
    Mapado\RestClientSdk\SdkClient: '@mapado.rest_client_sdk.foo'

If you have multiple entity managers, Symfony documentation explains how to deal with multiple implementation of the same type., (*10)

Imagine I have the following model, as defined in the component documentation:, (*11)

/**
 * @Rest\Entity(key="carts", client="Acme\Foo\Bar\CartClient")
 */
class Cart {
    // ...
}

I can now do something like this:, (*12)

$cartList = $this->get('mapado.rest_client_sdk.foo')
    ->getRepository('carts')
    ->findAll(); // `carts` is the `key` defined in the model

$cart = $this->get('mapado.rest_client_sdk.foo')
    ->getRepository('carts')
    ->find(1);

For a more complete information on the usage, I recommand you to look at the component documentation, (*13)

Using cache

By providing a Psr6 Psr\Cache\CacheItemPoolInterface to cache.cache_item_pool, each entity and entityList fetched will be stored in cache., (*14)

For example at Mapado, we are using the Symfony Array cache adapter like this:, (*15)

services:
    cache.rest_client_sdk:
        class: 'Symfony\Component\Cache\Adapter\ArrayAdapter'
        arguments:
            - 0
            - false # avoid serializing entities

mapado_rest_client_sdk:
    entity_managers:
        foo:
            server_url: '%server.url%'
            mappings:
                prefix: /v1
                dir: '%kernel.root_dir%/../src/Mapado/Foo/Model/'
            cache:
                cache_item_pool: 'cache.rest_client_sdk' # the id of the cache service
                cache_prefix: 'mapado_rest_client_'

Overriding default http client

Sometime, you need to override the base HTTP client. At Mapado, we like to add a the current page as a Referrer, pass down the current Accept-Language header, or send an Authorization for our API call., (*16)

As the HTTP client is automatically generated, the only way to do that is to decorate your default client :, (*17)

# config/services.yaml or app/config/service.yml

mapado.rest_client_sdk.decorating_ticketing
services:
  # ... 
  mapado.rest_client_sdk.decorating_foo_http_client:
      class:     'App\Rest\Decorator\DecoratingClient'
      decorates: 'mapado.rest_client_sdk.foo_http_client'
      arguments: ['@mapado.rest_client_sdk.decorating_foo_http_client.inner']
      public:    true

<?php

namespace App\Rest\Decorator;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class DecoratingClient implements ClientInterface
{
    /**
     * @var Client
     */
    private $decoratedClient;

    public function __construct(Client $decoratedClient)
    {
        $this->decoratedClient = $decoratedClient;
    }

    /**
     * {@inheritdoc}
     */
    public function send(RequestInterface $request, array $options = []): ResponseInterface
    {
        return $this->decoratedClient->send($request, $options);
    }

    /**
     * {@inheritdoc}
     */
    public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
    {
        return $this->decoratedClient->sendAsync($request, $options);
    }

    /**
     * {@inheritdoc}
     */
    public function request($method, $uri, array $options = []): ResponseInterface
    {
        if (!isset($options['headers'])) {
            $options['headers'] = [];
        }

        $options['headers'] = array_merge(
            $options['headers'],
            [
                'Authorization' => 'Bearer my-great-token',
                'Accept-Language' => 'fr',
            ]
        );

        return $this->decoratedClient->request($method, $uri, $options);
    }

    /**
     * {@inheritdoc}
     */
    public function requestAsync($method, $uri, array $options = []): PromiseInterface
    {
        return $this->decoratedClient->requestAsync($method, $uri, $options);
    }

    public function getConfig($option = null)
    {
        return $this->decoratedClient->getConfig($option);
    }
}

The Versions

31/03 2017
24/03 2017
23/01 2017
28/11 2016
09/11 2016
08/11 2016
11/10 2016
11/10 2016

dev-fc-feat-update-deps

dev-fc-feat-update-deps

Rest Client SDK Bundle for hydra API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dimitri Allegoet

13/06 2016
10/06 2016
10/06 2016
08/06 2016
08/06 2016
07/06 2016
03/06 2016
03/05 2016
05/04 2016
15/03 2016
07/03 2016
01/03 2016
23/10 2015