generated from renoki-co/laravel-package-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathMakesHttpCalls.php
158 lines (132 loc) · 4.38 KB
/
MakesHttpCalls.php
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
<?php
namespace RenokiCo\PhpK8s\Traits\Cluster;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\RequestOptions;
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
use RenokiCo\PhpK8s\ResourcesList;
trait MakesHttpCalls
{
/**
* Get the callable URL for a specific path.
*
* @param string $path
* @param array $query
* @return string
*/
public function getCallableUrl(string $path, array $query = ['pretty' => 1])
{
/**
* Replace any name[<number>]=value occurences with name=value
* to support argv input.
*/
$query = urldecode(preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($query)));
return "{$this->url}{$path}?{$query}";
}
/**
* Get the Guzzle Client to perform requests on.
*
* @return \GuzzleHttp\Client
*/
public function getClient()
{
$options = [
RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip, deflate',
],
RequestOptions::VERIFY => true,
];
if (is_bool($this->verify) || is_string($this->verify)) {
$options[RequestOptions::VERIFY] = $this->verify;
}
if ($this->token) {
$options[RequestOptions::HEADERS]['authorization'] = "Bearer {$this->token}";
}
if ($this->auth) {
$options[RequestOptions::AUTH] = $this->auth;
}
if ($this->cert) {
$options[RequestOptions::CERT] = $this->cert;
}
if ($this->sslKey) {
$options[RequestOptions::SSL_KEY] = $this->sslKey;
}
return new Client($options);
}
/**
* Make a HTTP call to a given path with a method and payload.
*
* @param string $method
* @param string $path
* @param string $payload
* @param array $query
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
public function call(
string $method,
string $path,
string $payload = '',
array $query = ['pretty' => 1],
array $options = []
): \Psr\Http\Message\ResponseInterface {
if ($payload) {
$options[RequestOptions::BODY] = $payload;
}
try {
$response = $this->getClient()->request($method, $this->getCallableUrl($path, $query), $options);
} catch (ClientException $e) {
$errorPayload = json_decode((string) $e->getResponse()->getBody(), true);
throw new KubernetesAPIException(
$e->getMessage(),
$errorPayload['code'] ?? 0,
$errorPayload
);
}
return $response;
}
/**
* Call the API with the specified method and path.
*
* @param string $method
* @param string $path
* @param string $payload
* @param array $query
* @param array $options
* @return mixed
*
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
protected function makeRequest(
string $method,
string $path,
string $payload = '',
array $query = ['pretty' => 1],
array $options = []
): mixed {
$resourceClass = $this->resourceClass;
$response = $this->call($method, $path, $payload, $query, $options);
$json = @json_decode($response->getBody(), true);
// If the output is not JSONable, return the response itself.
// This can be encountered in case of a pod log request, for example,
// where the data returned are just console logs.
if (! $json) {
return (string) $response->getBody();
}
// If the kind is a list, transform into a ResourcesList
// collection of instances for the same class.
if (isset($json['items'])) {
$results = [];
foreach ($json['items'] as $item) {
$results[] = (new $resourceClass($this, $item))->synced();
}
return new ResourcesList($results);
}
// If the items does not exist, it means the Kind
// is the same as the current class, so pass it
// for the payload.
return (new $resourceClass($this, $json))->synced();
}
}