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