2
2
3
3
namespace Swis \JsonApi ;
4
4
5
- use GuzzleHttp \ ClientInterface as GuzzleClientInterface ;
6
- use GuzzleHttp \ Exception \ ClientException ;
7
- use GuzzleHttp \ Exception \ ServerException ;
5
+ use Http \ Client \ Exception \ HttpException ;
6
+ use Http \ Client \ HttpClient ;
7
+ use Http \ Message \ MessageFactory ;
8
8
use Psr \Http \Message \RequestInterface ;
9
9
use Swis \JsonApi \Interfaces \ClientInterface ;
10
10
@@ -31,7 +31,7 @@ class Client implements ClientInterface
31
31
const METHOD_POST = 'POST ' ;
32
32
33
33
/**
34
- * @var \GuzzleHttp\ClientInterface
34
+ * @var \Http\Client\HttpClient
35
35
*/
36
36
private $ client ;
37
37
@@ -41,31 +41,28 @@ class Client implements ClientInterface
41
41
private $ baseUri ;
42
42
43
43
/**
44
- * @var \Swis\JsonApi\RequestFactory
44
+ * @var MessageFactory
45
45
*/
46
- private $ requestFactory ;
46
+ private $ messageFactory ;
47
47
48
- /**
49
- * @var \Swis\JsonApi\ResponseFactory
50
- */
51
- private $ responseFactory ;
48
+ protected $ defaultHeaders = [
49
+ ' Accept ' => ' application/vnd.api+json ' ,
50
+ ' Content-Type ' => ' application/vnd.api+json ' ,
51
+ ] ;
52
52
53
53
/**
54
- * @param \GuzzleHttp\ClientInterface $client
55
- * @param string $baseUri
56
- * @param \Swis\JsonApi\RequestFactory $requestFactory
57
- * @param \Swis\JsonApi\ResponseFactory $responseFactory
54
+ * @param \Http\Client\HttpClient $client
55
+ * @param string $baseUri
56
+ * @param MessageFactory $messageFactory
58
57
*/
59
58
public function __construct (
60
- GuzzleClientInterface $ client ,
59
+ HttpClient $ client ,
61
60
string $ baseUri ,
62
- RequestFactory $ requestFactory ,
63
- ResponseFactory $ responseFactory
61
+ MessageFactory $ messageFactory
64
62
) {
65
63
$ this ->client = $ client ;
66
64
$ this ->baseUri = $ baseUri ;
67
- $ this ->requestFactory = $ requestFactory ;
68
- $ this ->responseFactory = $ responseFactory ;
65
+ $ this ->messageFactory = $ messageFactory ;
69
66
}
70
67
71
68
/**
@@ -88,8 +85,6 @@ public function setBaseUri(string $baseUri)
88
85
* @param string $endpoint
89
86
* @param array $headers
90
87
*
91
- * @throws \GuzzleHttp\Exception\GuzzleException
92
- *
93
88
* @return \Swis\JsonApi\Interfaces\ResponseInterface
94
89
*/
95
90
public function get (string $ endpoint , array $ headers = [])
@@ -102,8 +97,6 @@ public function get(string $endpoint, array $headers = [])
102
97
* @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
103
98
* @param array $headers
104
99
*
105
- * @throws \GuzzleHttp\Exception\GuzzleException
106
- *
107
100
* @return \Swis\JsonApi\Interfaces\ResponseInterface
108
101
*/
109
102
public function post (string $ endpoint , $ body , array $ headers = [])
@@ -116,8 +109,6 @@ public function post(string $endpoint, $body, array $headers = [])
116
109
* @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
117
110
* @param array $headers
118
111
*
119
- * @throws \GuzzleHttp\Exception\GuzzleException
120
- *
121
112
* @return \Swis\JsonApi\Interfaces\ResponseInterface
122
113
*/
123
114
public function patch (string $ endpoint , $ body , array $ headers = [])
@@ -129,8 +120,6 @@ public function patch(string $endpoint, $body, array $headers = [])
129
120
* @param string $endpoint
130
121
* @param array $headers
131
122
*
132
- * @throws \GuzzleHttp\Exception\GuzzleException
133
- *
134
123
* @return \Swis\JsonApi\Interfaces\ResponseInterface
135
124
*/
136
125
public function delete (string $ endpoint , array $ headers = [])
@@ -144,23 +133,19 @@ public function delete(string $endpoint, array $headers = [])
144
133
* @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
145
134
* @param array $headers
146
135
*
147
- * @throws \GuzzleHttp\Exception\GuzzleException
148
- *
149
136
* @return \Swis\JsonApi\Interfaces\ResponseInterface
150
137
*/
151
138
public function request (string $ method , string $ endpoint , $ body = null , array $ headers = [])
152
139
{
153
140
$ request = $ this ->buildRequest ($ method , $ endpoint , $ body , $ headers );
154
141
155
142
try {
156
- $ response = $ this ->responseFactory ->make ($ this ->client ->send ($ request ));
157
- } catch (ClientException $ e ) {
158
- $ response = $ this ->responseFactory ->make ($ e ->getResponse ());
159
- } catch (ServerException $ e ) {
160
- $ response = $ this ->responseFactory ->make ($ e ->getResponse ());
143
+ $ response = $ this ->client ->sendRequest ($ request );
144
+ } catch (HttpException $ e ) {
145
+ $ response = $ e ->getResponse ();
161
146
}
162
147
163
- return $ response ;
148
+ return new Response ( $ response) ;
164
149
}
165
150
166
151
/**
@@ -173,7 +158,7 @@ public function request(string $method, string $endpoint, $body = null, array $h
173
158
*/
174
159
protected function buildRequest (string $ method , string $ endpoint , $ body = null , array $ headers = []): RequestInterface
175
160
{
176
- return $ this ->requestFactory -> make ($ method , $ this ->getEndpoint ($ endpoint ), $ body , $ headers );
161
+ return $ this ->messageFactory -> createRequest ($ method , $ this ->getEndpoint ($ endpoint ), [], $ body , $ this -> mergeHeaders ( $ headers) );
177
162
}
178
163
179
164
/**
@@ -185,4 +170,19 @@ protected function getEndpoint(string $endpoint): string
185
170
{
186
171
return $ this ->baseUri .$ endpoint ;
187
172
}
173
+
174
+ protected function mergeHeaders (array $ headers = [])
175
+ {
176
+ return array_merge ($ this ->defaultHeaders , $ headers );
177
+ }
178
+
179
+ public function getDefaultHeaders (): array
180
+ {
181
+ return $ this ->defaultHeaders ;
182
+ }
183
+
184
+ public function setDefaultHeaders (array $ defaultHeaders )
185
+ {
186
+ $ this ->defaultHeaders = $ defaultHeaders ;
187
+ }
188
188
}
0 commit comments