AnytimeSA / anytime-api-php-sdk

Anytime api php sdk
https://developers.anyti.me/en/api
GNU General Public License v3.0
8 stars 5 forks source link

Allow to pass a custom guzzle client #55

Open adaniloff opened 3 years ago

adaniloff commented 3 years ago

Hi,

We're currently using your SDK. In your ApiClientFactory.php (method create), you generate a new instance of a guzzle client.

I'd like to know if you could make it configurable ? For exemple:

<?php

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

class ApiClientFactory
{
    /**
     * @var FileReaderInterface
     */
    private $fileReader;

    /**
     * @var ClientInterface
     */
    private $client;

    /**
     * ApiClientFactory constructor.
     *
     * @param FileReaderInterface|null $fileReader Specific file reader implementing FileReaderInterface. Default is DiskFileReader.
     * @param ClientInterface|null $client Specific guzzle client implementing ClientInterface. Default is Client.
     */
    public function __construct(FileReaderInterface $fileReader = null, ClientInterface $client = null)
    {
        if(!$fileReader) {
            $fileReader = new DiskFileReader();
        }
        $this->fileReader = $fileReader;
        $this->client = $client ?? new Client([ 'timeout'   =>  0 ]);
    }

    /**
     * @param array $params Accepted params: version, client-id, client-secret, username, password, private-key
     * @param string $environment
     * @return ApiClient
     */
    public function create($params = [], $environment = Environment::SANDBOX)
    {
        if(!Environment::isValidEnvironment($environment)) {
            throw new \RuntimeException('Invalid environment "' . $environment . '"');
        }

        $setting = $this->createSettingObject($environment, $params);
        $modelResponsePopulator = new ModelResponsePopulator();
        $modelResponseFactory = new ModelResponseFactory($modelResponsePopulator);
        $jsonResponseParser = new JsonResponseParser();
        $binaryResponseParser = new BinaryResponseParser();

        return new ApiClient(
            $setting,
            new IOList(
                new IOFactory(
                    $this->client,
                    $setting,
                    new ModelRequestFactory(),
                    $modelResponseFactory,
                    new RequestDirectorFactory($setting, $this->fileReader),
                    new ApiClientExceptionFactory(
                        $modelResponsePopulator,
                        $modelResponseFactory,
                        $jsonResponseParser
                    ),
                    $modelResponsePopulator,
                    $jsonResponseParser,
                    $binaryResponseParser,
                    new ResponseAuthenticator()
                )
            )
        );
    }

There are many other ways to do so, but we would like to be able to pass a custom Guzzle Client.

adaniloff commented 2 years ago

Ping ?