HttpClient.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Webservice;
  6. use Temando\Shipping\Webservice\Exception\HttpRequestException;
  7. use Temando\Shipping\Webservice\Exception\HttpResponseException;
  8. /**
  9. * Wrapper around ZF2 HTTP Client
  10. *
  11. * @package Temando\Shipping\Webservice
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link http://www.temando.com/
  15. */
  16. class HttpClient implements HttpClientInterface
  17. {
  18. /**
  19. * @var \Zend\Http\Client
  20. */
  21. private $client;
  22. /**
  23. * HttpClient constructor.
  24. * @param \Zend\Http\Client $client
  25. */
  26. public function __construct(\Zend\Http\Client $client)
  27. {
  28. $this->client = $client;
  29. }
  30. /**
  31. * @param string[] $headers
  32. * @return \Zend\Http\Client
  33. */
  34. public function setHeaders(array $headers)
  35. {
  36. return $this->client->setHeaders($headers);
  37. }
  38. /**
  39. * @param string $uri
  40. * @return \Zend\Http\Client
  41. */
  42. public function setUri($uri)
  43. {
  44. return $this->client->setUri($uri);
  45. }
  46. /**
  47. * @param string[] $options
  48. * @return \Zend\Http\Client
  49. */
  50. public function setOptions(array $options)
  51. {
  52. return $this->client->setOptions($options);
  53. }
  54. /**
  55. * @param string $rawBody
  56. * @return \Zend\Http\Client
  57. */
  58. public function setRawBody($rawBody)
  59. {
  60. return $this->client->setRawBody($rawBody);
  61. }
  62. /**
  63. * @param string[] $queryParams
  64. * @return \Zend\Http\Client
  65. */
  66. public function setParameterGet($queryParams)
  67. {
  68. return $this->client->setParameterGet($queryParams);
  69. }
  70. /**
  71. * @param string $method
  72. * @return string The response body
  73. * @throws HttpRequestException
  74. * @throws HttpResponseException
  75. */
  76. public function send($method)
  77. {
  78. $this->client->setMethod($method);
  79. try {
  80. $response = $this->client->send();
  81. } catch (\Zend\Http\Exception\RuntimeException $e) {
  82. throw new HttpRequestException($e->getMessage(), $e->getCode(), $e);
  83. }
  84. if (!$response->isSuccess()) {
  85. throw new HttpResponseException(
  86. $response->getBody(),
  87. $response->getStatusCode(),
  88. null,
  89. $response->getHeaders()->toString()
  90. );
  91. }
  92. return $response->getBody();
  93. }
  94. }