Skip to content

Commit

Permalink
Merge pull request #129 from GW2Treasures/feature/return-response
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim authored May 10, 2023
2 parents d299f54 + 2e03325 commit 2cd7391
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/V2/ApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/V2/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/V2/Localization/LocalizationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 15 additions & 1 deletion tests/ApiHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
: [];
Expand Down Expand Up @@ -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 {
Expand All @@ -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 }');
}
}

0 comments on commit 2cd7391

Please sign in to comment.