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