Skip to content

Commit bc195a5

Browse files
authored
Merge pull request #14 from swisnl/php-http
Refactored to use php-http/httpplug
2 parents 3b6df54 + e20cc71 commit bc195a5

24 files changed

+513
-277
lines changed

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
"type": "library",
44
"require": {
55
"php": ">=7.0",
6-
"guzzlehttp/guzzle": "^6.2",
76
"art4/json-api-client": "^0.6.3",
7+
"illuminate/support": "5.3.*|5.4.*|5.5.*",
88
"jenssegers/model": "^1.1",
9-
"illuminate/support": "5.3.*|5.4.*|5.5.*"
9+
"php-http/client-implementation": "^1.0",
10+
"php-http/discovery": "^1.0"
1011
},
1112
"require-dev": {
12-
"phpunit/phpunit": "^6.1",
1313
"fzaninotto/faker": "^1.6",
1414
"friendsofphp/php-cs-fixer": "^2.0",
15-
"graham-campbell/testbench": "^4.0"
15+
"graham-campbell/testbench": "^4.0",
16+
"guzzlehttp/psr7": "^1.0",
17+
"phpunit/phpunit": "^6.1",
18+
"php-http/guzzle6-adapter": "^1.1",
19+
"php-http/mock-client": "^1.1"
1620
},
1721
"autoload": {
1822
"psr-4": {

src/Client.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace 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;
88
use Psr\Http\Message\RequestInterface;
99
use 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
}

src/DocumentClient.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public function setBaseUri(string $baseUri)
6060
/**
6161
* @param string $endpoint
6262
*
63-
* @throws \GuzzleHttp\Exception\GuzzleException
64-
*
6563
* @return \Swis\JsonApi\Interfaces\DocumentInterface
6664
*/
6765
public function get(string $endpoint): DocumentInterface
@@ -73,8 +71,6 @@ public function get(string $endpoint): DocumentInterface
7371
* @param string $endpoint
7472
* @param \Swis\JsonApi\Interfaces\ItemDocumentInterface $body
7573
*
76-
* @throws \GuzzleHttp\Exception\GuzzleException
77-
*
7874
* @return \Swis\JsonApi\Interfaces\DocumentInterface
7975
*/
8076
public function post(string $endpoint, ItemDocumentInterface $body): DocumentInterface
@@ -86,8 +82,6 @@ public function post(string $endpoint, ItemDocumentInterface $body): DocumentInt
8682
* @param string $endpoint
8783
* @param \Swis\JsonApi\Interfaces\ItemDocumentInterface $body
8884
*
89-
* @throws \GuzzleHttp\Exception\GuzzleException
90-
*
9185
* @return \Swis\JsonApi\Interfaces\DocumentInterface
9286
*/
9387
public function patch(string $endpoint, ItemDocumentInterface $body): DocumentInterface
@@ -98,8 +92,6 @@ public function patch(string $endpoint, ItemDocumentInterface $body): DocumentIn
9892
/**
9993
* @param string $endpoint
10094
*
101-
* @throws \GuzzleHttp\Exception\GuzzleException
102-
*
10395
* @return \Swis\JsonApi\Interfaces\DocumentInterface
10496
*/
10597
public function delete(string $endpoint): DocumentInterface
@@ -128,7 +120,7 @@ protected function sanitizeJson(string $json): string
128120
}
129121

130122
/**
131-
* @param \Swis\JsonApi\Interfaces\ResponseInterface $response
123+
* @param ResponseInterface $response
132124
*
133125
* @return \Swis\JsonApi\Interfaces\DocumentInterface
134126
*/

src/Guzzle/FixtureResponseBuilder.php renamed to src/Fixtures/FixtureResponseBuilder.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
namespace Swis\JsonApi\Guzzle;
3+
namespace Swis\JsonApi\Fixtures;
44

5-
use GuzzleHttp\Psr7\Response;
5+
use Http\Discovery\MessageFactoryDiscovery;
66
use Illuminate\Support\Str;
77
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
89

