1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cobak78\RancherApi\Clients; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class HttpClient |
12
|
|
|
* @package Cobak78\RancherApi\Clients |
13
|
|
|
*/ |
14
|
|
|
class HttpClient |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $accessKey; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ClientInterface |
23
|
|
|
*/ |
24
|
|
|
private $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $secretKey; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $host; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* true if the last response returns |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $isSocket; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* HttpClient constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->accessKey = $_SERVER['RANCHER_APIKEY']; |
49
|
|
|
$this->httpClient = new Client(); |
50
|
|
|
$this->secretKey = $_SERVER['RANCHER_SHARED']; |
51
|
|
|
$this->host = $_SERVER['RANCHER_HOST']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* get isSocket |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isSocket() |
60
|
|
|
{ |
61
|
|
|
return $this->isSocket; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $uri |
66
|
|
|
* @param bool $withHost |
67
|
|
|
* |
68
|
|
|
* @return int|mixed |
69
|
|
|
*/ |
70
|
|
|
public function get($uri, $withHost = false) |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
|
74
|
|
|
return json_decode($this->request('GET', $uri, $withHost)->getBody()); |
75
|
|
|
|
76
|
|
|
} catch (ClientException $exception) { |
77
|
|
|
|
78
|
|
|
return $exception->getCode(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $uri |
84
|
|
|
* @param array $data |
85
|
|
|
* @param bool $withHost |
86
|
|
|
* |
87
|
|
|
* @return mixed|ResponseInterface |
88
|
|
|
*/ |
89
|
|
|
public function post($uri, array $data = [], $withHost = false) |
90
|
|
|
{ |
91
|
|
|
return $this->request('post', $uri, $withHost, [ |
92
|
|
|
'body' => json_encode($data) |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ClientInterface $httpClient |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setHttpClient(ClientInterface $httpClient) |
102
|
|
|
{ |
103
|
|
|
$this->httpClient = $httpClient; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $method |
110
|
|
|
* @param $uri |
111
|
|
|
* @param $withHost |
112
|
|
|
* @param array $data |
113
|
|
|
* @return mixed|ResponseInterface |
114
|
|
|
*/ |
115
|
|
|
private function request($method, $uri, $withHost, $data = []) |
116
|
|
|
{ |
117
|
|
|
$data['auth'] = [$this->accessKey, $this->secretKey]; |
118
|
|
|
|
119
|
|
|
$response = $this->httpClient->request($method, ($withHost) ? $uri : $this->host . $uri, $data); |
120
|
|
|
|
121
|
|
|
$body = $response->getBody(); |
122
|
|
|
|
123
|
|
|
$this->parseResponse($response); |
124
|
|
|
|
125
|
|
|
$body->rewind(); |
126
|
|
|
|
127
|
|
|
return $response; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param ResponseInterface $response |
132
|
|
|
*/ |
133
|
|
|
private function parseResponse(ResponseInterface $response) |
134
|
|
|
{ |
135
|
|
|
$this->isSocket = (strpos($response->getBody()->getContents(), 'ws:')) ? true : false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: