|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace CCT\Component\Rest\Http; |
|
6
|
|
|
|
|
7
|
|
|
use CCT\Component\Rest\Exception\InvalidParameterException; |
|
8
|
|
|
use CCT\Component\Rest\Exception\ServiceUnavailableException; |
|
9
|
|
|
use CCT\Component\Rest\Http\Definition\QueryParams; |
|
10
|
|
|
use CCT\Component\Rest\Http\Definition\RequestHeaders; |
|
11
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
12
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
13
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractRequest implements RequestInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var GuzzleClient |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Request headers |
|
25
|
|
|
* |
|
26
|
|
|
* @var RequestHeaders |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $headers; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param GuzzleClient $client |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function __construct(GuzzleClient $client) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$this->client = $client; |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $uri |
|
40
|
|
|
* @param QueryParams|null $queryParams |
|
41
|
|
|
* |
|
42
|
|
|
* @return ResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
43
|
|
|
*/ |
|
44
|
3 |
|
protected function requestGet($uri, QueryParams $queryParams = null) |
|
45
|
|
|
{ |
|
46
|
3 |
|
return $this->execute(self::METHOD_GET, $uri, [], $queryParams); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $uri |
|
51
|
|
|
* @param QueryParams|null $queryParams |
|
52
|
|
|
* |
|
53
|
|
|
* @return ResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function requestDelete($uri, QueryParams $queryParams = null) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->execute(self::METHOD_DELETE, $uri, [], $queryParams); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $uri |
|
62
|
|
|
* @param array|object $formData |
|
63
|
|
|
* @param QueryParams|null $queryParams |
|
64
|
|
|
* |
|
65
|
|
|
* @return ResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function requestPost($uri, $formData, QueryParams $queryParams = null) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->execute(self::METHOD_POST, $uri, $formData, $queryParams); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $uri |
|
74
|
|
|
* @param array|object $formData |
|
75
|
|
|
* @param QueryParams|null $queryParams |
|
76
|
|
|
* |
|
77
|
|
|
* @return ResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function requestPatch($uri, $formData, QueryParams $queryParams = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->execute(self::METHOD_PATCH, $uri, $formData, $queryParams); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $uri |
|
86
|
|
|
* @param array|object $formData |
|
87
|
|
|
* @param QueryParams|null $queryParams |
|
88
|
|
|
* |
|
89
|
|
|
* @return ResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function requestPut($uri, $formData, QueryParams $queryParams = null) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->execute(self::METHOD_PUT, $uri, $formData, $queryParams); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $method |
|
98
|
|
|
* @param string $uri |
|
99
|
|
|
* @param array|object $formData |
|
100
|
|
|
* @param QueryParams|null $queryParams |
|
101
|
|
|
* |
|
102
|
|
|
* @return PsrResponseInterface|\Symfony\Component\HttpFoundation\Response |
|
103
|
|
|
*/ |
|
104
|
3 |
|
protected function execute($method, string $uri, $formData = [], QueryParams $queryParams = null) |
|
105
|
|
|
{ |
|
106
|
3 |
|
$options = $this->getRequestOptions($formData); |
|
107
|
|
|
|
|
108
|
3 |
|
$queryParams = $queryParams ?: new QueryParams(); |
|
109
|
3 |
|
$uri = $this->addQueryParamsToUri($uri, $queryParams); |
|
110
|
|
|
|
|
111
|
3 |
|
$response = $this->sendRequest($method, $uri, $options); |
|
112
|
|
|
|
|
113
|
3 |
|
return $response; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array|object $formData |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getRequestOptions($formData = []) |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
'form_params' => $formData, |
|
125
|
|
|
'headers' => $this->getHeaders()->toArray() |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $method |
|
131
|
|
|
* @param string $uri |
|
132
|
|
|
* @param array $options |
|
133
|
|
|
* |
|
134
|
|
|
* @throws ServiceUnavailableException |
|
135
|
|
|
* |
|
136
|
|
|
* @return Response|object |
|
137
|
|
|
*/ |
|
138
|
3 |
|
protected function sendRequest($method, string $uri, $options = []) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
3 |
|
$response = $this->client->request($method, $uri, $options); |
|
142
|
|
|
} catch (ConnectException $e) { |
|
143
|
|
|
throw new ServiceUnavailableException($e->getRequest(), $e->getMessage()); |
|
144
|
|
|
} catch (RequestException $e) { |
|
145
|
|
|
if (null === $e->getResponse()->getBody()) { |
|
146
|
|
|
throw $e; |
|
147
|
|
|
} |
|
148
|
|
|
$response = $e->getResponse(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
3 |
|
return $response; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Adds a query string params to the URI. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $uri |
|
158
|
|
|
* @param QueryParams $queryParams |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
3 |
|
protected function addQueryParamsToUri(string $uri, QueryParams $queryParams) |
|
163
|
|
|
{ |
|
164
|
3 |
|
if (false !== strpos($uri, '?')) { |
|
165
|
|
|
throw new InvalidParameterException(sprintf( |
|
166
|
|
|
'It was not possible to normalize the URI as the current URI %s already |
|
167
|
|
|
has the interrogation char in its string.' . |
|
168
|
|
|
$uri |
|
169
|
|
|
)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
3 |
|
return $uri . $queryParams->toString(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Set headers for request |
|
177
|
|
|
* |
|
178
|
|
|
* @param RequestHeaders $headers |
|
179
|
|
|
*/ |
|
180
|
3 |
|
protected function setHeaders(RequestHeaders $headers) |
|
181
|
|
|
{ |
|
182
|
3 |
|
$this->headers = $headers; |
|
183
|
3 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get headers for request |
|
187
|
|
|
* |
|
188
|
|
|
* @return RequestHeaders |
|
189
|
|
|
*/ |
|
190
|
3 |
|
protected function getHeaders(): RequestHeaders |
|
191
|
|
|
{ |
|
192
|
3 |
|
return $this->headers; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|