Skip to content

Commit

Permalink
Forward http client response to service client result (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
RhydianJenkins authored Mar 23, 2023
1 parent 351e515 commit 85d1a40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $client = ClientFactory::create([
'password' => 'bar',
'handler' => HandlerStack::create(
new MockHandler([
new Response(404, [], 'Hello, World! This is a test response.'),
new Response(404, [], '"Hello, World! This is a test response."'),
])
) ,
]);
Expand Down
18 changes: 16 additions & 2 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Utils;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

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

return new GuzzleClient(
$httpClient,
$serviceDescription,
client: $httpClient,
description: $serviceDescription,
responseToResultTransformer: function (
ResponseInterface $response,
RequestInterface $request
): ResultInterface {
return new Result([
'response' => Utils::jsonDecode((string) $response->getBody(), true)
]);
}
);
}

Expand All @@ -72,6 +85,7 @@ private static function createOAuthHttpClient(array $config): ClientInterface
'token' => $config['token'],
'token_secret' => $config['token_secret'],
]));

$config['handler'] = $stack;
$config['auth'] = 'oauth';

Expand Down
11 changes: 9 additions & 2 deletions tests/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ public function executesMockCommandSuccessfully(): void

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

$client->execute($createSite);
$result = $client->execute($createSite);

$this->assertEquals(
'Hello, World! This is a test response.',
$result['response']
);
}

/**
Expand Down Expand Up @@ -157,9 +162,11 @@ public function executesMockErrorSuccessfully(): void

private function createMockHandler(int $responseCode): HandlerStack
{
$jsonResponse = '"Hello, World! This is a test response."'; // ensure this is valid json

return HandlerStack::create(
new MockHandler([
new Response($responseCode, [], 'Hello, World! This is a test response.'),
new Response($responseCode, [], $jsonResponse),
])
);
}
Expand Down

0 comments on commit 85d1a40

Please sign in to comment.