1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Message; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Helper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractRequest |
11
|
|
|
* |
12
|
|
|
* This abstract class implements RequestInterface and defines a basic |
13
|
|
|
* set of functions that all Watson Requests are intended to include. |
14
|
|
|
* |
15
|
|
|
* Requests of this class are usually created using the createRequest |
16
|
|
|
* function and then actioned using methods within this |
17
|
|
|
* class or a class that extends this class. |
18
|
|
|
* |
19
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Message |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractRequest implements RequestInterface |
22
|
|
|
{ |
23
|
|
|
const ENDPOINT = 'https://gateway.watsonplatform.net/visual-recognition-beta/api/v2/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The request parameters |
27
|
|
|
* |
28
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
29
|
|
|
*/ |
30
|
|
|
protected $parameters; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The request client. |
34
|
|
|
* |
35
|
|
|
* @var \GuzzleHttp\ClientInterface |
36
|
|
|
*/ |
37
|
|
|
protected $httpClient; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* An associated ResponseInterface. |
42
|
|
|
* |
43
|
|
|
* @var ResponseInterface |
44
|
|
|
*/ |
45
|
|
|
protected $response; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new Request |
49
|
|
|
* |
50
|
|
|
* @param ClientInterface $httpClient A Guzzle client to make API calls with |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ClientInterface $httpClient) |
53
|
|
|
{ |
54
|
|
|
$this->httpClient = $httpClient; |
55
|
|
|
$this->initialize(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the watson API endpoint |
60
|
|
|
* |
61
|
|
|
* @api |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getEndpoint() |
65
|
|
|
{ |
66
|
|
|
return static::ENDPOINT; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get full API URL |
71
|
|
|
* |
72
|
|
|
* @param String $path of API needed |
73
|
|
|
* @returns String of full URL |
74
|
|
|
*/ |
75
|
|
|
public function getApiUrl($path) |
76
|
|
|
{ |
77
|
|
|
return $this->getEndpoint() . $path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize the object with parameters. |
82
|
|
|
* |
83
|
|
|
* @param array $parameters An associative array of parameters |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
* @throws \RuntimeException |
87
|
|
|
*/ |
88
|
|
|
public function initialize(array $parameters = array()) |
89
|
|
|
{ |
90
|
|
|
if (null !== $this->response) { |
91
|
|
|
throw new \RuntimeException('Request cannot be modified after it has been sent!'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->parameters = new ParameterBag; |
95
|
|
|
|
96
|
|
|
Helper::initialize($this, $parameters); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get all parameters as an associative array. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getParameters() |
107
|
|
|
{ |
108
|
|
|
return $this->parameters->all(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a single parameter. |
113
|
|
|
* |
114
|
|
|
* @param string $key The parameter key |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
protected function getParameter($key) |
118
|
|
|
{ |
119
|
|
|
return $this->parameters->get($key); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set a single parameter |
124
|
|
|
* |
125
|
|
|
* @param string $key The parameter key |
126
|
|
|
* @param mixed $value The value to set |
127
|
|
|
* @return AbstractRequest Provides a fluent interface |
128
|
|
|
* @throws \RuntimeException if a request parameter is modified after the request has been sent. |
129
|
|
|
*/ |
130
|
|
|
protected function setParameter($key, $value) |
131
|
|
|
{ |
132
|
|
|
if (null !== $this->response) { |
133
|
|
|
throw new \RuntimeException('Request cannot be modified after it has been sent!'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->parameters->set($key, $value); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get Username |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getUsername() |
147
|
|
|
{ |
148
|
|
|
return $this->getParameter('username'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set Username |
153
|
|
|
* |
154
|
|
|
* @param string $value |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setUsername($value) |
158
|
|
|
{ |
159
|
|
|
$this->setParameter('username', $value); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get password |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getPassword() |
170
|
|
|
{ |
171
|
|
|
return $this->getParameter('password'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set Password |
176
|
|
|
* |
177
|
|
|
* @param string $value |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
|
|
public function setPassword($value) |
181
|
|
|
{ |
182
|
|
|
$this->setParameter('password', $value); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get Version |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getVersion() |
193
|
|
|
{ |
194
|
|
|
return $this->getParameter('version'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set version |
199
|
|
|
* |
200
|
|
|
* @param $value |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function setVersion($value) |
204
|
|
|
{ |
205
|
|
|
$this->setParameter('version', $value); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set verbose parameter |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getVerbose() |
216
|
|
|
{ |
217
|
|
|
return $this->getParameter('verbose'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $value |
222
|
|
|
* @return $this |
223
|
|
|
*/ |
224
|
|
|
public function setVerbose($value) |
225
|
|
|
{ |
226
|
|
|
$this->setParameter('verbose', $value); |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get image file. |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function getImagesFile() |
237
|
|
|
{ |
238
|
|
|
return $this->getParameter('images_file'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set Image file |
243
|
|
|
* |
244
|
|
|
* @param string $value |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function setImagesFile($value) |
248
|
|
|
{ |
249
|
|
|
$this->setParameter('images_file', $value); |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get Classifier Ids |
256
|
|
|
* |
257
|
|
|
* @return string[] |
258
|
|
|
*/ |
259
|
|
|
public function getClassifierIds() |
260
|
|
|
{ |
261
|
|
|
return $this->getParameter('classifier_ids'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set Classifier Ids |
266
|
|
|
* |
267
|
|
|
* @param string[] $value |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
|
|
public function setClassifierIds($value) |
271
|
|
|
{ |
272
|
|
|
$this->setParameter('classifier_ids', $value); |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get Classifier ID |
279
|
|
|
* |
280
|
|
|
* @return String |
281
|
|
|
*/ |
282
|
|
|
public function getClassifierId() |
283
|
|
|
{ |
284
|
|
|
return $this->getParameter('classifier_id'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Set Classifier ID |
289
|
|
|
* |
290
|
|
|
* @param String $value |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
public function setClassifierId($value) |
294
|
|
|
{ |
295
|
|
|
$this->setParameter('classifier_id', $value); |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Get positive examples zip file path |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function getPositiveExamples() |
306
|
|
|
{ |
307
|
|
|
return $this->getParameter('positive_examples'); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set file path of positive examples |
312
|
|
|
* |
313
|
|
|
* @param string $value |
314
|
|
|
* @return $this |
315
|
|
|
*/ |
316
|
|
|
public function setPositiveExamples($value) |
317
|
|
|
{ |
318
|
|
|
$this->setParameter('positive_examples', $value); |
319
|
|
|
|
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get negative examples zip file path |
325
|
|
|
* |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
|
|
public function getNegativeExamples() |
329
|
|
|
{ |
330
|
|
|
return $this->getParameter('negative_examples'); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Set file path of negative examples |
335
|
|
|
* |
336
|
|
|
* @param string $value |
337
|
|
|
* @return $this |
338
|
|
|
*/ |
339
|
|
|
public function setNegativeExamples($value) |
340
|
|
|
{ |
341
|
|
|
$this->setParameter('negative_examples', $value); |
342
|
|
|
|
343
|
|
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Get classifier name |
348
|
|
|
* |
349
|
|
|
* @return string |
350
|
|
|
*/ |
351
|
|
|
public function getName() |
352
|
|
|
{ |
353
|
|
|
return $this->getParameter('name'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Set classifier name |
358
|
|
|
* |
359
|
|
|
* @param string $value |
360
|
|
|
* @return $this |
361
|
|
|
*/ |
362
|
|
|
public function setName($value) |
363
|
|
|
{ |
364
|
|
|
$this->setParameter('name', $value); |
365
|
|
|
|
366
|
|
|
return $this; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get Default request data params |
371
|
|
|
* |
372
|
|
|
* @return array |
373
|
|
|
*/ |
374
|
|
|
public function getData() |
375
|
|
|
{ |
376
|
|
|
return [ |
377
|
|
|
'username' => $this->getUsername(), |
378
|
|
|
'password' => $this->getPassword(), |
379
|
|
|
'version' => $this->getVersion() |
380
|
|
|
]; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Send the request |
386
|
|
|
* |
387
|
|
|
* @return ResponseInterface |
388
|
|
|
*/ |
389
|
|
|
public function send() |
390
|
|
|
{ |
391
|
|
|
$data = $this->getData(); |
392
|
|
|
|
393
|
|
|
return $this->sendData($data); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Get the associated Response. |
398
|
|
|
* |
399
|
|
|
* @return ResponseInterface |
400
|
|
|
*/ |
401
|
|
|
public function getResponse() |
402
|
|
|
{ |
403
|
|
|
if (null === $this->response) { |
404
|
|
|
throw new \RuntimeException('You must call send() before accessing the Response!'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return $this->response; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|