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