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
|
|
|
* Builder constructor. |
54
|
|
|
* |
55
|
|
|
* @param \Http\Client\HttpClient|null $httpClient |
56
|
|
|
* @param \Http\Message\UriFactory|null $uriFactory |
57
|
|
|
* |
58
|
|
|
* @throws \Http\Discovery\Exception\NotFoundException |
59
|
|
|
*/ |
60
|
21 |
|
public function __construct( |
61
|
|
|
HttpClient $httpClient = null, |
62
|
|
|
UriFactory $uriFactory = null |
63
|
|
|
) { |
64
|
21 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
65
|
21 |
|
$this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find(); |
66
|
21 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create HTTP client with specified plugins |
70
|
|
|
* |
71
|
|
|
* @return \Http\Client\Common\PluginClient |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
* @throws \RuntimeException |
75
|
|
|
*/ |
76
|
21 |
|
public function createConfiguredClient() |
77
|
|
|
{ |
78
|
21 |
|
$plugins = $this->pluginsPrepend; |
79
|
|
|
|
80
|
21 |
|
$plugins[] = new HeaderDefaultsPlugin([ |
81
|
7 |
|
'User-Agent' => 'adam-paterson/ibm-watson-sdk (https://github.com/adam-paterson/ibm-watson-sdk)' |
82
|
14 |
|
]); |
83
|
|
|
|
84
|
21 |
|
if (null !== $this->endpoint) { |
85
|
9 |
|
$plugins[] = new Plugin\BaseUriPlugin($this->uriFactory->createUri($this->endpoint)); |
86
|
6 |
|
} |
87
|
|
|
|
88
|
21 |
|
if (null !== $this->username && null !== $this->password) { |
89
|
9 |
|
$plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password)); |
90
|
6 |
|
} |
91
|
|
|
|
92
|
21 |
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->pluginsAppend)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add endpoint to client |
97
|
|
|
* |
98
|
|
|
* @param string $endpoint |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
9 |
|
public function withEndpoint($endpoint) |
103
|
|
|
{ |
104
|
9 |
|
$this->endpoint = $endpoint; |
105
|
|
|
|
106
|
9 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add username to client |
111
|
|
|
* |
112
|
|
|
* @param string $username |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
9 |
|
public function withUsername($username) |
117
|
|
|
{ |
118
|
9 |
|
$this->username = $username; |
119
|
|
|
|
120
|
9 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add password to client |
125
|
|
|
* |
126
|
|
|
* @param string $password |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
9 |
|
public function withPassword($password) |
131
|
|
|
{ |
132
|
9 |
|
$this->password = $password; |
133
|
|
|
|
134
|
9 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add username and password to client |
139
|
|
|
* |
140
|
|
|
* @param string $username |
141
|
|
|
* @param string $password |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
9 |
|
public function withCredentials($username, $password) |
146
|
|
|
{ |
147
|
6 |
|
return $this |
148
|
9 |
|
->withUsername($username) |
149
|
9 |
|
->withPassword($password); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add plugins to client after it has been configured |
154
|
|
|
* |
155
|
|
|
* @param \Http\Client\Common\Plugin|\Http\Client\Common\Plugin[] ...$plugins |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
3 |
|
public function appendPlugin(Plugin ...$plugins) |
160
|
|
|
{ |
161
|
3 |
|
foreach ($plugins as $plugin) { |
162
|
3 |
|
$this->pluginsAppend[] = $plugin; |
163
|
2 |
|
} |
164
|
|
|
|
165
|
3 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Add plugins to client before it has been configured |
170
|
|
|
* |
171
|
|
|
* @param \Http\Client\Common\Plugin|\Http\Client\Common\Plugin[] ...$plugins |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
3 |
|
public function prependPlugin(Plugin ...$plugins) |
176
|
|
|
{ |
177
|
3 |
|
$plugins = array_reverse($plugins); |
178
|
|
|
|
179
|
3 |
|
foreach ($plugins as $plugin) { |
180
|
3 |
|
array_unshift($this->pluginsPrepend, $plugin); |
181
|
2 |
|
} |
182
|
|
|
|
183
|
3 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|