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\Plugin\HeaderDefaultsPlugin; |
8
|
|
|
use Http\Client\Common\Plugin\QueryDefaultsPlugin; |
9
|
|
|
use Http\Client\Common\PluginClient; |
10
|
|
|
use Http\Client\HttpClient; |
11
|
|
|
use Http\Discovery\HttpClientDiscovery; |
12
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
13
|
|
|
use Http\Message\Authentication\BasicAuth; |
14
|
|
|
use Http\Message\UriFactory; |
15
|
|
|
|
16
|
|
|
final class Builder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $endpoint; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $username; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $password; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $apiKey; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $version; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Http\Client\HttpClient |
45
|
|
|
*/ |
46
|
|
|
private $httpClient; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Http\Message\UriFactory |
50
|
|
|
*/ |
51
|
|
|
private $uriFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $pluginsAppend = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private $pluginsPrepend = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
private $learningOptOut; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Builder constructor. |
70
|
|
|
* |
71
|
|
|
* @param \Http\Client\HttpClient|null $httpClient |
72
|
|
|
* @param \Http\Message\UriFactory|null $uriFactory |
73
|
|
|
* |
74
|
|
|
* @throws \Http\Discovery\Exception\NotFoundException |
75
|
|
|
*/ |
76
|
|
|
public function __construct( |
77
|
|
|
HttpClient $httpClient = null, |
78
|
|
|
UriFactory $uriFactory = null |
79
|
|
|
) { |
80
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
81
|
|
|
$this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create HTTP client with specified plugins |
86
|
|
|
* |
87
|
|
|
* @return \Http\Client\Common\PluginClient |
88
|
|
|
* |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
* @throws \RuntimeException |
91
|
|
|
*/ |
92
|
|
|
public function createConfiguredClient() |
93
|
|
|
{ |
94
|
|
|
$plugins = $this->pluginsPrepend; |
95
|
|
|
|
96
|
|
|
$plugins[] = new HeaderDefaultsPlugin([ |
97
|
|
|
'User-Agent' => 'adam-paterson/ibm-watson-sdk (https://github.com/adam-paterson/ibm-watson-sdk)' |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
if (null !== $this->endpoint) { |
101
|
|
|
$plugins[] = new Plugin\BaseUriPlugin($this->uriFactory->createUri($this->endpoint)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (null !== $this->username && null !== $this->password) { |
105
|
|
|
$plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!$this->learningOptOut) { |
109
|
|
|
$plugins[] = new HeaderDefaultsPlugin([ |
110
|
|
|
'X-Watson-Learning-Opt-Out' => true |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->apiKey) { |
115
|
|
|
$plugins[] = new QueryDefaultsPlugin(['api_key' => $this->apiKey]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$version = $this->version ?: \date('Y-m-d'); |
119
|
|
|
$plugins[] = new QueryDefaultsPlugin(['version' => $version]); |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->pluginsAppend)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Add endpoint to client |
127
|
|
|
* |
128
|
|
|
* @param string $endpoint |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function withEndpoint($endpoint) |
133
|
|
|
{ |
134
|
|
|
$this->endpoint = $endpoint; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add username to client |
141
|
|
|
* |
142
|
|
|
* @param string $username |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function withUsername($username) |
147
|
|
|
{ |
148
|
|
|
$this->username = $username; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add password to client |
155
|
|
|
* |
156
|
|
|
* @param string $password |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function withPassword($password) |
161
|
|
|
{ |
162
|
|
|
$this->password = $password; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function withApiKey($apiKey) |
168
|
|
|
{ |
169
|
|
|
$this->apiKey = $apiKey; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function withVersion($version) |
175
|
|
|
{ |
176
|
|
|
$this->version = $version; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Add username and password to client |
183
|
|
|
* |
184
|
|
|
* @param string $username |
185
|
|
|
* @param string $password |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function withCredentials($username, $password) |
190
|
|
|
{ |
191
|
|
|
return $this |
192
|
|
|
->withUsername($username) |
193
|
|
|
->withPassword($password); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Opt out of Watson request logging for all requests made with this client |
198
|
|
|
* |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function withLearningOptOut() |
202
|
|
|
{ |
203
|
|
|
$this->learningOptOut = false; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Add plugins to client after it has been configured |
210
|
|
|
* |
211
|
|
|
* @param \Http\Client\Common\Plugin|\Http\Client\Common\Plugin[] ...$plugins |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function appendPlugin(Plugin ...$plugins) |
216
|
|
|
{ |
217
|
|
|
foreach ($plugins as $plugin) { |
218
|
|
|
$this->pluginsAppend[] = $plugin; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Add plugins to client before it has been configured |
226
|
|
|
* |
227
|
|
|
* @param \Http\Client\Common\Plugin|\Http\Client\Common\Plugin[] ...$plugins |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function prependPlugin(Plugin ...$plugins) |
232
|
|
|
{ |
233
|
|
|
$plugins = array_reverse($plugins); |
234
|
|
|
|
235
|
|
|
foreach ($plugins as $plugin) { |
236
|
|
|
array_unshift($this->pluginsPrepend, $plugin); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|