1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client; |
4
|
|
|
|
5
|
|
|
use Http\Client\Exception\HttpException; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Http\Message\MessageFactory; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
10
|
|
|
|
11
|
|
|
class Client implements ClientInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const METHOD_DELETE = 'DELETE'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const METHOD_GET = 'GET'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const METHOD_PATCH = 'PATCH'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const METHOD_POST = 'POST'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Http\Client\HttpClient |
35
|
|
|
*/ |
36
|
|
|
private $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $baseUri; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var MessageFactory |
45
|
|
|
*/ |
46
|
|
|
private $messageFactory; |
47
|
|
|
|
48
|
|
|
protected $defaultHeaders = [ |
49
|
|
|
'Accept' => 'application/vnd.api+json', |
50
|
|
|
'Content-Type' => 'application/vnd.api+json', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \Http\Client\HttpClient $client |
55
|
|
|
* @param string $baseUri |
56
|
|
|
* @param MessageFactory $messageFactory |
57
|
|
|
*/ |
58
|
45 |
|
public function __construct( |
59
|
|
|
HttpClient $client, |
60
|
|
|
string $baseUri, |
61
|
|
|
MessageFactory $messageFactory |
62
|
|
|
) { |
63
|
45 |
|
$this->client = $client; |
64
|
45 |
|
$this->baseUri = $baseUri; |
65
|
45 |
|
$this->messageFactory = $messageFactory; |
66
|
45 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
5 |
|
public function getBaseUri(): string |
72
|
|
|
{ |
73
|
5 |
|
return $this->baseUri; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $baseUri |
78
|
|
|
*/ |
79
|
5 |
|
public function setBaseUri(string $baseUri) |
80
|
|
|
{ |
81
|
5 |
|
$this->baseUri = $baseUri; |
82
|
5 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $endpoint |
86
|
|
|
* @param array $headers |
87
|
|
|
* |
88
|
|
|
* @throws \Http\Client\Exception |
89
|
|
|
* |
90
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
91
|
|
|
*/ |
92
|
15 |
|
public function get(string $endpoint, array $headers = []) |
93
|
|
|
{ |
94
|
15 |
|
return $this->request(static::METHOD_GET, $endpoint, null, $headers); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $endpoint |
99
|
|
|
* @param resource|string|int|float|bool|\Psr\Http\Message\StreamInterface|callable|null $body |
100
|
|
|
* @param array $headers |
101
|
|
|
* |
102
|
|
|
* @throws \Http\Client\Exception |
103
|
|
|
* |
104
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
105
|
|
|
*/ |
106
|
5 |
|
public function post(string $endpoint, $body, array $headers = []) |
107
|
|
|
{ |
108
|
5 |
|
return $this->request(static::METHOD_POST, $endpoint, $body, $headers); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $endpoint |
113
|
|
|
* @param resource|string|int|float|bool|\Psr\Http\Message\StreamInterface|callable|null $body |
114
|
|
|
* @param array $headers |
115
|
|
|
* |
116
|
|
|
* @throws \Http\Client\Exception |
117
|
|
|
* |
118
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
119
|
|
|
*/ |
120
|
5 |
|
public function patch(string $endpoint, $body, array $headers = []) |
121
|
|
|
{ |
122
|
5 |
|
return $this->request(static::METHOD_PATCH, $endpoint, $body, $headers); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $endpoint |
127
|
|
|
* @param array $headers |
128
|
|
|
* |
129
|
|
|
* @throws \Http\Client\Exception |
130
|
|
|
* |
131
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
132
|
|
|
*/ |
133
|
5 |
|
public function delete(string $endpoint, array $headers = []) |
134
|
|
|
{ |
135
|
5 |
|
return $this->request(static::METHOD_DELETE, $endpoint, null, $headers); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $method |
140
|
|
|
* @param string $endpoint |
141
|
|
|
* @param resource|string|int|float|bool|\Psr\Http\Message\StreamInterface|callable|null $body |
142
|
|
|
* @param array $headers |
143
|
|
|
* |
144
|
|
|
* @throws \Http\Client\Exception |
145
|
|
|
* |
146
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
147
|
|
|
*/ |
148
|
35 |
|
public function request(string $method, string $endpoint, $body = null, array $headers = []) |
149
|
|
|
{ |
150
|
35 |
|
$request = $this->buildRequest($method, $endpoint, $body, $headers); |
151
|
|
|
|
152
|
|
|
try { |
153
|
35 |
|
return $this->client->sendRequest($request); |
154
|
10 |
|
} catch (HttpException $e) { |
155
|
5 |
|
return $e->getResponse(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $method |
161
|
|
|
* @param string $endpoint |
162
|
|
|
* @param resource|string|int|float|bool|\Psr\Http\Message\StreamInterface|callable|null $body |
163
|
|
|
* @param array $headers |
164
|
|
|
* |
165
|
|
|
* @return \Psr\Http\Message\RequestInterface |
166
|
|
|
*/ |
167
|
35 |
|
protected function buildRequest(string $method, string $endpoint, $body = null, array $headers = []): RequestInterface |
168
|
|
|
{ |
169
|
35 |
|
return $this->messageFactory->createRequest($method, $this->getEndpoint($endpoint), $this->mergeHeaders($headers), $body); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $endpoint |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
35 |
|
protected function getEndpoint(string $endpoint): string |
178
|
|
|
{ |
179
|
35 |
|
return $this->baseUri.$endpoint; |
180
|
|
|
} |
181
|
|
|
|
182
|
35 |
|
protected function mergeHeaders(array $headers = []) |
183
|
|
|
{ |
184
|
35 |
|
return array_merge($this->defaultHeaders, $headers); |
185
|
|
|
} |
186
|
|
|
|
187
|
5 |
|
public function getDefaultHeaders(): array |
188
|
|
|
{ |
189
|
5 |
|
return $this->defaultHeaders; |
190
|
|
|
} |
191
|
|
|
|
192
|
5 |
|
public function setDefaultHeaders(array $defaultHeaders) |
193
|
|
|
{ |
194
|
5 |
|
$this->defaultHeaders = $defaultHeaders; |
195
|
5 |
|
} |
196
|
|
|
} |
197
|
|
|
|