1 | <?php |
||
21 | class HttpGuzzleClient implements HttpClientInterface |
||
22 | { |
||
23 | const DEFAULT_CONNECTION_TIMEOUT = 5; |
||
24 | const DEFAULT_TIMEOUT = 30; |
||
25 | |||
26 | /** |
||
27 | * @var ClientInterface |
||
28 | */ |
||
29 | private $guzzleClient; |
||
30 | |||
31 | /** |
||
32 | * @var AuthEntity |
||
33 | */ |
||
34 | private $authEntity; |
||
35 | |||
36 | /** |
||
37 | * @param ClientInterface $guzzleClient |
||
38 | * @param AuthEntity $authEntity |
||
39 | */ |
||
40 | 7 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * @param string $url |
||
50 | * @return HttpClientResponseInterface |
||
51 | * @throws HttpConnectionException |
||
52 | */ |
||
53 | 3 | public function get($url) |
|
54 | { |
||
55 | 3 | $requestOptions = $this->getDefaultRequestOptions(); |
|
56 | |||
57 | try { |
||
58 | 3 | $response = $this->guzzleClient->request('GET', $url, $requestOptions); |
|
59 | 2 | return new HttpGuzzleResponse($response); |
|
60 | 1 | } catch (ClientException $exception) { // 400 level errors |
|
61 | throw new HttpConnectionException( |
||
62 | sprintf('Client error: Calling %s returned %d', $this->guzzleClient->getConfig('base_uri') . $url, $exception->getCode()), |
||
63 | $exception->getCode(), |
||
64 | $exception |
||
65 | ); |
||
66 | 1 | } catch (ServerException $exception) { // 500 level errors |
|
67 | throw new HttpConnectionException( |
||
68 | sprintf('Server error: Calling %s returned %d', $this->guzzleClient->getConfig('base_uri') . $url, $exception->getCode()), |
||
69 | $exception->getCode(), |
||
70 | $exception |
||
71 | ); |
||
72 | 1 | } catch (TooManyRedirectsException $exception) { // too many redirects to follow |
|
73 | throw new HttpConnectionException( |
||
74 | sprintf('Request to %s failed due to too many redirects', $this->guzzleClient->getConfig('base_uri') . $url), |
||
75 | HttpConnectionException::ERROR_CODE_TOO_MANY_REDIRECT_EXCEPTION, |
||
76 | $exception |
||
77 | ); |
||
78 | 1 | } catch (ConnectException $exception) { // networking error |
|
79 | throw new HttpConnectionException( |
||
80 | sprintf('Cannot connect to %s due to some networking error', $this->guzzleClient->getConfig('base_uri') . $url), |
||
81 | HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION, |
||
82 | $exception |
||
83 | ); |
||
84 | 1 | } catch (RequestException $exception) { // networking error (connection timeout, DNS errors, etc.) |
|
85 | throw new HttpConnectionException( |
||
86 | sprintf('Cannot connect to %s due to networking error', $this->guzzleClient->getConfig('base_uri') . $url), |
||
87 | HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION, |
||
88 | $exception |
||
89 | ); |
||
90 | 1 | } catch (\Exception $exception) { |
|
91 | 1 | throw new HttpConnectionException( |
|
92 | 1 | sprintf('Can\'t get response from "%s"', $this->guzzleClient->getConfig('base_uri') . $url), |
|
93 | 1 | HttpConnectionException::ERROR_CODE_UNKNOWN, |
|
94 | 1 | $exception |
|
95 | ); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $url |
||
101 | * @param mixed $postData |
||
102 | * @return HttpGuzzleResponse |
||
103 | */ |
||
104 | 2 | public function postJsonData($url, $postData) |
|
108 | |||
109 | /** |
||
110 | * @param string $url |
||
111 | * @param mixed $mPutData |
||
112 | * @return HttpGuzzleResponse |
||
113 | */ |
||
114 | public function putJsonData($url, $mPutData) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * @param string $url |
||
122 | * @return HttpGuzzleResponse |
||
123 | */ |
||
124 | 2 | public function delete($url) |
|
130 | |||
131 | /** |
||
132 | * @param string $method |
||
133 | * @param string $url |
||
134 | * @param mixed $data |
||
135 | * @return HttpGuzzleResponse |
||
136 | */ |
||
137 | 2 | private function sendJsonDataWithMethod($method, $url, $data) |
|
146 | |||
147 | /** |
||
148 | * Returns default options for the HTTP request. |
||
149 | * If an username and password is provided, auth |
||
150 | * header will be applied as well. |
||
151 | * |
||
152 | * @return array<string,integer|string> |
||
153 | */ |
||
154 | 7 | private function getDefaultRequestOptions() |
|
172 | } |
||
173 |