|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IBM\Watson\Common\HttpClient; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
|
6
|
|
|
use Http\Client\Common\PluginClient; |
|
7
|
|
|
use Http\Client\HttpClient; |
|
8
|
|
|
use Http\Message\Authentication\BasicAuth; |
|
9
|
|
|
use IBM\Watson\Common\Util\DiscoveryTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Builder creates a user configured HTTP client. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Builder |
|
15
|
|
|
{ |
|
16
|
|
|
use DiscoveryTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Http\Client\HttpClient |
|
20
|
|
|
*/ |
|
21
|
|
|
private $httpClient; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $plugins = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $username |
|
30
|
|
|
*/ |
|
31
|
|
|
private $username; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $password |
|
35
|
|
|
*/ |
|
36
|
|
|
private $password; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param HttpClient|null $httpClient HTTP Client for sending requests. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
HttpClient $httpClient = null |
|
43
|
|
|
) { |
|
44
|
|
|
$this->httpClient = $httpClient ?: $this->discoverHttpClient(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return \Http\Client\Common\PluginClient HTTP client with configured plugins. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createConfiguredClient() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->addAuthenticationPlugin(); |
|
53
|
|
|
|
|
54
|
|
|
return new PluginClient($this->httpClient, $this->plugins); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Add username and password to client. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $username API username. |
|
61
|
|
|
* @param string $password API password. |
|
62
|
|
|
* |
|
63
|
|
|
* @return \IBM\Watson\Common\HttpClient\Builder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function withCredentials($username, $password) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this |
|
68
|
|
|
->withUsername($username) |
|
69
|
|
|
->withPassword($password); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add username to client. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $username API username. |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function withUsername($username) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->username = $username; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Add password to client. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $password API password. |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function withPassword($password) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->password = $password; |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Add BasicAuth authentication plugin to client. |
|
102
|
|
|
* |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
private function addAuthenticationPlugin() |
|
106
|
|
|
{ |
|
107
|
|
|
if (null !== $this->username && null !== $this->password) { |
|
108
|
|
|
$this->plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|