Skip to content

Commit c8795af

Browse files
authored
Merge pull request #112 from GW2Treasures/schema
Add schema version support
2 parents c6788ab + 7d4aee5 commit c8795af

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

src/GW2Api.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@
5353
use GW2Treasures\GW2Api\V2\IEndpoint;
5454
use GW2Treasures\GW2Api\V2\Localization\LocalizationHandler;
5555
use GW2Treasures\GW2Api\V2\Pagination\PaginationHandler;
56+
use GW2Treasures\GW2Api\V2\Schema\SchemaHandler;
5657

5758
class GW2Api {
59+
/** @const string */
60+
const DEFAULT_SCHEMA = '2019-03-20T00:00:00Z';
61+
5862
/** @var string $apiUrl */
5963
protected $apiUrl = 'https://api.guildwars2.com/';
6064

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

74+
/** @var string $schema */
75+
protected $schema = self::DEFAULT_SCHEMA;
76+
7077
function __construct( array $options = [] ) {
7178
$this->options = $this->getOptions( $options );
7279

@@ -149,6 +156,7 @@ protected function registerDefaultHandlers() {
149156
$this->registerHandler(LocalizationHandler::class);
150157
$this->registerHandler(PaginationHandler::class);
151158
$this->registerHandler(RestrictedGuildHandler::class);
159+
$this->registerHandler(SchemaHandler::class);
152160
}
153161

154162
public function getClient() {
@@ -330,4 +338,25 @@ public function attachRegisteredHandlers( IEndpoint $endpoint ) {
330338
}
331339
}
332340
}
341+
342+
/**
343+
* Set the schema version to use for this api instance.
344+
*
345+
* @param string $schema
346+
* @return $this
347+
*/
348+
public function schema( $schema ) {
349+
$this->schema = $schema;
350+
351+
return $this;
352+
}
353+
354+
/**
355+
* Get the schema version used by this api instance.
356+
*
357+
* @return String
358+
*/
359+
public function getSchema() {
360+
return $this->schema;
361+
}
333362
}

src/V2/Endpoint.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ abstract class Endpoint implements IEndpoint {
2020
/** @var ApiHandler[] */
2121
protected $handlers = [];
2222

23+
/** @var string schema */
24+
protected $schema = null;
25+
2326
/**
2427
* @param GW2Api $api
2528
*/
@@ -43,6 +46,28 @@ protected function getApi() {
4346
return $this->api;
4447
}
4548

49+
/**
50+
* Set the schema version to use when requesting this endpoint.
51+
*
52+
* @param string $schema
53+
* @return $this
54+
*/
55+
public function schema( $schema ) {
56+
$this->schema = $schema;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Get the schema used to request this endpoint.
63+
* Falls back to the global schema set in the GW2API instance.
64+
*
65+
* @return String
66+
*/
67+
public function getSchema() {
68+
return $this->schema !== null ? $this->schema : $this->getApi()->getSchema();
69+
}
70+
4671
/**
4772
* Creates a new Request to this Endpoint.
4873
*

src/V2/IEndpoint.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ public function attach( ApiHandler $handler );
1616
* @return string
1717
*/
1818
public function url();
19+
20+
/**
21+
* Set the schema version to use when requesting this endpoint.
22+
*
23+
* @param string $schema
24+
* @return $this
25+
*/
26+
public function schema( $schema );
27+
28+
/**
29+
* Get the schema used to request this endpoint.
30+
* Falls back to the global schema set in the GW2API instance.
31+
*
32+
* @return String
33+
*/
34+
public function getSchema();
1935
}

src/V2/Schema/SchemaHandler.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace GW2Treasures\GW2Api\V2\Schema;
4+
5+
use GW2Treasures\GW2Api\V2\ApiHandler;
6+
use GW2Treasures\GW2Api\V2\IEndpoint;
7+
use Psr\Http\Message\MessageInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
class SchemaHandler extends ApiHandler {
11+
function __construct( IEndpoint $endpoint ) {
12+
parent::__construct( $endpoint );
13+
}
14+
15+
/**
16+
* Add the schema version header.
17+
*
18+
* @param RequestInterface $request
19+
*
20+
* @return MessageInterface|RequestInterface
21+
*/
22+
public function onRequest( RequestInterface $request ) {
23+
$schema = $this->getEndpoint()->getSchema();
24+
25+
return $request->withHeader( 'X-Schema-Version', $schema );
26+
}
27+
}

tests/SchemaTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use GW2Treasures\GW2Api\GW2Api;
4+
use Stubs\EndpointStub;
5+
6+
const EXPECTED_DEFAULT_SCHEMA = GW2Api::DEFAULT_SCHEMA;
7+
const EXPECTED_CUSTOM_SCHEMA = '2077-01-01T00:00:00Z';
8+
9+
class SchemaTest extends TestCase {
10+
protected function getEndpoint() {
11+
return new EndpointStub( $this->api() );
12+
}
13+
14+
public function testDefaultSchema() {
15+
$this->mockResponse('[]');
16+
17+
$endpoint = $this->getEndpoint();
18+
19+
$this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema());
20+
21+
$endpoint->test();
22+
23+
$request = $this->getLastRequest();
24+
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_DEFAULT_SCHEMA);
25+
}
26+
27+
public function testCustomSchema() {
28+
$this->mockResponse('[]');
29+
30+
$endpoint = $this->getEndpoint()->schema(EXPECTED_CUSTOM_SCHEMA);
31+
32+
$this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema());
33+
34+
$endpoint->test();
35+
36+
$request = $this->getLastRequest();
37+
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA);
38+
}
39+
40+
public function testGlobalSchema() {
41+
$this->mockResponse('[]');
42+
43+
$endpoint = $this->getEndpoint();
44+
$this->assertEquals(EXPECTED_DEFAULT_SCHEMA, $endpoint->getSchema());
45+
46+
$this->api()->schema(EXPECTED_CUSTOM_SCHEMA);
47+
48+
$this->assertEquals(EXPECTED_CUSTOM_SCHEMA, $endpoint->getSchema());
49+
50+
$endpoint->test();
51+
52+
$request = $this->getLastRequest();
53+
$this->assertHasHeader($request, 'X-Schema-Version', EXPECTED_CUSTOM_SCHEMA);
54+
55+
// clean up schema for other tests
56+
$this->resetApi();
57+
}
58+
}

tests/TestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ protected function api() {
3939
return $this->api;
4040
}
4141

42+
protected function resetApi() {
43+
unset($this->api);
44+
}
45+
4246
/**
4347
* @param Response|RequestException|string $response
4448
* @param string $language

0 commit comments

Comments
 (0)