Skip to content

Commit

Permalink
Merge pull request #112 from GW2Treasures/schema
Browse files Browse the repository at this point in the history
Add schema version support
  • Loading branch information
darthmaim authored Mar 20, 2019
2 parents c6788ab + 7d4aee5 commit c8795af
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/GW2Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@
use GW2Treasures\GW2Api\V2\IEndpoint;
use GW2Treasures\GW2Api\V2\Localization\LocalizationHandler;
use GW2Treasures\GW2Api\V2\Pagination\PaginationHandler;
use GW2Treasures\GW2Api\V2\Schema\SchemaHandler;

class GW2Api {
/** @const string */
const DEFAULT_SCHEMA = '2019-03-20T00:00:00Z';

/** @var string $apiUrl */
protected $apiUrl = 'https://api.guildwars2.com/';

Expand All @@ -67,6 +71,9 @@ class GW2Api {
/** @var array $handlers */
protected $handlers = [];

/** @var string $schema */
protected $schema = self::DEFAULT_SCHEMA;

function __construct( array $options = [] ) {
$this->options = $this->getOptions( $options );

Expand Down Expand Up @@ -149,6 +156,7 @@ protected function registerDefaultHandlers() {
$this->registerHandler(LocalizationHandler::class);
$this->registerHandler(PaginationHandler::class);
$this->registerHandler(RestrictedGuildHandler::class);
$this->registerHandler(SchemaHandler::class);
}

public function getClient() {
Expand Down Expand Up @@ -330,4 +338,25 @@ public function attachRegisteredHandlers( IEndpoint $endpoint ) {
}
}
}

/**
* Set the schema version to use for this api instance.
*
* @param string $schema
* @return $this
*/
public function schema( $schema ) {
$this->schema = $schema;

return $this;
}

/**
* Get the schema version used by this api instance.
*
* @return String
*/
public function getSchema() {
return $this->schema;
}
}
25 changes: 25 additions & 0 deletions src/V2/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ abstract class Endpoint implements IEndpoint {
/** @var ApiHandler[] */
protected $handlers = [];

/** @var string schema */
protected $schema = null;

/**
* @param GW2Api $api
*/
Expand All @@ -43,6 +46,28 @@ protected function getApi() {
return $this->api;
}

/**
* Set the schema version to use when requesting this endpoint.
*
* @param string $schema
* @return $this
*/
public function schema( $schema ) {
$this->schema = $schema;

return $this;
}

/**
* Get the schema used to request this endpoint.
* Falls back to the global schema set in the GW2API instance.
*
* @return String
*/
public function getSchema() {
return $this->schema !== null ? $this->schema : $this->getApi()->getSchema();
}

/**
* Creates a new Request to this Endpoint.
*
Expand Down
16 changes: 16 additions & 0 deletions src/V2/IEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ public function attach( ApiHandler $handler );
* @return string
*/
public function url();

/**
* Set the schema version to use when requesting this endpoint.
*
* @param string $schema
* @return $this
*/
public function schema( $schema );

/**
* Get the schema used to request this endpoint.
* Falls back to the global schema set in the GW2API instance.
*
* @return String
*/
public function getSchema();
}
27 changes: 27 additions & 0 deletions src/V2/Schema/SchemaHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace GW2Treasures\GW2Api\V2\Schema;

use GW2Treasures\GW2Api\V2\ApiHandler;
use GW2Treasures\GW2Api\V2\IEndpoint;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;

class SchemaHandler extends ApiHandler {
function __construct( IEndpoint $endpoint ) {
parent::__construct( $endpoint );
}

/**
* Add the schema version header.
*
* @param RequestInterface $request
*
* @return MessageInterface|RequestInterface
*/
public function onRequest( RequestInterface $request ) {
$schema = $this->getEndpoint()->getSchema();

return $request->withHeader( 'X-Schema-Version', $schema );
}
}
58 changes: 58 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use GW2Treasures\GW2Api\GW2Api;
use Stubs\EndpointStub;

const EXPECTED_DEFAULT_SCHEMA = GW2Api::DEFAULT_SCHEMA;
const EXPECTED_CUSTOM_SCHEMA = '2077-01-01T00:00:00Z';

class SchemaTest extends TestCase {
protected function getEndpoint() {
return new EndpointStub( $this->api() );
}

public function testDefaultSchema() {
$this->mockResponse('[]');

$endpoint = $this->getEndpoint();

$this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema());

$endpoint->test();

$request = $this->getLastRequest();
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_DEFAULT_SCHEMA);
}

public function testCustomSchema() {
$this->mockResponse('[]');

$endpoint = $this->getEndpoint()->schema(EXPECTED_CUSTOM_SCHEMA);

$this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema());

$endpoint->test();

$request = $this->getLastRequest();
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA);
}

public function testGlobalSchema() {
$this->mockResponse('[]');

$endpoint = $this->getEndpoint();
$this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema());

$this->api()->schema(EXPECTED_CUSTOM_SCHEMA);

$this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema());

$endpoint->test();

$request = $this->getLastRequest();
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA);

// clean up schema for other tests
$this->resetApi();
}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ protected function api() {
return $this->api;
}

protected function resetApi() {
unset($this->api);
}

/**
* @param Response|RequestException|string $response
* @param string $language
Expand Down

0 comments on commit c8795af

Please sign in to comment.