-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathClient.php
More file actions
216 lines (189 loc) · 5.37 KB
/
Client.php
File metadata and controls
216 lines (189 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Opensaucesystems\Lxd;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Opensaucesystems\Lxd\Exception\InvalidEndpointException;
use Opensaucesystems\Lxd\Exception\ClientConnectionException;
use Opensaucesystems\Lxd\Exception\ServerException;
use Opensaucesystems\Lxd\HttpClient\Plugin\PathPrepend;
use Opensaucesystems\Lxd\HttpClient\Plugin\PathTrimEnd;
use Opensaucesystems\Lxd\HttpClient\Plugin\LxdExceptionThower;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
class Client
{
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $apiVersion;
/**
* The object that sends HTTP messages
*
* @var ClientInterface
*/
private $httpClient;
/**
* A HTTP client with all our plugins
*
* @var PluginClient
*/
private $pluginClient;
/**
* @var RequestFactoryInterface
*/
private $requestFactory;
/**
* @var Plugin[]
*/
private $plugins = [];
/**
* True if we should create a new Plugin client at next request.
*
* @var bool
*/
private $httpClientModified = true;
/**
* @var mixed|\Psr\Http\Message\StreamFactoryInterface
*/
private mixed $streamFactory;
/**
* Create a new lxd client Instance
*/
public function __construct(?ClientInterface $httpClient = null, $apiVersion = null, $url = null)
{
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
$this->requestFactory = Psr17FactoryDiscovery::findServerRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->apiVersion = $apiVersion ?: '1.0';
$this->url = $url ?: 'https://127.0.0.1:8443';
$this->addPlugin(new LxdExceptionThower());
$this->setUrl($this->url);
}
/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Sets the URL of your LXD instance.
*
* @param string $url URL of the API in the form of https://hostname:port
*/
public function setUrl($url)
{
$this->url = $url;
$this->removePlugin(Plugin\AddHostPlugin::class);
$this->removePlugin(PathPrepend::class);
$this->removePlugin(PathTrimEnd::class);
$this->addPlugin(new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($this->url)));
$this->addPlugin(new PathPrepend(sprintf('/%s', $this->getApiVersion())));
$this->addPlugin(new PathTrimEnd());
}
/**
* Add a new plugin to the end of the plugin chain.
*
* @param Plugin $plugin
*/
public function addPlugin(Plugin $plugin)
{
$this->plugins[] = $plugin;
$this->httpClientModified = true;
}
/**
* Remove a plugin by its fully qualified class name (FQCN).
*
* @param string $fqcn
*/
public function removePlugin($fqcn)
{
foreach ($this->plugins as $idx => $plugin) {
if ($plugin instanceof $fqcn) {
unset($this->plugins[$idx]);
$this->httpClientModified = true;
}
}
}
/**
* @return HttpMethodsClient
*/
public function getHttpClient()
{
if ($this->httpClientModified) {
$this->httpClientModified = false;
$this->pluginClient = new HttpMethodsClient(
new PluginClient($this->httpClient, $this->plugins),
$this->requestFactory,
$this->streamFactory
);
}
return $this->pluginClient;
}
/**
* @param ClientInterface $httpClient
*/
public function setHttpClient(ClientInterface $httpClient)
{
$this->httpClientModified = true;
$this->httpClient = $httpClient;
}
/**
* @return string
*/
public function getApiVersion()
{
return $this->apiVersion;
}
/**
* Add a cache plugin to cache responses locally.
*
* @param CacheItemPoolInterface $cache
* @param array $config
*/
public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
{
$this->removeCache();
$this->addPlugin(new Plugin\CachePlugin($cachePool, $this->streamFactory, $config));
}
/**
* Remove the cache plugin
*/
public function removeCache()
{
$this->removePlugin(Plugin\CachePlugin::class);
}
public function __get($endpoint)
{
$class = __NAMESPACE__.'\\Endpoint\\'.ucfirst($endpoint);
if (class_exists($class)) {
return new $class($this);
} else {
throw new InvalidEndpointException(
'Endpoint '.$class.', not implemented.'
);
}
}
/**
* Make sure to move the cache plugin to the end of the chain
*/
private function pushBackCachePlugin()
{
$cachePlugin = null;
foreach ($this->plugins as $i => $plugin) {
if ($plugin instanceof Plugin\CachePlugin) {
$cachePlugin = $plugin;
unset($this->plugins[$i]);
$this->plugins[] = $cachePlugin;
return;
}
}
}
}