forked from zepgram/module-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClient.php
executable file
·174 lines (158 loc) · 6.14 KB
/
HttpClient.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
<?php
/**
* This file is part of Zepgram\Rest\Model
*
* @package Zepgram\Rest\Model
* @file HttpClient.php
* @date 04 11 2021 23:28
*
* @author Benjamin Calef <[email protected]>
* @copyright 2021 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
**/
declare(strict_types=1);
namespace Zepgram\Rest\Model;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientFactory as GuzzleClientFactory;
use GuzzleHttp\Exception\RequestException;
use InvalidArgumentException;
use Magento\Framework\Serialize\SerializerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use Zepgram\Rest\Exception\ExternalException;
use Zepgram\Rest\Exception\InternalException;
use Zepgram\Rest\Exception\Result\ServiceException;
use Zepgram\Rest\Exception\Result\HttpException;
use Zepgram\Rest\Exception\Result\NotFoundException;
use Zepgram\Rest\Exception\Result\UnserializeException;
use Zepgram\Rest\Model\Cache\Identifier;
use Zepgram\Rest\Model\Cache\RestApiCache;
class HttpClient
{
public function __construct(
private GuzzleClientFactory $guzzleClientFactory,
private SerializerInterface $serializer,
private RestApiCache $restApiCache,
private Identifier $identifier,
private LoggerInterface $logger
) {}
/**
* @param ParametersInterface $parameters
* @return string|int|bool|array|null|float
* @throws ExternalException
* @throws InternalException
*/
public function request(ParametersInterface $parameters): string|int|bool|array|null|float
{
$config = $parameters->getConfig();
$context['method'] = __METHOD__;
$context['request'] = array_merge(['base_uri' => $config->getBaseUri()], $parameters->toArray());
$serviceName = $parameters->getServiceName();
if ($config->isDebugEnabled()) {
$this->logger->info(sprintf('[REST API] %s request', $serviceName), $context);
}
try {
if ($parameters->getCacheKey() && $result = $this->getCache($parameters)) {
if ($config->isDebugEnabled()) {
$context['response'] = $result;
$this->logger->info(sprintf('[REST API] %s cache', $serviceName), $context);
}
return $result;
}
$response = $this->guzzleRequest($parameters);
$code = $response->getStatusCode();
$body = (string) $response->getBody();
$context['code'] = $code;
$context['headers'] = $response->getHeaders();
try {
$result = $parameters->getIsJsonResponse() ? $this->serializer->unserialize($body) : $body;
$context['response'] = $result;
} catch (InvalidArgumentException $e) {
throw new UnserializeException(__('[REST API] %1', $serviceName), $e);
}
if ($parameters->getCacheKey()) {
$this->saveCache($parameters, $result);
}
if ($config->isDebugEnabled()) {
$this->logger->info(sprintf('[REST API] %s response', $serviceName), $context);
}
return $result;
} catch (ExternalException $e) {
if ($e->getCode() === 404) {
if ($config->isDebugEnabled()) {
$this->logger->info($e, $context);
}
} else {
$this->logger->alert($e, $context);
}
throw $e;
} catch (InternalException $e) {
$this->logger->emergency($e, $context);
throw $e;
} catch (Throwable $e) {
$this->logger->emergency($e, $context);
throw new InternalException(__($e->getMessage()));
}
}
/**
* @param ParametersInterface $parameters
* @return ResponseInterface
* @throws ServiceException
* @throws HttpException
* @throws NotFoundException
*/
private function guzzleRequest(ParametersInterface $parameters): ResponseInterface
{
/** @var GuzzleClient $client */
$client = $this->guzzleClientFactory->create([
'config' => [
'base_uri' => $parameters->getConfig()->getBaseUri(),
'timeout' => $parameters->getConfig()->getTimeout()
]
]);
$serviceName = $parameters->getServiceName();
try {
return $client->request(
$parameters->getMethod(),
$parameters->getUri(),
$parameters->getOptions()
);
} catch (RequestException $e) {
$response = $e->getResponse();
$body = $response && $response->getBody() ? (string) $response->getBody() : null;
if ($e->getCode() === 404) {
throw new NotFoundException(__('[REST API] %1 not found: %2', $serviceName, $body), $e, $e->getCode());
}
if (!empty($body) && $e->getCode() < 500) {
throw new ServiceException(__('[REST API] %1 error: %2', $serviceName, $body), $e, $e->getCode());
}
throw new HttpException(__('[REST API] %1', $serviceName), $e, $e->getCode());
} catch (Throwable $e) {
throw new HttpException(__('[REST API] %1', $serviceName), $e, $e->getCode());
}
}
/**
* @param ParametersInterface $parameters
* @return string|int|bool|array|null|float
*/
private function getCache(ParametersInterface $parameters): string|int|bool|array|null|float
{
$cacheKey = $this->identifier->getCacheKey($parameters);
$cache = $this->restApiCache->load($cacheKey);
if (!$cache) {
return false;
}
return $this->serializer->unserialize($cache);
}
/**
* @param ParametersInterface $parameters
* @param $result
*/
private function saveCache(ParametersInterface $parameters, $result): void
{
$cacheKey = $this->identifier->getCacheKey($parameters);
$data = $this->serializer->serialize($result);
$this->restApiCache->save($data, $cacheKey, [], $parameters->getConfig()->getCacheLifetime());
}
}