22
33namespace Swis \JsonApi ;
44
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 ;
88use Psr \Http \Message \RequestInterface ;
99use Swis \JsonApi \Interfaces \ClientInterface ;
1010
@@ -31,7 +31,7 @@ class Client implements ClientInterface
3131 const METHOD_POST = 'POST ' ;
3232
3333 /**
34- * @var \GuzzleHttp\ClientInterface
34+ * @var \Http\Client\HttpClient
3535 */
3636 private $ client ;
3737
@@ -41,31 +41,28 @@ class Client implements ClientInterface
4141 private $ baseUri ;
4242
4343 /**
44- * @var \Swis\JsonApi\RequestFactory
44+ * @var MessageFactory
4545 */
46- private $ requestFactory ;
46+ private $ messageFactory ;
4747
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+ ] ;
5252
5353 /**
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
5857 */
5958 public function __construct (
60- GuzzleClientInterface $ client ,
59+ HttpClient $ client ,
6160 string $ baseUri ,
62- RequestFactory $ requestFactory ,
63- ResponseFactory $ responseFactory
61+ MessageFactory $ messageFactory
6462 ) {
6563 $ this ->client = $ client ;
6664 $ this ->baseUri = $ baseUri ;
67- $ this ->requestFactory = $ requestFactory ;
68- $ this ->responseFactory = $ responseFactory ;
65+ $ this ->messageFactory = $ messageFactory ;
6966 }
7067
7168 /**
@@ -88,8 +85,6 @@ public function setBaseUri(string $baseUri)
8885 * @param string $endpoint
8986 * @param array $headers
9087 *
91- * @throws \GuzzleHttp\Exception\GuzzleException
92- *
9388 * @return \Swis\JsonApi\Interfaces\ResponseInterface
9489 */
9590 public function get (string $ endpoint , array $ headers = [])
@@ -102,8 +97,6 @@ public function get(string $endpoint, array $headers = [])
10297 * @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
10398 * @param array $headers
10499 *
105- * @throws \GuzzleHttp\Exception\GuzzleException
106- *
107100 * @return \Swis\JsonApi\Interfaces\ResponseInterface
108101 */
109102 public function post (string $ endpoint , $ body , array $ headers = [])
@@ -116,8 +109,6 @@ public function post(string $endpoint, $body, array $headers = [])
116109 * @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
117110 * @param array $headers
118111 *
119- * @throws \GuzzleHttp\Exception\GuzzleException
120- *
121112 * @return \Swis\JsonApi\Interfaces\ResponseInterface
122113 */
123114 public function patch (string $ endpoint , $ body , array $ headers = [])
@@ -129,8 +120,6 @@ public function patch(string $endpoint, $body, array $headers = [])
129120 * @param string $endpoint
130121 * @param array $headers
131122 *
132- * @throws \GuzzleHttp\Exception\GuzzleException
133- *
134123 * @return \Swis\JsonApi\Interfaces\ResponseInterface
135124 */
136125 public function delete (string $ endpoint , array $ headers = [])
@@ -144,23 +133,19 @@ public function delete(string $endpoint, array $headers = [])
144133 * @param resource|string|null|int|float|bool|\Psr\Http\Message\StreamInterface|callable $body
145134 * @param array $headers
146135 *
147- * @throws \GuzzleHttp\Exception\GuzzleException
148- *
149136 * @return \Swis\JsonApi\Interfaces\ResponseInterface
150137 */
151138 public function request (string $ method , string $ endpoint , $ body = null , array $ headers = [])
152139 {
153140 $ request = $ this ->buildRequest ($ method , $ endpoint , $ body , $ headers );
154141
155142 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 ();
161146 }
162147
163- return $ response ;
148+ return new Response ( $ response) ;
164149 }
165150
166151 /**
@@ -173,7 +158,7 @@ public function request(string $method, string $endpoint, $body = null, array $h
173158 */
174159 protected function buildRequest (string $ method , string $ endpoint , $ body = null , array $ headers = []): RequestInterface
175160 {
176- return $ this ->requestFactory -> make ($ method , $ this ->getEndpoint ($ endpoint ), $ body , $ headers );
161+ return $ this ->messageFactory -> createRequest ($ method , $ this ->getEndpoint ($ endpoint ), [], $ body , $ this -> mergeHeaders ( $ headers) );
177162 }
178163
179164 /**
@@ -185,4 +170,19 @@ protected function getEndpoint(string $endpoint): string
185170 {
186171 return $ this ->baseUri .$ endpoint ;
187172 }
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+ }
188188}
0 commit comments