-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHttpApi.php
168 lines (151 loc) · 5.17 KB
/
HttpApi.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
<?php
declare(strict_types=1);
/*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FAPI\Sylius\Api;
use FAPI\Sylius\Exception\Domain as DomainExceptions;
use FAPI\Sylius\Exception\DomainException;
use FAPI\Sylius\Hydrator\Hydrator;
use FAPI\Sylius\Hydrator\NoopHydrator;
use FAPI\Sylius\RequestBuilder;
use Http\Client\HttpClient;
use Psr\Http\Message\ResponseInterface;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
abstract class HttpApi
{
/**
* @var HttpClient
*/
protected $httpClient;
/**
* @var Hydrator
*/
protected $hydrator;
/**
* @var RequestBuilder
*/
protected $requestBuilder;
public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder)
{
$this->httpClient = $httpClient;
$this->requestBuilder = $requestBuilder;
if (!$hydrator instanceof NoopHydrator) {
$this->hydrator = $hydrator;
}
}
/**
* Send a GET request with query parameters.
*
* @param string $path Request path
* @param array $params GET parameters
* @param array $requestHeaders Request Headers
*/
protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
if (\count($params) > 0) {
$path .= '?'.\http_build_query($params);
}
return $this->httpClient->sendRequest(
$this->requestBuilder->create('GET', $path, $requestHeaders)
);
}
/**
* Send a POST request with JSON-encoded parameters.
*
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*/
protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
return $this->httpPostRaw($path, $this->createJsonBody($params), $requestHeaders);
}
/**
* Send a POST request with raw data.
*
* @param string $path Request path
* @param array|string $body Request body
* @param array $requestHeaders Request headers
*/
protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
{
return $response = $this->httpClient->sendRequest(
$this->requestBuilder->create('POST', $path, $requestHeaders, $body)
);
}
/**
* Send a PUT request with JSON-encoded parameters.
*
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*/
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
return $this->httpClient->sendRequest(
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($params))
);
}
/**
* Send a PATCH request with JSON-encoded parameters.
*
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*/
protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
return $this->httpClient->sendRequest(
$this->requestBuilder->create('PATCH', $path, $requestHeaders, $this->createJsonBody($params))
);
}
/**
* Send a DELETE request with JSON-encoded parameters.
*
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*/
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
return $this->httpClient->sendRequest(
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($params))
);
}
/**
* Handle HTTP errors.
*
* Call is controlled by the specific API methods.
*
* @throws DomainException
*/
protected function handleErrors(ResponseInterface $response)
{
$body = $response->getBody()->__toString();
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException($body);
case 401:
throw new DomainExceptions\UnauthorizedException();
case 404:
throw new DomainExceptions\NotFoundException();
default:
throw new DomainExceptions\UnknownErrorException();
}
}
/**
* Create a JSON encoded version of an array of parameters.
*
* @param array $params Request parameters
*
* @return string|null
*/
private function createJsonBody(array $params)
{
return (0 === \count($params)) ? null : \json_encode($params, empty($params) ? \JSON_FORCE_OBJECT : 0);
}
}