1 | <?php |
||
8 | abstract class BaseClient |
||
9 | { |
||
10 | const RESPONSE_OK = 200; |
||
11 | |||
12 | const RESPONSE_OK_NO_BODY = 204; |
||
13 | |||
14 | /** |
||
15 | * @var \GuzzleHttp\Client |
||
16 | */ |
||
17 | protected $client; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $version; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $partner; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $accessToken; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $venture; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $uniqueId; |
||
43 | |||
44 | /** |
||
45 | * @var \Psr\Log\LoggerInterface |
||
46 | */ |
||
47 | protected $logger; |
||
48 | |||
49 | /** |
||
50 | * @param \Iris\Interfaces\Configuration $configuration |
||
51 | * @param \Psr\Log\LoggerInterface $logger |
||
52 | */ |
||
53 | 32 | public function __construct( |
|
64 | |||
65 | /** |
||
66 | * @param \Iris\Interfaces\Connection $connection |
||
67 | */ |
||
68 | 32 | protected function loadClient(\Iris\Interfaces\Connection $connection) |
|
75 | |||
76 | /** |
||
77 | * @param string $baseUrl |
||
78 | */ |
||
79 | protected function loadGuzzle($baseUrl) |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | 27 | public function getUniqueId() |
|
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | 26 | public function getVersion() |
|
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | 20 | public function getVenture() |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 7 | public function getPartner() |
|
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | 20 | public function getAccessToken() |
|
131 | |||
132 | /** |
||
133 | * @return \GuzzleHttp\Client |
||
134 | */ |
||
135 | public function getClient() |
||
139 | |||
140 | /**TODO generalize this method to be used by Bob as venture |
||
141 | * Builds path to call api. |
||
142 | * @param string $callPath Suffix path, the action. |
||
143 | * @param string $venture Venture code. |
||
144 | * @return string |
||
145 | */ |
||
146 | 6 | protected function buildRequestUrl($callPath, $venture) |
|
156 | |||
157 | /** |
||
158 | * @param \Exception $exception |
||
159 | * @param string $message |
||
160 | * @param array $context |
||
161 | * @throw \Iris\Exceptions\RetryMessage |
||
162 | */ |
||
163 | 2 | public function throwException(\Exception $exception, $message, array $context = []) |
|
180 | |||
181 | /** |
||
182 | * @param string $url |
||
183 | * @param $body |
||
184 | * @return \GuzzleHttp\Message\Response |
||
185 | */ |
||
186 | 2 | protected function create($url, $body) |
|
198 | |||
199 | /** |
||
200 | * @param string $url |
||
201 | * @param $body |
||
202 | * @return \GuzzleHttp\Message\Response |
||
203 | */ |
||
204 | 17 | protected function update($url, $body) |
|
216 | |||
217 | /** |
||
218 | * @param string $method |
||
219 | * @param string $url |
||
220 | * @param array $options |
||
221 | * @return \GuzzleHttp\Message\Response |
||
222 | */ |
||
223 | 25 | protected function doRequest($method, $url, $options = array()) |
|
224 | { |
||
225 | 25 | $message = sprintf( |
|
226 | 25 | '%s REQUEST TO: %s -- OPTIONS %s', |
|
227 | 25 | $this->getUniqueId(), |
|
228 | 25 | $url, |
|
229 | 25 | json_encode($options) |
|
230 | 25 | ); |
|
231 | |||
232 | 25 | $this->logger->info($message); |
|
233 | |||
234 | 25 | $response = $this->getClient()->$method($url, $options); |
|
235 | |||
236 | 14 | $this->logResponse($response); |
|
237 | |||
238 | 14 | if (!$this->isResponseOk($response)) { |
|
239 | 3 | throw new \Iris\Exceptions\RetryMessage($response->getBody(), $response->getStatusCode()); |
|
240 | } |
||
241 | |||
242 | 11 | return $response; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param \GuzzleHttp\Message\ResponseInterface $response |
||
247 | * @return void |
||
248 | */ |
||
249 | 15 | protected function logResponse(\GuzzleHttp\Message\ResponseInterface $response) |
|
261 | |||
262 | /** |
||
263 | * @param \GuzzleHttp\Message\ResponseInterface $response |
||
264 | * @return bool |
||
265 | */ |
||
266 | 14 | protected function isResponseOk(\GuzzleHttp\Message\ResponseInterface $response) |
|
270 | } |
||
271 |