-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClient.php
175 lines (156 loc) · 4.8 KB
/
Client.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php declare(strict_types=1);
namespace MyENA\RGW;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request as PSR7Request;
use GuzzleHttp\Psr7\Uri as PSR7Uri;
use GuzzleHttp\RequestOptions;
use MyENA\RGW\Error\ApiError;
use MyENA\RGW\Error\TransportError;
use MyENA\RGW\Signature\V2Signature;
use Psr\Http\Message\RequestInterface;
/**
* Class Client
*
* @package MyENA\RGW
*/
class Client
{
private const HTTP_BAD_REQUEST = 400;
private const REQUEST_OPTIONS = [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::DECODE_CONTENT => false,
RequestOptions::ALLOW_REDIRECTS => true,
];
/** @var \MyENA\RGW\Config */
private $config;
/** @var \MyENA\RGW\Signature */
private $signer;
/** @var string */
private $address;
/**
* Client constructor.
*
* @param \MyENA\RGW\Config $config
* @param \MyENA\RGW\Signature $signer
*/
public function __construct(Config $config, Signature $signer)
{
$this->config = $config;
$this->signer = $signer;
}
/**
* Returns a client built with environment config values and the v2 signer
*
* @return \MyENA\RGW\Client
*/
public static function defaultClient(): Client
{
return new static(Config::defaultConfig(), new V2Signature());
}
/**
* @return \MyENA\RGW\Config
*/
public function getConfig(): Config
{
return $this->config;
}
/**
* @return \MyENA\RGW\Chain\BucketRootLink
*/
public function Bucket(): Chain\BucketRootLink
{
return Chain\BucketRootLink::new(null, [], $this, $this->config->getLogger());
}
/**
* @return \MyENA\RGW\Chain\MetadataRootLink
*/
public function Metadata(): Chain\MetadataRootLink
{
return Chain\MetadataRootLink::new(null, [], $this, $this->config->getLogger());
}
/**
* @return \MyENA\RGW\Chain\UserRootLink
*/
public function User(): Chain\UserRootLink
{
return Chain\UserRootLink::new(null, [], $this, $this->config->getLogger());
}
/**
* @return \MyENA\RGW\Chain\UsageRootLink
*/
public function Usage(): Chain\UsageRootLink
{
return Chain\UsageRootLink::new(null, [], $this, $this->config->getLogger());
}
/**
* @param \MyENA\RGW\Request $request
* @return array(
* @type \Psr\Http\Message\ResponseInterface|null
* @type \MyENA\RGW\Error|null
* )
*/
public function do(Request $request): array
{
$psrRequest = $this->compilePSR7($request);
if (!$this->config->isSilent()) {
$this->config->getLogger()->debug(
"Executing {$psrRequest->getMethod()} {$psrRequest->getUri()->getPath()}"
);
}
try {
$resp = $this->config->getHttpClient()->send($psrRequest, self::REQUEST_OPTIONS);
} catch (GuzzleException $e) {
if (!$this->config->isSilent()) {
$this->config->getLogger()->error("Query returned {$e->getCode()}: {$e->getMessage()}");
}
return [null, new TransportError($e->getCode(), $e->getMessage())];
}
if (!$this->config->isSilent()) {
$this->config->getLogger()->debug("Query returned {$resp->getStatusCode()}: {$resp->getReasonPhrase()}");
}
if (self::HTTP_BAD_REQUEST <= $resp->getStatusCode()) {
return [null, ApiError::fromResponse($resp)];
}
return [$resp, null];
}
/**
* @param \MyENA\RGW\Request $r
* @return \Psr\Http\Message\RequestInterface
*/
private function compilePSR7(Request $r): RequestInterface
{
$uri = new PSR7Uri("{$this->serviceAddress()}{$r->uri()}");
if (0 < (count($r->parameters()))) {
$params = [];
foreach ($r->parameters() as $k => $v) {
if (null === $v) {
$params[] = $k;
} else {
$params[] = "{$k}={$v}";
}
}
$uri = $uri->withQuery(implode('&', $params));
$this->config->getLogger()->debug(sprintf('Compiled URL: %s', $uri));
}
$psrRequest = new PSR7Request($r->method(), $uri, $r->headers(), $r->body());
if ($r->authenticated()) {
$psrRequest = $this->signer->sign($this->config, $psrRequest);
}
return $psrRequest;
}
/**
* @return string
*/
private function serviceAddress(): string
{
if (!isset($this->address)) {
$this->address = sprintf(
'%s://%s/%s',
$this->config->isNoSSL() ? 'http' : 'https',
$this->config->getAddress(),
$this->config->getAdminPath()
);
}
return $this->address;
}
}