1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IBM\Watson\Common\HttpClient; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin; |
6
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
7
|
|
|
use Http\Client\Common\PluginClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
10
|
|
|
use http\Exception\RuntimeException; |
11
|
|
|
use Http\Message\Authentication\BasicAuth; |
12
|
|
|
use Http\Message\UriFactory; |
13
|
|
|
use IBM\Watson\Common\Exception\Api\InvalidArgumentException; |
14
|
|
|
use IBM\Watson\Common\Hydrator\HydratorInterface; |
15
|
|
|
use IBM\Watson\Common\Util\DiscoveryTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* HttpClient builder |
19
|
|
|
*/ |
20
|
|
|
final class Builder |
21
|
|
|
{ |
22
|
|
|
use DiscoveryTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Http\Client\HttpClient|null |
26
|
|
|
*/ |
27
|
|
|
private $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Http\Message\UriFactory|null |
31
|
|
|
*/ |
32
|
|
|
private $uriFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $plugins = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $username; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $password; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $host; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $version; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Http\Client\HttpClient|null $httpClient |
61
|
|
|
* @param \Http\Message\UriFactory|null $uriFactory |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
HttpClient $httpClient = null, |
65
|
|
|
UriFactory $uriFactory = null |
66
|
|
|
) { |
67
|
|
|
$this->httpClient = $httpClient ?: $this->discoverHttpClient(); |
68
|
|
|
$this->uriFactory = $uriFactory ?: $this->discoverUriFactory(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a configured HTTP client |
73
|
|
|
* |
74
|
|
|
* @return \Http\Client\Common\PluginClient |
75
|
|
|
* @throws \Exception |
76
|
|
|
*/ |
77
|
|
|
public function createConfiguredClient() |
78
|
|
|
{ |
79
|
|
|
$this->addHostPlugin(); |
80
|
|
|
$this->addPathPlugin(); |
81
|
|
|
$this->addVersionQueryPlugin(); |
82
|
|
|
$this->addAuthenticationPlugin(); |
83
|
|
|
|
84
|
|
|
return new PluginClient($this->httpClient, $this->plugins); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add username to client |
89
|
|
|
* |
90
|
|
|
* @param string $username |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function withUsername($username) |
95
|
|
|
{ |
96
|
|
|
$this->username = $username; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add password to client |
103
|
|
|
* |
104
|
|
|
* @param string $password |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function withPassword($password) |
109
|
|
|
{ |
110
|
|
|
$this->password = $password; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add username and password to client |
117
|
|
|
* |
118
|
|
|
* @param string $username |
119
|
|
|
* @param string $password |
120
|
|
|
* |
121
|
|
|
* @return \IBM\Watson\Common\HttpClient\Builder |
122
|
|
|
*/ |
123
|
|
|
public function withCredentials($username, $password) |
124
|
|
|
{ |
125
|
|
|
return $this |
126
|
|
|
->withUsername($username) |
127
|
|
|
->withPassword($password); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $host |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function withHost($host) |
136
|
|
|
{ |
137
|
|
|
$this->host = $host; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $version |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function withVersion($version) |
148
|
|
|
{ |
149
|
|
|
$this->version = $version; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param \Http\Client\Common\Plugin ...$plugins |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function addPlugin(Plugin ...$plugins) |
160
|
|
|
{ |
161
|
|
|
foreach ($plugins as $plugin) { |
162
|
|
|
$this->plugins[] = $plugin; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return \Psr\Http\Message\UriInterface |
170
|
|
|
*/ |
171
|
|
|
private function getHostUri() |
172
|
|
|
{ |
173
|
|
|
return $this->uriFactory->createUri($this->host); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $version |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
private function validateVersion($version) |
182
|
|
|
{ |
183
|
|
|
$date = \DateTime::createFromFormat('Y-m-d', $version); |
184
|
|
|
|
185
|
|
|
return $date && $date->format('Y-m-d') === $version; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
private function addHostPlugin() |
192
|
|
|
{ |
193
|
|
|
if (null !== $this->host) { |
194
|
|
|
$this->plugins[] = new Plugin\AddHostPlugin($this->getHostUri()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
private function addAuthenticationPlugin() |
205
|
|
|
{ |
206
|
|
|
if (null !== $this->username && null !== $this->password) { |
207
|
|
|
$this->plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
private function addVersionQueryPlugin() |
217
|
|
|
{ |
218
|
|
|
if (null === $this->version) { |
219
|
|
|
$this->version = date('Y-m-d'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (!$this->validateVersion($this->version)) { |
223
|
|
|
throw new InvalidArgumentException('Version must be a date in the Y-m-d format'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->plugins[] = new Plugin\QueryDefaultsPlugin([ |
227
|
|
|
'version' => $this->version |
228
|
|
|
]); |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
|
|
private function addPathPlugin() |
237
|
|
|
{ |
238
|
|
|
$this->plugins[] = new Plugin\AddPathPlugin($this->uriFactory->createUri('/tone-analyzer/api/v3')); |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: