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