2017 © Pedro Peláez
 

library lib-rest-client-common

Base classes/helpers used in REST Clients

image

paysera/lib-rest-client-common

Base classes/helpers used in REST Clients

  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 15 Versions
  • 59 % Grown

The README.md

lib-rest-client-common

Generic library for RESTful API clients, (*1)

Usage

You should create ClientFactory class, which extends ClientFactoryAbstract. In ClientFactory you can override any parent configuration if needed., (*2)

Simple example of ClientFactory:, (*3)

class TestClientFactory extends ClientFactoryAbstract
{
    const DEFAULT_BASE_URL = 'http://example.com/test/rest/v1/{locale}/';

    private $apiClient;

    public function __construct(array $options)
    {
        $this->apiClient = $this->createApiClient($options);
    }

    public function getTestClient()
    {
        return new TestClient($this->apiClient);
    }
}

Using this pattern you can reuse same ApiClient with it's authentication and other options in different APIs., (*4)

In addition to ClientFactory, you should create also the Client itself. Finally you will use the Client itself, so it should contain all the methods your API provides., (*5)

Simple example of TestClient:, (*6)

class TestClient
{
    private $apiClient;

    public function __construct(ApiClient $apiClient)
    {
        $this->apiClient = $apiClient;
    }

    public function withOptions(array $options)
    {
        return new TestClient($this->apiClient->withOptions($options));
    }

    /**
     * @return array
     */
    public function getSomething()
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_GET,
            sprintf('/something'),
            null
        );
        return $this->apiClient->makeRequest($request);
    }
}

You should implement mapping or data transformation where applicable in TestClient methods., (*7)

Example:


use Paysera\Client\CategoryClient\ClientFactory; $clientFactory = new ClientFactory([ 'base_url' => 'custom base url', 'auth_base_url' => 'custom auth base url', 'basic' => [ 'username' => 'user', 'password' => 'pass' ], 'oauth' => [ 'token' => [ 'access_token' => 'your oauth access token', 'refresh_token' => 'your oauth refresh token', ], ], 'mac' => [ 'mac_id' => 'mac id', 'mac_secret' => 'mac secret', 'parameters' => [ // list of needed parameters ] ], 'url_parameters' => [ 'locale' => 'en', // list of base_url placeholder parameter values ], 'headers' => [ 'Accept-Language' => 'en', ], // other configuration options ]); $testClient = $clientFactory->getTestClient(); $data = $testClient->getSomething();
  • Please note that only single authentication mechanism is supported.

In case you want to change some configuration options at runtime, use TestClient::withOptions():, (*8)

$factory = new TestClientFactory([
    MacAuthentication::TYPE => [
        'mac_id' => $macId,
        'mac_secret' => $macSecret,
        'parameters' => [
            'user_id' => 100,
        ]
    ]
]);

$client = $factory->getTestClient();

$client2 = $factory->getTestClient()->withOptions([
    MacAuthentication::TYPE => [
        'parameters' => ['user_id' => 999],
    ]
]);

Here for $client2 only user_id in parameters will be changed. Other configuration, like mac_id, mac_secret will be left intact., (*9)

The Versions

27/07 2018

dev-master

9999999-dev

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

27/07 2018

2.1.0

2.1.0.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

26/07 2018

2.0.2

2.0.2.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

19/07 2018

2.0.1

2.0.1.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

19/07 2018

2.0.0

2.0.0.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

19/06 2018

1.1.1

1.1.1.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

18/06 2018

1.1.0

1.1.0.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

13/06 2018

1.0.7

1.0.7.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

03/07 2017

1.0.6

1.0.6.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

03/07 2017

1.0.5

1.0.5.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

The Development Requires

14/06 2017

1.0.4

1.0.4.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

06/06 2017

1.0.3

1.0.3.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

10/04 2017

1.0.2

1.0.2.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

10/04 2017

1.0.1

1.0.1.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires

 

27/02 2017

1.0.0

1.0.0.0

Base classes/helpers used in REST Clients

  Sources   Download

The Requires