910
class FixtureResponseBuilder implements FixtureResponseBuilderInterface
1011
{
@@ -47,14 +48,15 @@ public function __construct(string $fixturesPath, array $domainAliases = [])
4748
* @param \Psr\Http\Message\RequestInterface $request
4849
*
4950
* @throws \RuntimeException
50-
* @throws \Swis\JsonApi\Guzzle\MockNotFoundException
51+
* @throws \Swis\JsonApi\Fixtures\MockNotFoundException
5152
*
52-
* @return \GuzzleHttp\Psr7\Response
53+
* @return ResponseInterface
5354
*/
54-
public function build(RequestInterface $request): Response
55+
public function build(RequestInterface $request): ResponseInterface
5556
{
56-
return new Response(
57+
return MessageFactoryDiscovery::find()->createResponse(
5758
$this->getMockStatusForRequest($request),
59+
'',
5860
$this->getMockHeadersForRequest($request),
5961
$this->getMockBodyForRequest($request)
6062
);
@@ -95,7 +97,7 @@ protected function getMockHeadersForRequest(RequestInterface $request): array
9597
try {
9698
$file = $this->getMockFilePathForRequest($request, self::TYPE_HEADERS);
9799

98-
$headers = \GuzzleHttp\json_decode(file_get_contents($file), true);
100+
$headers = json_decode(file_get_contents($file), true);
99101
} catch (MockNotFoundException $e) {
100102
}
101103

@@ -106,7 +108,7 @@ protected function getMockHeadersForRequest(RequestInterface $request): array
106108
* @param \Psr\Http\Message\RequestInterface $request
107109
*
108110
* @throws \RuntimeException
109-
* @throws \Swis\JsonApi\Guzzle\MockNotFoundException
111+
* @throws \Swis\JsonApi\Fixtures\MockNotFoundException
110112
*
111113
* @return string
112114
*/
@@ -121,7 +123,7 @@ protected function getMockBodyForRequest(RequestInterface $request): string
121123
* @param \Psr\Http\Message\RequestInterface $request
122124
* @param string $type
123125
*
124-
* @throws \Swis\JsonApi\Guzzle\MockNotFoundException
126+
* @throws \Swis\JsonApi\Fixtures\MockNotFoundException
125127
* @throws \RuntimeException
126128
*
127129
* @return string
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace Swis\JsonApi\Guzzle;
3+
namespace Swis\JsonApi\Fixtures;
44

55
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
67

78
interface FixtureResponseBuilderInterface
89
{
@@ -11,5 +12,5 @@ interface FixtureResponseBuilderInterface
1112
*
1213
* @return \Psr\Http\Message\ResponseInterface
1314
*/
14-
public function build(RequestInterface $request);
15+
public function build(RequestInterface $request): ResponseInterface;
1516
}

src/Fixtures/FixturesClient.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Fixtures;
4+
5+
use Http\Message\ResponseFactory;
6+
use Http\Mock\Client;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class FixturesClient extends Client
10+
{
11+
protected $fixtureResponseBuilder;
12+
13+
/**
14+
* @param FixtureResponseBuilderInterface $fixtureResponseBuilder
15+
* @param ResponseFactory|null $responseFactory
16+
*/
17+
public function __construct(FixtureResponseBuilderInterface $fixtureResponseBuilder, ResponseFactory $responseFactory = null)
18+
{
19+
parent::__construct($responseFactory);
20+
21+
$this->fixtureResponseBuilder = $fixtureResponseBuilder;
22+
}
23+
24+
public function sendRequest(RequestInterface $request)
25+
{
26+
$this->setDefaultResponse($this->fixtureResponseBuilder->build($request));
27+
28+
return parent::sendRequest($request);
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Fixtures\Guzzle;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Swis\JsonApi\Fixtures\FixtureResponseBuilder as BaseFixtureResponseBuilder;
9+
use Swis\JsonApi\Fixtures\FixtureResponseBuilderInterface;
10+
11+
class FixtureResponseBuilder extends BaseFixtureResponseBuilder
12+
{
13+
/**
14+
* @param \Psr\Http\Message\RequestInterface $request
15+
*
16+
* @throws \RuntimeException
17+
* @throws \Swis\JsonApi\Fixtures\MockNotFoundException
18+
*
19+
* @return ResponseInterface
20+
*/
21+
public function build(RequestInterface $request): ResponseInterface
22+
{
23+
$response = new Response(
24+
$this->getMockStatusForRequest($request),
25+
$this->getMockHeadersForRequest($request),
26+
$this->getMockBodyForRequest($request)
27+
);
28+
29+
return $response;
30+
}
31+
}

0 commit comments

Comments
 (0)