1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifyRequest; |
6
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\CreateClassifierRequest; |
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\DeleteClassifierRequest; |
8
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifierRequest; |
9
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifiersRequest; |
10
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\RequestInterface; |
11
|
|
|
use GuzzleHttp\ClientInterface as HttpClientInterface; |
12
|
|
|
use GuzzleHttp\Client as HttpClient; |
13
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the primary library class which handles request to the IBM Watson Visual Recognition Service |
17
|
|
|
* |
18
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition |
19
|
|
|
* @author Tom Robertshaw <[email protected]> |
20
|
|
|
* @api |
21
|
|
|
*/ |
22
|
|
|
class Client implements ClientInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Request parameters |
27
|
|
|
* |
28
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
29
|
|
|
*/ |
30
|
|
|
protected $parameters; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HTTP Client |
34
|
|
|
* @var \GuzzleHttp\ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Instantiate Visual Recognition client with API credentials |
40
|
|
|
* |
41
|
|
|
* @api |
42
|
|
|
* @param HttpClientInterface|null $httpClient |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
30 |
|
public function __construct(HttpClientInterface $httpClient = null) |
46
|
|
|
{ |
47
|
30 |
|
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); |
48
|
30 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the global default HTTP client. |
52
|
|
|
* |
53
|
|
|
* @return HttpClient |
54
|
|
|
*/ |
55
|
18 |
|
protected function getDefaultHttpClient() |
56
|
|
|
{ |
57
|
18 |
|
return new HttpClient( |
58
|
|
|
array( |
59
|
18 |
|
'curl.options' => array(CURLOPT_CONNECTTIMEOUT => 60), |
60
|
|
|
) |
61
|
18 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Initialize this client with default parameters |
66
|
|
|
* |
67
|
|
|
* @param array $parameters |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
29 |
|
public function initialize(array $parameters = array()) |
71
|
|
|
{ |
72
|
29 |
|
$this->parameters = new ParameterBag; |
73
|
|
|
|
74
|
|
|
// set default parameters |
75
|
29 |
|
foreach ($this->getDefaultParameters() as $key => $value) { |
76
|
29 |
|
$this->parameters->set($key, $value); |
77
|
29 |
|
} |
78
|
|
|
|
79
|
29 |
|
Helper::initialize($this, $parameters); |
80
|
|
|
|
81
|
29 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create new request object and initialize. |
86
|
|
|
* |
87
|
|
|
* @param $class |
88
|
|
|
* @param array $parameters |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
25 |
|
protected function createRequest($class, array $parameters) |
92
|
|
|
{ |
93
|
|
|
/** @var RequestInterface $obj */ |
94
|
25 |
|
$obj = new $class($this->httpClient); |
95
|
|
|
|
96
|
25 |
|
return $obj->initialize(array_replace($this->getParameters(), $parameters)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Default & required parameters for requests |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
29 |
|
public function getDefaultParameters() |
104
|
|
|
{ |
105
|
|
|
return array( |
106
|
29 |
|
'username' => '', |
107
|
29 |
|
'password' => '', |
108
|
|
|
'version' => '2015-12-02' |
109
|
29 |
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get Parameters |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
25 |
|
public function getParameters() |
118
|
|
|
{ |
119
|
25 |
|
return $this->parameters->all(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get Username |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
2 |
|
public function getUsername() |
128
|
|
|
{ |
129
|
2 |
|
return $this->parameters->get('username'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set Username |
134
|
|
|
* |
135
|
|
|
* @param $value |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
29 |
|
public function setUsername($value) |
139
|
|
|
{ |
140
|
29 |
|
$this->parameters->set('username', $value); |
141
|
|
|
|
142
|
29 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get password |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
2 |
|
public function getPassword() |
151
|
|
|
{ |
152
|
2 |
|
return $this->parameters->get('password'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set Password |
157
|
|
|
* |
158
|
|
|
* @param $value |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
29 |
|
public function setPassword($value) |
162
|
|
|
{ |
163
|
29 |
|
$this->parameters->set('password', $value); |
164
|
|
|
|
165
|
29 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get Version |
170
|
|
|
* |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
1 |
|
public function getVersion() |
174
|
|
|
{ |
175
|
1 |
|
return $this->parameters->get('version'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Set version |
180
|
|
|
* |
181
|
|
|
* @param $value |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
18 |
|
public function setVersion($value) |
185
|
|
|
{ |
186
|
18 |
|
$this->parameters->set('version', $value); |
187
|
|
|
|
188
|
18 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get list of available classifiers |
193
|
|
|
* |
194
|
|
|
* @api |
195
|
|
|
* @param array $parameters |
196
|
|
|
* @return GetClassifiersRequest |
197
|
|
|
* @throws \Exception |
198
|
|
|
*/ |
199
|
9 |
|
public function getClassifiers(array $parameters = []) |
200
|
|
|
{ |
201
|
9 |
|
return $this->createRequest(GetClassifiersRequest::class, $parameters); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get detail on individual classifier |
206
|
|
|
* |
207
|
|
|
* @param array $parameters |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
4 |
|
public function getClassifier(array $parameters = []) |
211
|
|
|
{ |
212
|
4 |
|
return $this->createRequest(GetClassifierRequest::class, $parameters); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Classify image |
217
|
|
|
* |
218
|
|
|
* @param array $parameters |
219
|
|
|
* @return ClassifyRequest |
220
|
|
|
*/ |
221
|
4 |
|
public function classify(array $parameters = []) |
222
|
|
|
{ |
223
|
4 |
|
return $this->createRequest(ClassifyRequest::class, $parameters); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Train a new classifier |
228
|
|
|
* |
229
|
|
|
* @param array $parameters |
230
|
|
|
* @return mixed |
231
|
|
|
*/ |
232
|
3 |
|
public function createClassifier(array $parameters = []) |
233
|
|
|
{ |
234
|
3 |
|
return $this->createRequest(CreateClassifierRequest::class, $parameters); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Delete a classifier |
239
|
|
|
* |
240
|
|
|
* @param array $parameters |
241
|
|
|
* @return mixed |
242
|
|
|
*/ |
243
|
5 |
|
public function deleteClassifier(array $parameters = []) |
244
|
|
|
{ |
245
|
5 |
|
return $this->createRequest(DeleteClassifierRequest::class, $parameters); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|