* @link https://www.oxidmodule.com */ declare(strict_types=1); namespace D3\LinkmobilityClient; use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\Exceptions\ExceptionMessages; use D3\LinkmobilityClient\Request\RequestInterface; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; class Client { private $accessToken; public $apiUrl; public $requestClient; public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null) { $this->accessToken = $accessToken; $this->apiUrl = $apiUrl ?: new Url(); $this->requestClient = $client ?: new \GuzzleHttp\Client([ 'base_uri' => $this->apiUrl->getBaseUri() ]); } /** * @param RequestInterface $request * * @return Response\ResponseInterface * @throws ApiException * @throws GuzzleException * @throws InvalidArgumentException */ public function request(RequestInterface $request): Response\ResponseInterface { $request->validate(); return $request->getResponseInstance( $this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions()) ); } /** * @param $url * @param string $method * @param array $options * * @return ResponseInterface * @throws ApiException * @throws GuzzleException */ protected function rawRequest($url, string $method = RequestInterface::METHOD_GET, array $options = []): ResponseInterface { $options['headers']['Authorization'] = 'Bearer '.$this->accessToken; $this->getLoggerHandler()->getLogger()->debug('request '.$url, $options); $response = $this->requestClient->request( $method, $url, $options ); if ($response->getStatusCode() != 200) { $message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode()); $this->getLoggerHandler()->getLogger()->error($message); throw new ApiException($message); } $response->getBody()->rewind(); $this->getLoggerHandler()->getLogger()->debug('response', [$response->getBody()->getContents()]); return $response; } /** * @return LoggerHandler */ public function getLoggerHandler(): LoggerHandler { return LoggerHandler::getInstance(); } }