1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoman4eg\Nalog\Api; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
7
|
|
|
use Psr\Http\Client\ClientInterface; |
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 ClientInterface $httpClient; |
21
|
|
|
protected RequestBuilder $requestBuilder; |
22
|
|
|
protected ModelHydrator $hydrator; |
23
|
|
|
|
24
|
|
|
public function __construct(ClientInterface $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 \JsonException |
59
|
|
|
* @throws ClientExceptionInterface |
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
|
|
|
* @throws ClientExceptionInterface |
70
|
|
|
*/ |
71
|
|
|
protected function httpPostRaw(string $path, ?string $body, array $requestHeaders = []): ResponseInterface |
72
|
|
|
{ |
73
|
|
|
return $this->httpClient->sendRequest( |
74
|
|
|
$this->requestBuilder->create('POST', $path, $requestHeaders, $body) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Send a PUT request with JSON-encoded parameters. |
80
|
|
|
* |
81
|
|
|
* @param string $path Request path |
82
|
|
|
* @param array $params POST parameters to be JSON encoded |
83
|
|
|
* @param array $requestHeaders Request headers |
84
|
|
|
* |
85
|
|
|
* @throws \JsonException |
86
|
|
|
* @throws ClientExceptionInterface |
87
|
|
|
*/ |
88
|
|
|
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
89
|
|
|
{ |
90
|
|
|
return $this->httpClient->sendRequest( |
91
|
|
|
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($params)) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Send a PATCH request with JSON-encoded parameters. |
97
|
|
|
* |
98
|
|
|
* @param string $path Request path |
99
|
|
|
* @param array $params POST parameters to be JSON encoded |
100
|
|
|
* @param array $requestHeaders Request headers |
101
|
|
|
* |
102
|
|
|
* @throws \JsonException |
103
|
|
|
* @throws ClientExceptionInterface |
104
|
|
|
*/ |
105
|
|
|
protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
106
|
|
|
{ |
107
|
|
|
return $this->httpClient->sendRequest( |
108
|
|
|
$this->requestBuilder->create('PATCH', $path, $requestHeaders, $this->createJsonBody($params)) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
114
|
|
|
* |
115
|
|
|
* @param string $path Request path |
116
|
|
|
* @param array $params POST parameters to be JSON encoded |
117
|
|
|
* @param array $requestHeaders Request headers |
118
|
|
|
* |
119
|
|
|
* @throws \JsonException |
120
|
|
|
* @throws ClientExceptionInterface |
121
|
|
|
*/ |
122
|
|
|
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
123
|
|
|
{ |
124
|
|
|
return $this->httpClient->sendRequest( |
125
|
|
|
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($params)) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Handle HTTP errors. |
131
|
|
|
* |
132
|
|
|
* Call is controlled by the specific API methods. |
133
|
|
|
* Use ErrorHandler::handle instead of this |
134
|
|
|
* |
135
|
|
|
* @throws DomainException |
136
|
|
|
* |
137
|
|
|
* @deprecated |
138
|
|
|
*/ |
139
|
|
|
protected function handleErrors(ResponseInterface $response): void |
140
|
|
|
{ |
141
|
|
|
(new ErrorHandler())->handleResponse($response); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Create a JSON encoded version of an array of parameters. |
146
|
|
|
* |
147
|
|
|
* @param array $params Request parameters |
148
|
|
|
* |
149
|
|
|
* @throws \JsonException |
150
|
|
|
*/ |
151
|
|
|
private function createJsonBody(array $params): ?string |
152
|
|
|
{ |
153
|
|
|
$options = empty($params) ? \JSON_FORCE_OBJECT : 0; |
154
|
|
|
|
155
|
|
|
return (\count($params) === 0) ? null : JSON::encode($params, $options); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|