1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Phraseanet SDK. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhraseanetSDK\Http; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponse; |
17
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
18
|
|
|
use GuzzleHttp\RequestOptions; |
19
|
|
|
// use GuzzleLogMiddleware\LogMiddleware; |
20
|
|
|
use PhraseanetSDK\ApplicationInterface; |
21
|
|
|
use PhraseanetSDK\Exception\BadResponseException; |
22
|
|
|
use PhraseanetSDK\Exception\InvalidArgumentException; |
23
|
|
|
use PhraseanetSDK\Exception\RuntimeException; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
// use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class GuzzleAdapter implements GuzzleAdapterInterface |
29
|
|
|
{ |
30
|
|
|
/** @var ClientInterface */ |
31
|
|
|
private $guzzleClient; |
32
|
|
|
private $extended = false; |
33
|
89 |
|
|
34
|
|
|
/** @var string |
35
|
89 |
|
* since client->getConfig() is deprecated, we keep here a copy of the endpoint passed on "create()" |
36
|
89 |
|
*/ |
37
|
|
|
private $baseUri = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
* since client->setUserAgent() is removed, we keep it here and pass it on exery "call()" |
42
|
|
|
*/ |
43
|
2 |
|
private $userAgent = ''; |
44
|
|
|
|
45
|
2 |
|
public function __construct(ClientInterface $guzzleClient) |
46
|
|
|
{ |
47
|
|
|
$this->guzzleClient = $guzzleClient; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* |
53
|
2 |
|
* @return ClientInterface |
54
|
|
|
*/ |
55
|
2 |
|
public function getGuzzle(): ClientInterface |
56
|
|
|
{ |
57
|
|
|
return $this->guzzleClient; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the baseUrl |
62
|
|
|
* |
63
|
1 |
|
* @param string $baseUrl |
64
|
|
|
* @return GuzzleAdapter |
65
|
1 |
|
*/ |
66
|
1 |
|
public function setBaseUrl(string $baseUrl): GuzzleAdapterInterface |
67
|
|
|
{ |
68
|
|
|
$this->baseUri = $baseUrl; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the client base URL |
75
|
|
|
* |
76
|
6 |
|
* @return string |
77
|
1 |
|
*/ |
78
|
|
|
public function getBaseUrl(): string |
79
|
6 |
|
{ |
80
|
|
|
// return $this->guzzleClient->getBaseUrl(); // removed |
81
|
|
|
return $this->baseUri; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the user agent |
86
|
|
|
* |
87
|
|
|
* @param string $userAgent |
88
|
|
|
* @return GuzzleAdapter |
89
|
|
|
*/ |
90
|
|
|
public function setUserAgent(string $userAgent): GuzzleAdapterInterface |
91
|
|
|
{ |
92
|
|
|
// $this->guzzleClient->setUserAgent($userAgent); // removed |
93
|
|
|
$this->userAgent = $userAgent; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getUserAgent(): string |
102
|
|
|
{ |
103
|
|
|
return ($this->userAgent); |
104
|
82 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets extended mode |
108
|
|
|
* |
109
|
|
|
* Extended mode fetch more data (status, meta, subdefs) in one request |
110
|
|
|
* for a record |
111
|
|
|
* |
112
|
|
|
* @param bool $extended |
113
|
|
|
* @return GuzzleAdapter |
114
|
82 |
|
*/ |
115
|
82 |
|
public function setExtended(bool $extended): GuzzleAdapterInterface |
116
|
|
|
{ |
117
|
82 |
|
$this->extended = $extended; |
118
|
82 |
|
|
119
|
81 |
|
return $this; |
120
|
82 |
|
} |
121
|
2 |
|
|
122
|
17 |
|
/** |
123
|
15 |
|
* @return boolean |
124
|
2 |
|
*/ |
125
|
1 |
|
public function isExtended(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->extended; |
128
|
63 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Performs an HTTP request, returns the body response |
132
|
|
|
* |
133
|
|
|
* @param string $method The method |
134
|
|
|
* @param string $path The path to query |
135
|
|
|
* @param array $query An array of query parameters |
136
|
|
|
* @param array $postFields An array of post fields |
137
|
|
|
* @param array $files An array of post files |
138
|
|
|
* @param array $headers An array of request headers |
139
|
6 |
|
* |
140
|
|
|
* @return string The response body |
141
|
|
|
* |
142
|
|
|
* @throws InvalidArgumentException |
143
|
6 |
|
* @throws BadResponseException |
144
|
|
|
* @throws RuntimeException |
145
|
|
|
*/ |
146
|
|
|
public function call(string $method, string $path, array $query = [], array $postFields = [], array $files = [], array $headers = []): string |
147
|
6 |
|
{ |
148
|
|
|
try { |
149
|
|
|
$acceptHeader = [ |
150
|
6 |
|
'Accept' => $this->extended ? 'application/vnd.phraseanet.record-extended+json' : 'application/json' |
151
|
|
|
]; |
152
|
6 |
|
|
153
|
6 |
|
$options = [ |
154
|
6 |
|
RequestOptions::QUERY => $query |
155
|
|
|
]; |
156
|
6 |
|
|
157
|
6 |
|
// files -- receiving files has no usage found in the code, so format of $files is unknown, so... not implmented |
158
|
6 |
|
if (count($files) > 0) { |
159
|
6 |
|
throw new InvalidArgumentException('request with "files" is not implemented'); |
160
|
|
|
} |
161
|
6 |
|
|
162
|
|
|
// postFields |
163
|
6 |
|
if (count($postFields) > 0) { |
164
|
3 |
|
if ($method !== 'POST') { |
165
|
6 |
|
throw new InvalidArgumentException('postFields are only allowed with "POST" method'); |
166
|
|
|
} |
167
|
6 |
|
if (count($files) > 0) { |
168
|
|
|
// this will not happen while "files" is not implemented |
169
|
|
|
throw new InvalidArgumentException('request can\'t contain both postFields and files'); |
170
|
82 |
|
} |
171
|
|
|
$options[RequestOptions::FORM_PARAMS] = $postFields; |
172
|
82 |
|
} |
173
|
19 |
|
|
174
|
82 |
|
// headers |
175
|
|
|
$h = array_merge($acceptHeader, $headers); |
176
|
82 |
|
if ($this->userAgent !== '' && !array_key_exists('User-Agent', $h)) { |
177
|
13 |
|
// use the defaut user-agent if none is provided in headers |
178
|
|
|
$h['User-Agent'] = sprintf('%s version %s', ApplicationInterface::USER_AGENT, ApplicationInterface::VERSION); |
179
|
|
|
} |
180
|
|
|
if (count($h) > 0) { |
181
|
|
|
$options[RequestOptions::HEADERS] = $h; |
182
|
|
|
} |
183
|
|
|
|
184
|
14 |
|
$response = $this->guzzleClient->request($method, $path, $options); |
185
|
10 |
|
|
186
|
13 |
|
// $request = new Request($method, $path, array_merge($acceptHeader, $headers)); |
187
|
61 |
|
|
188
|
3 |
|
// $this->addRequestParameters($request, $query, $postFields, $files); |
189
|
13 |
|
// $response = $request->send(); |
190
|
82 |
|
} |
191
|
1 |
|
catch (GuzzleBadResponse $e) { |
192
|
|
|
throw BadResponseException::fromGuzzleResponse($e); |
193
|
81 |
|
} |
194
|
|
|
catch (GuzzleException $e) { |
195
|
|
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $response->getBody(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Creates a new instance of GuzzleAdapter |
203
|
|
|
* |
204
|
|
|
* @param string $endpoint |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
|
|
public static function create(string $endpoint): GuzzleAdapterInterface |
208
|
|
|
{ |
209
|
|
|
if (!is_string($endpoint)) { |
210
|
|
|
throw new InvalidArgumentException('API url endpoint must be a valid url'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$versionMountPoint = ApplicationInterface::API_MOUNT_POINT; |
214
|
|
|
|
215
|
|
|
// test if url already end with API_MOUNT_POINT |
216
|
|
|
$mountPoint = substr(trim($endpoint, '/'), -strlen($versionMountPoint)); |
217
|
|
|
|
218
|
|
|
if ($versionMountPoint !== $mountPoint) { |
219
|
|
|
$endpoint = sprintf('%s%s/', trim($endpoint, '/'), $versionMountPoint); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$guzzleClient = new GuzzleClient([ |
223
|
|
|
'base_uri' => $endpoint, |
224
|
|
|
RequestOptions::HEADERS => [ |
225
|
|
|
'User-Agent' => sprintf('%s version %s', ApplicationInterface::USER_AGENT, ApplicationInterface::VERSION) |
226
|
|
|
] |
227
|
|
|
]); |
228
|
|
|
|
229
|
|
|
// $guzzleClient->setUserAgent(sprintf( |
230
|
|
|
// '%s version %s', |
231
|
|
|
// ApplicationInterface::USER_AGENT, |
232
|
|
|
// ApplicationInterface::VERSION |
233
|
|
|
// )); |
234
|
|
|
|
235
|
|
|
/* todo : for now, no plugins |
236
|
|
|
$logger = new Logger(); //A new PSR-3 Logger like Monolog |
237
|
|
|
$stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it. |
238
|
|
|
$stack->push(new LogMiddleware($logger)); |
239
|
|
|
foreach ($plugins as $plugin) { |
240
|
|
|
$guzzleClient->addSubscriber($plugin); |
241
|
|
|
} |
242
|
|
|
*/ |
243
|
|
|
|
244
|
|
|
return (new static($guzzleClient))->setBaseUrl($endpoint); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/* |
248
|
|
|
private function addRequestParameters(RequestInterface $request, $query, $postFields, $files) |
249
|
|
|
{ |
250
|
|
|
foreach ($query as $name => $value) { |
251
|
|
|
$request->getQuery()->add($name, $value); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// todo : EntityEnclosingRequestInterface ??? |
255
|
|
|
if ($request instanceof EntityEnclosingRequestInterface) { |
256
|
|
|
if ($request->getHeader('Content-Type') == 'application/json') { |
257
|
|
|
$request->getHeaders()->offsetUnset('Content-Type'); |
258
|
|
|
$request->setBody(json_encode($postFields)); |
259
|
|
|
|
260
|
|
|
return; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
foreach ($postFields as $name => $value) { |
264
|
|
|
$request->getPostFields()->add($name, $value); |
265
|
|
|
} |
266
|
|
|
foreach ($files as $name => $filename) { |
267
|
|
|
$request->addPostFile($name, $filename); |
268
|
|
|
} |
269
|
|
|
} elseif (0 < count($postFields)) { |
270
|
|
|
throw new InvalidArgumentException('Can not add post fields to GET request'); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
*/ |
274
|
|
|
} |
275
|
|
|
|