1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the KongAdminApi package. |
5
|
|
|
* |
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi; |
13
|
|
|
|
14
|
|
|
use Http\Client\Common\HttpMethodsClient; |
15
|
|
|
use Http\Client\Common\Plugin; |
16
|
|
|
use Http\Client\Common\PluginClient; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Discovery\HttpClientDiscovery; |
19
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
20
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
21
|
|
|
use Http\Message\MessageFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* client |
25
|
|
|
* |
26
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
27
|
|
|
*/ |
28
|
|
|
class Client |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* http client |
32
|
|
|
* @var \Http\Client\HttpClient $httpClient |
33
|
|
|
*/ |
34
|
|
|
private $httpClient = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* message factory |
38
|
|
|
* @var \Http\Message\MessageFactory $messageFactory |
39
|
|
|
*/ |
40
|
|
|
private $messageFactory = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* plugins |
44
|
|
|
* @var array $plugins |
45
|
|
|
*/ |
46
|
|
|
private $plugins = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* constructor |
50
|
|
|
* |
51
|
|
|
* @param string $baseUri |
52
|
|
|
* @param \Http\Client\HttpClient $httpClient |
53
|
|
|
*/ |
54
|
3 |
|
public function __construct($baseUri, HttpClient $httpClient = null) |
55
|
|
|
{ |
56
|
3 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
57
|
3 |
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
58
|
|
|
|
59
|
3 |
|
$this->plugins[] = new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($baseUri)); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* api |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return \Unikorp\KongAdminApi\ApiInterface |
68
|
|
|
* |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
*/ |
71
|
6 |
|
public function api($name) |
72
|
|
|
{ |
73
|
|
|
switch ($name) { |
74
|
6 |
|
case 'api': |
75
|
1 |
|
return new Api\Api($this); |
76
|
5 |
|
case 'cluster': |
77
|
1 |
|
return new Api\Cluster($this); |
78
|
4 |
|
case 'consumer': |
79
|
1 |
|
return new Api\Consumer($this); |
80
|
3 |
|
case 'information': |
81
|
1 |
|
return new Api\Information($this); |
82
|
2 |
|
case 'plugin': |
83
|
1 |
|
return new Api\Plugin($this); |
84
|
|
|
default: |
85
|
1 |
|
throw new \InvalidArgumentException(sprintf('Undefined api instance called: %s', $name)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* add plugin |
91
|
|
|
* |
92
|
|
|
* @param \Http\Client\Common\Plugin $plugin |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
1 |
|
public function addPlugin(Plugin $plugin) |
97
|
|
|
{ |
98
|
1 |
|
$this->plugins[] = $plugin; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* get http client |
103
|
|
|
* |
104
|
|
|
* @return \Http\Client\Common\HttpMethodsClient |
105
|
|
|
*/ |
106
|
1 |
|
public function getHttpClient() |
107
|
|
|
{ |
108
|
1 |
|
return new HttpMethodsClient( |
109
|
1 |
|
new PluginClient($this->httpClient, $this->plugins), |
110
|
1 |
|
$this->messageFactory |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|