RestClient.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest;
  6. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  7. use Temando\Shipping\Rest\Exception\RestException;
  8. use Temando\Shipping\Rest\Exception\RestResponseException;
  9. use Temando\Shipping\Webservice\Exception\HttpException;
  10. use Temando\Shipping\Webservice\HttpClientInterface;
  11. use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
  12. /**
  13. * Temando HTTP REST Client
  14. *
  15. * @package Temando\Shipping\Rest
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  18. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link https://www.temando.com/
  20. */
  21. class RestClient implements RestClientInterface
  22. {
  23. /**
  24. * @var HttpClientInterfaceFactory
  25. */
  26. private $httpClientFactory;
  27. /**
  28. * RestClient constructor.
  29. * @param HttpClientInterfaceFactory $httpClientFactory
  30. */
  31. public function __construct(HttpClientInterfaceFactory $httpClientFactory)
  32. {
  33. $this->httpClientFactory = $httpClientFactory;
  34. }
  35. /**
  36. * @param string $uri
  37. * @param string $rawBody
  38. * @param string[] $headers
  39. * @return string
  40. * @throws RestException
  41. */
  42. public function post($uri, $rawBody, array $headers)
  43. {
  44. $httpClient = $this->httpClientFactory->create();
  45. $httpClient->setHeaders($headers);
  46. $httpClient->setUri($uri);
  47. $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
  48. $httpClient->setRawBody($rawBody);
  49. try {
  50. $response = $httpClient->send(HttpClientInterface::METHOD_POST);
  51. } catch (HttpException $e) {
  52. $errorCode = $e->getCode();
  53. if ($errorCode < 500 && $errorCode > 401) {
  54. // handle client errors with parseable content
  55. throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
  56. } else {
  57. throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
  58. }
  59. }
  60. return $response;
  61. }
  62. /**
  63. * @param string $uri
  64. * @param string $rawBody
  65. * @param string[] $headers
  66. * @return string
  67. * @throws RestException
  68. */
  69. public function put($uri, $rawBody, array $headers)
  70. {
  71. $httpClient = $this->httpClientFactory->create();
  72. $httpClient->setHeaders($headers);
  73. $httpClient->setUri($uri);
  74. $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
  75. $httpClient->setRawBody($rawBody);
  76. try {
  77. $response = $httpClient->send(HttpClientInterface::METHOD_PUT);
  78. } catch (HttpException $e) {
  79. $errorCode = $e->getCode();
  80. if ($errorCode < 500 && $errorCode >= 400) {
  81. // handle client errors with parseable content
  82. throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
  83. } else {
  84. throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
  85. }
  86. }
  87. return $response;
  88. }
  89. /**
  90. * @param string $uri
  91. * @param string $rawBody
  92. * @param string[] $headers
  93. * @return string
  94. * @throws RestException
  95. */
  96. public function patch($uri, $rawBody, array $headers)
  97. {
  98. $httpClient = $this->httpClientFactory->create();
  99. $httpClient->setHeaders($headers);
  100. $httpClient->setUri($uri);
  101. $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
  102. $httpClient->setRawBody($rawBody);
  103. try {
  104. $response = $httpClient->send(HttpClientInterface::METHOD_PATCH);
  105. } catch (HttpException $e) {
  106. $errorCode = $e->getCode();
  107. if ($errorCode < 500 && $errorCode >= 400) {
  108. // handle client errors with parseable content
  109. throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
  110. } else {
  111. throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
  112. }
  113. }
  114. return $response;
  115. }
  116. /**
  117. * @param string $uri
  118. * @param string[] $queryParams
  119. * @param string[] $headers
  120. * @return string
  121. * @throws RestException
  122. */
  123. public function get($uri, array $queryParams, array $headers)
  124. {
  125. $httpClient = $this->httpClientFactory->create();
  126. $httpClient->setHeaders($headers);
  127. $httpClient->setUri($uri);
  128. $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
  129. $httpClient->setParameterGet($queryParams);
  130. try {
  131. $response = $httpClient->send(HttpClientInterface::METHOD_GET);
  132. } catch (HttpException $e) {
  133. $errorCode = $e->getCode();
  134. if ($errorCode < 500 && $errorCode >= 400) {
  135. // handle client errors with parseable content
  136. throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
  137. } else {
  138. throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
  139. }
  140. }
  141. return $response;
  142. }
  143. /**
  144. * @param string $uri
  145. * @param string[] $headers
  146. *
  147. * @return string
  148. * @throws RestClientErrorException
  149. * @throws RestResponseException
  150. */
  151. public function delete($uri, array $headers)
  152. {
  153. $httpClient = $this->httpClientFactory->create();
  154. $httpClient->setHeaders($headers);
  155. $httpClient->setUri($uri);
  156. $httpClient->setOptions(['trace' => 1, 'maxredirects' => 0, 'timeout' => 30, 'useragent' => 'M2']);
  157. try {
  158. $response = $httpClient->send(HttpClientInterface::METHOD_DELETE);
  159. } catch (HttpException $e) {
  160. $errorCode = $e->getCode();
  161. if ($errorCode < 500 && $errorCode >= 400) {
  162. // handle client errors with parseable content
  163. throw new RestClientErrorException($e->getMessage(), $e->getCode(), $e);
  164. } else {
  165. throw new RestResponseException($e->getMessage(), $e->getCode(), $e);
  166. }
  167. }
  168. return $response;
  169. }
  170. }