|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Client; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\InflateStream; |
|
8
|
|
|
use Inspirum\Balikobot\Client\Response\Validator; |
|
9
|
|
|
use Inspirum\Balikobot\Exception\BadRequestException; |
|
10
|
|
|
use JsonException; |
|
11
|
|
|
use Psr\Http\Message\StreamInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use function json_decode; |
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
use function str_replace; |
|
16
|
|
|
use function trim; |
|
17
|
|
|
use const JSON_THROW_ON_ERROR; |
|
18
|
|
|
|
|
19
|
|
|
final class DefaultClient implements Client |
|
20
|
|
|
{ |
|
21
|
557 |
|
public function __construct( |
|
22
|
|
|
private readonly Requester $requester, |
|
23
|
|
|
private readonly Validator $validator, |
|
24
|
|
|
) { |
|
25
|
557 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @inheritDoc */ |
|
28
|
557 |
|
public function call( |
|
29
|
|
|
string $baseUrl, |
|
30
|
|
|
?string $carrier, |
|
31
|
|
|
string $method, |
|
32
|
|
|
array $data = [], |
|
33
|
|
|
?string $path = null, |
|
34
|
|
|
bool $shouldHaveStatus = true, |
|
35
|
|
|
bool $gzip = false, |
|
36
|
|
|
): array { |
|
37
|
557 |
|
$url = $this->resolveUrl($baseUrl, $carrier, $method, $path, $gzip); |
|
38
|
|
|
|
|
39
|
557 |
|
$response = $this->requester->request($url, $data); |
|
40
|
|
|
|
|
41
|
557 |
|
$statusCode = $response->getStatusCode(); |
|
42
|
557 |
|
$contents = $this->getContents($response->getBody(), $gzip); |
|
43
|
557 |
|
$parsedContent = $this->parseContents($contents, $statusCode < 300); |
|
44
|
|
|
|
|
45
|
555 |
|
$this->validateResponse($statusCode, $parsedContent, $shouldHaveStatus); |
|
46
|
|
|
|
|
47
|
260 |
|
return $parsedContent; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return non-empty-string |
|
52
|
|
|
*/ |
|
53
|
557 |
|
private function resolveUrl(string $baseUrl, ?string $carrier, string $method, ?string $path, bool $gzip): string |
|
54
|
|
|
{ |
|
55
|
557 |
|
$url = sprintf('%s/%s/%s', $carrier, $method, $path ?? ''); |
|
56
|
557 |
|
$url = trim(str_replace('//', '/', $url), '/'); |
|
57
|
|
|
|
|
58
|
557 |
|
if ($gzip) { |
|
59
|
478 |
|
$url = sprintf('%s?gzip=1', $url); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
557 |
|
return sprintf('%s/%s', $baseUrl, $url); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array<string,mixed> |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Inspirum\Balikobot\Exception\Exception |
|
69
|
|
|
*/ |
|
70
|
557 |
|
private function parseContents(string $content, bool $throwOnError): array |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
557 |
|
return json_decode($content, true, flags: JSON_THROW_ON_ERROR); |
|
74
|
3 |
|
} catch (JsonException $exception) { |
|
75
|
3 |
|
if ($throwOnError) { |
|
76
|
2 |
|
throw new BadRequestException([], 400, $exception, 'Cannot parse response data'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
557 |
|
private function getContents(StreamInterface $stream, bool $gzip): string |
|
84
|
|
|
{ |
|
85
|
557 |
|
if ($gzip === false) { |
|
86
|
79 |
|
return $stream->getContents(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
478 |
|
$inflateStream = new InflateStream($stream); |
|
91
|
|
|
|
|
92
|
478 |
|
return $inflateStream->getContents(); |
|
93
|
10 |
|
} catch (Throwable) { |
|
94
|
10 |
|
$stream->rewind(); |
|
95
|
|
|
|
|
96
|
10 |
|
return $stream->getContents(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param array<string,mixed> $response |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \Inspirum\Balikobot\Exception\Exception |
|
104
|
|
|
*/ |
|
105
|
555 |
|
private function validateResponse(int $statusCode, array $response, bool $shouldHaveStatus): void |
|
106
|
|
|
{ |
|
107
|
555 |
|
$this->validator->validateStatus($statusCode, $response); |
|
108
|
|
|
|
|
109
|
535 |
|
$this->validator->validateResponseStatus($response, null, $shouldHaveStatus); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|