1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoman4eg\Nalog\Api; |
5
|
|
|
|
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Shoman4eg\Nalog\ErrorHandler; |
10
|
|
|
use Shoman4eg\Nalog\Exception\DomainException; |
11
|
|
|
use Shoman4eg\Nalog\RequestBuilder; |
12
|
|
|
use Shoman4eg\Nalog\Util\JSON; |
13
|
|
|
use Shoman4eg\Nalog\Util\ModelHydrator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseHttpApi |
19
|
|
|
{ |
20
|
|
|
protected HttpClient $httpClient; |
21
|
|
|
protected RequestBuilder $requestBuilder; |
22
|
|
|
protected ModelHydrator $hydrator; |
23
|
|
|
|
24
|
|
|
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder) |
25
|
|
|
{ |
26
|
|
|
$this->httpClient = $httpClient; |
27
|
|
|
$this->requestBuilder = $requestBuilder; |
28
|
|
|
$this->hydrator = new ModelHydrator(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Send a GET request with query parameters. |
33
|
|
|
* |
34
|
|
|
* @param string $path Request path |
35
|
|
|
* @param array $params GET parameters |
36
|
|
|
* @param array $requestHeaders Request Headers |
37
|
|
|
* |
38
|
|
|
* @throws ClientExceptionInterface |
39
|
|
|
*/ |
40
|
|
|
protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
41
|
|
|
{ |
42
|
|
|
if (\count($params) > 0) { |
43
|
|
|
$path .= sprintf('?%s', \http_build_query($params)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->httpClient->sendRequest( |
47
|
|
|
$this->requestBuilder->create('GET', $path, $requestHeaders) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Send a POST request with JSON-encoded parameters. |
53
|
|
|
* |
54
|
|
|
* @param string $path Request path |
55
|
|
|
* @param array $params POST parameters to be JSON encoded |
56
|
|
|
* @param array $requestHeaders Request headers |
57
|
|
|
* |
58
|
|
|
* @throws ClientExceptionInterface |
59
|
|
|
* @throws \JsonException |
60
|
|
|
*/ |
61
|
|
|
protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
62
|
|
|
{ |
63
|
|
|
return $this->httpPostRaw($path, $this->createJsonBody($params), $requestHeaders); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Send a POST request with raw data. |
68
|
|
|
* |
69
|
|
|
* @param string $path Request path |
70
|
|
|
* @param array|string $body Request body |
71
|
|
|
* @param array $requestHeaders Request headers |
72
|
|
|
* |
73
|
|
|
* @throws ClientExceptionInterface |
74
|
|
|
*/ |
75
|
|
|
protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
76
|
|
|
{ |
77
|
|
|
return $this->httpClient->sendRequest( |
78
|
|
|
$this->requestBuilder->create('POST', $path, $requestHeaders, $body) |
|
|
|
|
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Send a PUT request with JSON-encoded parameters. |
84
|
|
|
* |
85
|
|
|
* @param string $path Request path |
86
|
|
|
* @param array $params POST parameters to be JSON encoded |
87
|
|
|
* @param array $requestHeaders Request headers |
88
|
|
|
* |
89
|
|
|
* @throws ClientExceptionInterface |
90
|
|
|
* @throws \JsonException |
91
|
|
|
*/ |
92
|
|
|
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
93
|
|
|
{ |
94
|
|
|
return $this->httpClient->sendRequest( |
95
|
|
|
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($params)) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send a PATCH request with JSON-encoded parameters. |
101
|
|
|
* |
102
|
|
|
* @param string $path Request path |
103
|
|
|
* @param array $params POST parameters to be JSON encoded |
104
|
|
|
* @param array $requestHeaders Request headers |
105
|
|
|
* |
106
|
|
|
* @throws ClientExceptionInterface |
107
|
|
|
* @throws \JsonException |
108
|
|
|
*/ |
109
|
|
|
protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
110
|
|
|
{ |
111
|
|
|
return $this->httpClient->sendRequest( |
112
|
|
|
$this->requestBuilder->create('PATCH', $path, $requestHeaders, $this->createJsonBody($params)) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
118
|
|
|
* |
119
|
|
|
* @param string $path Request path |
120
|
|
|
* @param array $params POST parameters to be JSON encoded |
121
|
|
|
* @param array $requestHeaders Request headers |
122
|
|
|
* |
123
|
|
|
* @throws ClientExceptionInterface |
124
|
|
|
* @throws \JsonException |
125
|
|
|
*/ |
126
|
|
|
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
127
|
|
|
{ |
128
|
|
|
return $this->httpClient->sendRequest( |
129
|
|
|
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($params)) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Handle HTTP errors. |
135
|
|
|
* |
136
|
|
|
* Call is controlled by the specific API methods. |
137
|
|
|
* Use ErrorHandler::handle instead of this |
138
|
|
|
* |
139
|
|
|
* @throws DomainException |
140
|
|
|
* |
141
|
|
|
* @deprecated |
142
|
|
|
*/ |
143
|
|
|
protected function handleErrors(ResponseInterface $response): void |
144
|
|
|
{ |
145
|
|
|
(new ErrorHandler())->handleResponse($response); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create a JSON encoded version of an array of parameters. |
150
|
|
|
* |
151
|
|
|
* @param array $params Request parameters |
152
|
|
|
* |
153
|
|
|
* @throws \JsonException |
154
|
|
|
*/ |
155
|
|
|
private function createJsonBody(array $params): ?string |
156
|
|
|
{ |
157
|
|
|
$options = empty($params) ? \JSON_FORCE_OBJECT : 0; |
158
|
|
|
|
159
|
|
|
return (\count($params) === 0) ? null : JSON::encode($params, $options); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|