linkmobility-php-client/src/Client.php

125 lignes
3.3 KiB
PHP
Brut Vue normale Historique

2022-06-20 14:47:33 +02:00
<?php
/**
2022-07-13 10:50:45 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-06-20 14:47:33 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 10:50:45 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
2022-06-20 14:47:33 +02:00
*/
2022-07-13 10:41:23 +02:00
declare(strict_types=1);
2022-06-24 16:00:56 +02:00
2022-06-20 14:47:33 +02:00
namespace D3\LinkmobilityClient;
2023-02-02 00:07:12 +01:00
use Assert\InvalidArgumentException;
2022-06-20 14:47:33 +02:00
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\Url\UrlInterface;
use GuzzleHttp\Client as GuzzleClient;
2022-07-10 21:52:37 +02:00
use GuzzleHttp\ClientInterface;
2022-06-24 16:00:56 +02:00
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleRetry\GuzzleRetryMiddleware;
use Psr\Http\Message\ResponseInterface;
2022-06-20 14:47:33 +02:00
class Client
{
private $accessToken;
public $apiUrl;
public $requestClient;
2022-07-10 21:52:37 +02:00
public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null)
2022-06-20 14:47:33 +02:00
{
$this->accessToken = $accessToken;
$this->apiUrl = $apiUrl ?: new Url();
$this->requestClient = $client ?: $this->getDefaultClient();
}
/**
* @return GuzzleClient
*/
protected function getDefaultClient(): GuzzleClient
{
$handlerStack = HandlerStack::create();
$handlerStack->push($this->getLoggerMiddleware());
$handlerStack->push($this->getRetryMiddleware());
return new GuzzleClient( [
'base_uri' => $this->apiUrl->getBaseUri(),
'handler' => $handlerStack
]);
2022-06-20 14:47:33 +02:00
}
2022-06-24 16:00:56 +02:00
/**
* @param RequestInterface $request
*
* @return Response\ResponseInterface
* @throws GuzzleException
* @throws InvalidArgumentException
2022-06-24 16:00:56 +02:00
*/
2022-07-13 10:41:23 +02:00
public function request(RequestInterface $request): Response\ResponseInterface
2022-06-20 14:47:33 +02:00
{
$request->validate();
2022-06-20 14:47:33 +02:00
return $request->getResponseInstance(
$this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions())
);
}
/**
* @param $url
* @param string $method
2022-06-24 16:00:56 +02:00
* @param array $options
2022-06-20 14:47:33 +02:00
*
* @return ResponseInterface
2022-06-20 14:47:33 +02:00
* @throws GuzzleException
*/
2022-07-13 10:41:23 +02:00
protected function rawRequest($url, string $method = RequestInterface::METHOD_GET, array $options = []): ResponseInterface
2022-06-20 14:47:33 +02:00
{
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
2023-01-18 09:14:38 +01:00
$response = $this->requestClient->request($method, $url, $options);
2022-07-14 11:47:45 +02:00
$response->getBody()->rewind();
2022-06-20 14:47:33 +02:00
return $response;
}
/**
2022-07-14 11:47:45 +02:00
* @return LoggerHandler
*/
2022-07-14 11:47:45 +02:00
public function getLoggerHandler(): LoggerHandler
{
2022-07-14 11:47:45 +02:00
return LoggerHandler::getInstance();
2022-07-04 13:31:12 +02:00
}
/**
* @param string $loglevel
*
* @return callable
*/
protected function getLoggerMiddleware(string $loglevel = 'debug'): callable
{
return Middleware::log(
$this->getLoggerHandler()->getLogger(),
new MessageFormatter(MessageFormatter::DEBUG),
$loglevel
);
}
/**
* @return callable
*/
protected function getRetryMiddleware(): callable
{
return GuzzleRetryMiddleware::factory([
'max_retry_attempts' => 3
]);
}
2022-07-13 10:41:23 +02:00
}