123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Rest;
- use Temando\Shipping\Rest\Exception\RestClientErrorException;
- use Temando\Shipping\Rest\Exception\RestException;
- use Temando\Shipping\Rest\Exception\RestResponseException;
- use Temando\Shipping\Webservice\Exception\HttpException;
- use Temando\Shipping\Webservice\HttpClientInterface;
- use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
- /**
- * Temando HTTP REST Client
- *
- * @package Temando\Shipping\Rest
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
- * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * @link https://www.temando.com/
- */
- class RestClient implements RestClientInterface
- {
- /**
- * @var HttpClientInterfaceFactory
- */
- private $httpClientFactory;
- /**
- * RestClient constructor.
- * @param HttpClientInterfaceFactory $httpClientFactory
- */
- public function __construct(HttpClientInterfaceFactory $httpClientFactory)
- {
- $this->httpClientFactory = $httpClientFactory;
- }
- /**
- * @param string $uri
- * @param string $rawBody
- * @param string[] $headers
- * @return string
- * @throws RestException
- */
- public function post($uri, $rawBody, array $headers)
- {
- $httpClient = $this->httpClientFactory->create();
- $httpClient->setHeaders($headers);
- $httpClient->setUri($uri);
- $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
- $httpClient->setRawBody($rawBody);
- try {
- $response = $httpClient->send(HttpClientInterface::METHOD_POST);
- } catch (HttpException $e) {
- $errorCode = $e->getCode();
- if ($errorCode < 500 && $errorCode > 401) {
- // handle client errors with parseable content
- throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
- } else {
- throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
- }
- }
- return $response;
- }
- /**
- * @param string $uri
- * @param string $rawBody
- * @param string[] $headers
- * @return string
- * @throws RestException
- */
- public function put($uri, $rawBody, array $headers)
- {
- $httpClient = $this->httpClientFactory->create();
- $httpClient->setHeaders($headers);
- $httpClient->setUri($uri);
- $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
- $httpClient->setRawBody($rawBody);
- try {
- $response = $httpClient->send(HttpClientInterface::METHOD_PUT);
- } catch (HttpException $e) {
- $errorCode = $e->getCode();
- if ($errorCode < 500 && $errorCode >= 400) {
- // handle client errors with parseable content
- throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
- } else {
- throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
- }
- }
- return $response;
- }
- /**
- * @param string $uri
- * @param string $rawBody
- * @param string[] $headers
- * @return string
- * @throws RestException
- */
- public function patch($uri, $rawBody, array $headers)
- {
- $httpClient = $this->httpClientFactory->create();
- $httpClient->setHeaders($headers);
- $httpClient->setUri($uri);
- $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
- $httpClient->setRawBody($rawBody);
- try {
- $response = $httpClient->send(HttpClientInterface::METHOD_PATCH);
- } catch (HttpException $e) {
- $errorCode = $e->getCode();
- if ($errorCode < 500 && $errorCode >= 400) {
- // handle client errors with parseable content
- throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
- } else {
- throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
- }
- }
- return $response;
- }
- /**
- * @param string $uri
- * @param string[] $queryParams
- * @param string[] $headers
- * @return string
- * @throws RestException
- */
- public function get($uri, array $queryParams, array $headers)
- {
- $httpClient = $this->httpClientFactory->create();
- $httpClient->setHeaders($headers);
- $httpClient->setUri($uri);
- $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
- $httpClient->setParameterGet($queryParams);
- try {
- $response = $httpClient->send(HttpClientInterface::METHOD_GET);
- } catch (HttpException $e) {
- $errorCode = $e->getCode();
- if ($errorCode < 500 && $errorCode >= 400) {
- // handle client errors with parseable content
- throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
- } else {
- throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
- }
- }
- return $response;
- }
- /**
- * @param string $uri
- * @param string[] $headers
- *
- * @return string
- * @throws RestClientErrorException
- * @throws RestResponseException
- */
- public function delete($uri, array $headers)
- {
- $httpClient = $this->httpClientFactory->create();
- $httpClient->setHeaders($headers);
- $httpClient->setUri($uri);
- $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
- try {
- $response = $httpClient->send(HttpClientInterface::METHOD_DELETE);
- } catch (HttpException $e) {
- $errorCode = $e->getCode();
- if ($errorCode < 500 && $errorCode >= 400) {
- // handle client errors with parseable content
- throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
- } else {
- throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
- }
- }
- return $response;
- }
- }
|