PHPackages                             mapado/rest-client-sdk-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mapado/rest-client-sdk-bundle

ActiveSymfony-bundle[API Development](/categories/api)

mapado/rest-client-sdk-bundle
=============================

Rest Client SDK Bundle for hydra API

v3.0.0(1mo ago)524.7k↓25%1[4 issues](https://github.com/mapado/rest-client-sdk-bundle/issues)MITPHPCI passing

Since Oct 23Pushed 1mo ago7 watchersCompare

[ Source](https://github.com/mapado/rest-client-sdk-bundle)[ Packagist](https://packagist.org/packages/mapado/rest-client-sdk-bundle)[ RSS](/packages/mapado-rest-client-sdk-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (8)Versions (49)Used By (0)

Rest Client Sdk Bundle [![StyleCI](https://camo.githubusercontent.com/b26e459dcfe4ccce442bd9f182b597fc97136d00a102b64e9eb08030c742bd0f/68747470733a2f2f7374796c6563692e696f2f7265706f732f34343830303836362f736869656c64)](https://styleci.io/repos/44800866)
============================================================================================================================================================================================================================================================

[](#rest-client-sdk-bundle-)

Symfony bundle for [mapado/rest-client-sdk](https://github.com/mapado/rest-client-sdk)

Installation
------------

[](#installation)

```
composer require mapado/rest-client-sdk-bundle
```

### Symfony flex

[](#symfony-flex)

Add it to your `config/bundle.php`

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

### Without flex

[](#without-flex)

Add it to your AppKernel.php

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

        // ...
```

Usage
-----

[](#usage)

Add this in your configuration file :

Symfony Flex: `config/packages/mapado_rest_client_sdk.yaml`, not flex: `app/config/config.yml`

```
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`).

The name of the service will be: `mapado.rest_client_sdk.`.

As I named my entity manager `foo`, The service name here will be : `mapado.rest_client_sdk.foo`.

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

```
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](https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type).

Imagine I have the following model, as defined in the [component documentation](https://github.com/mapado/rest-client-sdk#configuration):

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

I can now do something like this:

```
$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](https://github.com/mapado/rest-client-sdk#usage)

### Using cache

[](#using-cache)

By providing a Psr6 [`Psr\Cache\CacheItemPoolInterface`](http://www.php-fig.org/psr/psr-6/#cacheitempoolinterface) to `cache.cache_item_pool`, each entity and entityList fetched will be stored in cache.

For example at Mapado, we are using the [Symfony Array cache adapter](http://symfony.com/doc/current/components/cache/cache_pools.html#array-cache-adapter) like this:

```
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

[](#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.

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

```
# 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
```

```
