|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Iris\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
|
|
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
|
31 |
|
public function __construct( |
|
54
|
|
|
\Iris\Interfaces\Configuration $configuration, |
|
55
|
|
|
\Psr\Log\LoggerInterface $logger |
|
56
|
|
|
) { |
|
57
|
31 |
|
$this->partner = $configuration->getVenture(); |
|
58
|
31 |
|
$this->venture = $configuration->getVenture(); |
|
59
|
31 |
|
$this->uniqueId = uniqid(); |
|
60
|
31 |
|
$this->logger = $logger; |
|
61
|
|
|
|
|
62
|
31 |
|
$this->loadClient($configuration->getConnection()); |
|
63
|
31 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \Iris\Interfaces\Connection $connection |
|
67
|
|
|
*/ |
|
68
|
31 |
|
protected function loadClient(\Iris\Interfaces\Connection $connection) |
|
69
|
|
|
{ |
|
70
|
31 |
|
$this->version = $connection->getVersion(); |
|
71
|
31 |
|
$this->accessToken = $connection->getAccessToken(); |
|
72
|
|
|
|
|
73
|
31 |
|
$this->loadGuzzle($connection->getBaseUrl()); |
|
74
|
31 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $baseUrl |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function loadGuzzle($baseUrl) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->client = new HttpClient(array( |
|
82
|
|
|
'base_url' => $baseUrl, |
|
83
|
|
|
'defaults' => array( |
|
84
|
|
|
'headers' => array( |
|
85
|
|
|
'Accept' => 'application/json', |
|
86
|
|
|
'Content-Type' => 'application/json' |
|
87
|
|
|
) |
|
88
|
|
|
) |
|
89
|
|
|
)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
26 |
|
public function getUniqueId() |
|
96
|
|
|
{ |
|
97
|
26 |
|
return $this->uniqueId; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
25 |
|
public function getVersion() |
|
104
|
|
|
{ |
|
105
|
25 |
|
return $this->version; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
17 |
|
public function getVenture() |
|
112
|
|
|
{ |
|
113
|
17 |
|
return $this->venture; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public function getPartner() |
|
120
|
|
|
{ |
|
121
|
9 |
|
return $this->partner; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
17 |
|
public function getAccessToken() |
|
128
|
|
|
{ |
|
129
|
17 |
|
return $this->accessToken; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return \GuzzleHttp\Client |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getClient() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->client; |
|
138
|
|
|
} |
|
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
|
8 |
|
protected function buildRequestUrl($callPath, $venture) |
|
147
|
|
|
{ |
|
148
|
8 |
|
$requestUrl = sprintf( |
|
149
|
8 |
|
'api/%s/gfg/%s/%s', |
|
150
|
8 |
|
$this->getVersion(), |
|
151
|
8 |
|
$this->getPartner(), |
|
152
|
|
|
$venture |
|
153
|
8 |
|
); |
|
154
|
8 |
|
return $requestUrl . $callPath; |
|
155
|
|
|
} |
|
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 = []) |
|
164
|
|
|
{ |
|
165
|
2 |
|
$message = $message . ' ( Exception message: ' . $exception->getMessage() . ')'; |
|
166
|
2 |
|
$this->logger->error($message, $context); |
|
167
|
|
|
|
|
168
|
2 |
|
$retryMessage = 'Try Again'; |
|
169
|
2 |
|
$code = 500; |
|
170
|
|
|
|
|
171
|
2 |
|
if ($exception instanceof RequestException) { |
|
172
|
1 |
|
$this->logResponse($exception->getResponse()); |
|
173
|
|
|
|
|
174
|
1 |
|
$retryMessage = $exception->getResponse()->getBody(); |
|
175
|
1 |
|
$code = $exception->getResponse()->getStatusCode(); |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
2 |
|
throw new \Iris\Exceptions\RetryMessage($retryMessage, $code); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $url |
|
183
|
|
|
* @param $body |
|
184
|
|
|
* @return \GuzzleHttp\Message\Response |
|
185
|
|
|
*/ |
|
186
|
2 |
|
protected function create($url, $body) |
|
187
|
|
|
{ |
|
188
|
2 |
|
return $this->doRequest( |
|
189
|
2 |
|
'post', |
|
190
|
2 |
|
$url, |
|
191
|
|
|
array( |
|
192
|
2 |
|
'body' => json_encode($body), |
|
193
|
2 |
|
'headers' => ['venture-access-token' => $this->getAccessToken(), 'venture-key' => $this->getVenture()], |
|
194
|
|
|
'exceptions' => false |
|
195
|
2 |
|
) |
|
196
|
2 |
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param string $url |
|
201
|
|
|
* @param $body |
|
202
|
|
|
* @return \GuzzleHttp\Message\Response |
|
203
|
|
|
*/ |
|
204
|
14 |
|
protected function update($url, $body) |
|
205
|
|
|
{ |
|
206
|
14 |
|
return $this->doRequest( |
|
207
|
14 |
|
'put', |
|
208
|
14 |
|
$url, |
|
209
|
|
|
array( |
|
210
|
14 |
|
'body' => json_encode($body), |
|
211
|
14 |
|
'headers' => ['venture-access-token' => $this->getAccessToken(), 'venture-key' => $this->getVenture()], |
|
212
|
|
|
'exceptions' => false |
|
213
|
14 |
|
) |
|
214
|
14 |
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param string $method |
|
219
|
|
|
* @param string $url |
|
220
|
|
|
* @param array $options |
|
221
|
|
|
* @return \GuzzleHttp\Message\Response |
|
222
|
|
|
*/ |
|
223
|
24 |
|
protected function doRequest($method, $url, $options = array()) |
|
224
|
|
|
{ |
|
225
|
24 |
|
$message = sprintf( |
|
226
|
24 |
|
'%s REQUEST TO: %s -- OPTIONS %s', |
|
227
|
24 |
|
$this->getUniqueId(), |
|
228
|
24 |
|
$url, |
|
229
|
24 |
|
json_encode($options) |
|
230
|
24 |
|
); |
|
231
|
|
|
|
|
232
|
24 |
|
$this->logger->info($message); |
|
233
|
|
|
|
|
234
|
24 |
|
$response = $this->getClient()->$method($url, $options); |
|
235
|
|
|
|
|
236
|
12 |
|
$this->logResponse($response); |
|
237
|
|
|
|
|
238
|
12 |
|
if (!$this->isResponseOk($response)) { |
|
239
|
|
|
throw new \Iris\Exceptions\RetryMessage($response->getBody(), $response->getStatusCode()); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
12 |
|
return $response; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
247
|
|
|
* @return void |
|
248
|
|
|
*/ |
|
249
|
13 |
|
protected function logResponse(\GuzzleHttp\Message\ResponseInterface $response) |
|
250
|
|
|
{ |
|
251
|
13 |
|
$message = sprintf( |
|
252
|
13 |
|
'%s RESPONSE: %s -- HTTP CODE %s -- BODY %s', |
|
253
|
13 |
|
$this->getUniqueId(), |
|
254
|
13 |
|
$response->getEffectiveUrl(), |
|
255
|
13 |
|
$response->getStatusCode(), |
|
256
|
13 |
|
$response->getBody() |
|
257
|
13 |
|
); |
|
258
|
|
|
|
|
259
|
13 |
|
$this->logger->info($message); |
|
260
|
13 |
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
264
|
|
|
* @return bool |
|
265
|
|
|
*/ |
|
266
|
12 |
|
protected function isResponseOk(\GuzzleHttp\Message\ResponseInterface $response) |
|
267
|
|
|
{ |
|
268
|
12 |
|
return in_array($response->getStatusCode(), array(static::RESPONSE_OK, static::RESPONSE_OK_NO_BODY)); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|