Skip to content

Commit 85d1a40

Browse files
Forward http client response to service client result (#5)
1 parent 351e515 commit 85d1a40

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $client = ClientFactory::create([
5353
'password' => 'bar',
5454
'handler' => HandlerStack::create(
5555
new MockHandler([
56-
new Response(404, [], 'Hello, World! This is a test response.'),
56+
new Response(404, [], '"Hello, World! This is a test response."'),
5757
])
5858
) ,
5959
]);

src/ClientFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
use GuzzleHttp\ClientInterface;
99
use GuzzleHttp\Command\Guzzle\GuzzleClient;
1010
use GuzzleHttp\Command\Guzzle\Description;
11+
use GuzzleHttp\Command\Result;
12+
use GuzzleHttp\Command\ResultInterface;
1113
use GuzzleHttp\HandlerStack;
1214
use GuzzleHttp\Subscriber\Oauth\Oauth1;
15+
use GuzzleHttp\Utils;
1316
use InvalidArgumentException;
17+
use Psr\Http\Message\RequestInterface;
18+
use Psr\Http\Message\ResponseInterface;
1419

1520
class ClientFactory
1621
{
@@ -48,8 +53,16 @@ private static function createGuzzleClient(array $config, string $authType): Guz
4853
$serviceDescription = new Description($serviceDescriptionContents);
4954

5055
return new GuzzleClient(
51-
$httpClient,
52-
$serviceDescription,
56+
client: $httpClient,
57+
description: $serviceDescription,
58+
responseToResultTransformer: function (
59+
ResponseInterface $response,
60+
RequestInterface $request
61+
): ResultInterface {
62+
return new Result([
63+
'response' => Utils::jsonDecode((string) $response->getBody(), true)
64+
]);
65+
}
5366
);
5467
}
5568

@@ -72,6 +85,7 @@ private static function createOAuthHttpClient(array $config): ClientInterface
7285
'token' => $config['token'],
7386
'token_secret' => $config['token_secret'],
7487
]));
88+
7589
$config['handler'] = $stack;
7690
$config['auth'] = 'oauth';
7791

tests/ClientFactoryTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ public function executesMockCommandSuccessfully(): void
102102

103103
$this->assertTrue($createSite instanceof Command);
104104

105-
$client->execute($createSite);
105+
$result = $client->execute($createSite);
106+
107+
$this->assertEquals(
108+
'Hello, World! This is a test response.',
109+
$result['response']
110+
);
106111
}
107112

108113
/**
@@ -157,9 +162,11 @@ public function executesMockErrorSuccessfully(): void
157162

158163
private function createMockHandler(int $responseCode): HandlerStack
159164
{
165+
$jsonResponse = '"Hello, World! This is a test response."'; // ensure this is valid json
166+
160167
return HandlerStack::create(
161168
new MockHandler([
162-
new Response($responseCode, [], 'Hello, World! This is a test response.'),
169+
new Response($responseCode, [], $jsonResponse),
163170
])
164171
);
165172
}

0 commit comments

Comments
 (0)