diff --git a/src/V2/ApiHandler.php b/src/V2/ApiHandler.php index 19dec49..4296662 100644 --- a/src/V2/ApiHandler.php +++ b/src/V2/ApiHandler.php @@ -69,8 +69,12 @@ public function onRequest( RequestInterface $request ) { * * @param ResponseInterface $response * @param RequestInterface $request + * + * @return ResponseInterface */ - public function onResponse( ResponseInterface $response, RequestInterface $request ) { } + public function onResponse( ResponseInterface $response, RequestInterface $request ) { + return $response; + } /** * Handle errors by the api. diff --git a/src/V2/Endpoint.php b/src/V2/Endpoint.php index d84d21b..0860ecb 100644 --- a/src/V2/Endpoint.php +++ b/src/V2/Endpoint.php @@ -3,6 +3,7 @@ namespace GW2Treasures\GW2Api\V2; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Pool; use GuzzleHttp\Psr7\Request; @@ -101,7 +102,7 @@ protected function request( array $query = [], $url = null, $method = 'GET', $op } foreach( $this->handlers as $handler ) { - $handler->onResponse( $response, $request ); + $response = $handler->onResponse( $response, $request ); } return new ApiResponse( $response ); @@ -154,7 +155,7 @@ protected function requestMany( array $queries = [], $url = null, $method = 'GET } foreach( $this->handlers as $handler ) { - $handler->onResponse( $response, $request ); + $response = $handler->onResponse( $response, $request ); } $responses[] = new ApiResponse( $response ); diff --git a/src/V2/Localization/LocalizationHandler.php b/src/V2/Localization/LocalizationHandler.php index 61810fa..789d1fb 100644 --- a/src/V2/Localization/LocalizationHandler.php +++ b/src/V2/Localization/LocalizationHandler.php @@ -39,5 +39,7 @@ public function onResponse( ResponseInterface $response, RequestInterface $reque $message = 'Invalid language (expected: ' . $requestLanguage . '; actual: ' . $responseLanguage . ')'; throw new InvalidLanguageException( $message, $requestLanguage, $responseLanguage, $response ); } + + return $response; } } diff --git a/tests/ApiHandlerTest.php b/tests/ApiHandlerTest.php index f1c6451..15dae92 100644 --- a/tests/ApiHandlerTest.php +++ b/tests/ApiHandlerTest.php @@ -21,7 +21,7 @@ protected function getHandler( IEndpoint $endpoint ) { return $handler; } - protected function makeResponse( $content, $contentType = 'application/json; charset=utf-8' ) { + public static function makeResponse( $content, $contentType = 'application/json; charset=utf-8' ) { $header = !is_null( $contentType ) ? [ 'Content-Type' => $contentType ] : []; @@ -67,6 +67,16 @@ public function testRegisterHandler() { $this->api()->registerHandler( $this->getHandler( $this->getEndpoint() ) ); } + + public function testReturnCustomResponse() { + $endpoint = $this->getEndpoint(); + $this->getHandler($endpoint); + + $this->mockResponse('{ "handler": false }'); + $response = $endpoint->test(); + + $this->assertTrue( $response->handler ); + } } class TestHandler extends ApiHandler { @@ -77,4 +87,8 @@ public function responseAsJson( ResponseInterface $response ) { public function queryAsArray( RequestInterface $request ) { return $this->getQueryAsArray($request); } + + public function onResponse( ResponseInterface $response, RequestInterface $request ) { + return ApiHandlerTest::makeResponse('{ "handler": true }'); + } }