1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
<?php
namespace FrontBundle\Client;
use FrontBundle\Services\Http\RequestBuilder;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
class ApiClient implements ClientInterface
{
private $client;
private $requestBuilder;
public function __construct(GuzzleClientInterface $client, RequestBuilder $requestBuilder)
{
if ($client->getConfig('base_url') !== $requestBuilder->getBaseUrl()) {
throw new \InvalidArgumentException(
sprintf(
'Expected base url of client and request build to match. Got respectively "%s" and "%s" instead.',
$client->getConfig('base_url'),
$requestBuilder->getBaseUrl()
)
);
}
$this->client = $client;
$this->requestBuilder = $requestBuilder;
}
public function createRequest($method, $url = null, $token = null, array $options = [])
{
return $this->requestBuilder->createRequest($method, $url, $token, $options);
}
public function request($method, $url = null, $token = null, $options = [])
{
return $this->client->send($this->createRequest($method, $url, $token, $options));
}
public function send(RequestInterface $request)
{
return $this->client->send($request);
}
}