Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global authentication #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/GW2Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
use GW2Treasures\GW2Api\V2\Endpoint\World\WorldEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint\WvW\WvWEndpoint;
use GW2Treasures\GW2Api\V2\IEndpoint;
use GW2Treasures\GW2Api\V2\IParent;
use GW2Treasures\GW2Api\V2\Localization\LocalizationHandler;
use GW2Treasures\GW2Api\V2\Pagination\PaginationHandler;

class GW2Api {
class GW2Api implements IParent {
/** @var string $apiUrl */
protected $apiUrl = 'https://api.guildwars2.com/';

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

/** @var string|null $apiKey */
protected $apiKey = null;

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

Expand Down Expand Up @@ -141,8 +145,8 @@ public function getClient() {
return $this->client;
}

public function account( $apiKey ) {
return new AccountEndpoint( $this, $apiKey );
public function account() {
return new AccountEndpoint( $this );
}

public function achievements() {
Expand All @@ -153,8 +157,8 @@ public function build() {
return new BuildEndpoint( $this );
}

public function characters( $apiKey ) {
return new CharacterEndpoint( $this, $apiKey );
public function characters() {
return new CharacterEndpoint( $this );
}

public function colors() {
Expand Down Expand Up @@ -237,8 +241,8 @@ public function specializations() {
return new SpecializationEndpoint( $this );
}

public function tokeninfo( $apiKey ) {
return new TokeninfoEndpoint( $this, $apiKey );
public function tokeninfo() {
return new TokeninfoEndpoint( $this );
}

public function traits() {
Expand All @@ -260,4 +264,29 @@ public function attachRegisteredHandlers( IEndpoint $endpoint ) {
}
}
}

public function getApi() {
return $this;
}

public function getParent() {
return null;
}


/**
* Set the API key that should be used to request this endpoint.
*
* @param string $apiKey
* @return static
*/
public function auth($apiKey) {
$this->apiKey = $apiKey;

return $this;
}

public function getApiKey() {
return $this->apiKey;
}
}
37 changes: 36 additions & 1 deletion src/V2/Authentication/AuthenticatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

namespace GW2Treasures\GW2Api\V2\Authentication;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\IParent;

trait AuthenticatedEndpoint {
/**
* @return GW2Api
*/
protected abstract function getApi();

/**
* @return IParent
*/
protected abstract function getParent();

/** @var string $apiKey */
protected $apiKey;

Expand All @@ -12,6 +25,28 @@ trait AuthenticatedEndpoint {
* @return string
*/
public function getApiKey() {
return $this->apiKey;
if(isset($this->apiKey)) {
return $this->apiKey;
}

$parent = $this->getParent();

while (!($parent instanceof IAuthenticatedEndpoint || $parent instanceof GW2Api)) {
$parent = $parent->getParent();
}

return $parent->getApiKey();
}

/**
* Set the API key that should be used to request this endpoint.
*
* @param string $apiKey
* @return static
*/
public function auth($apiKey) {
$this->apiKey = $apiKey;

return $this;
}
}
8 changes: 7 additions & 1 deletion src/V2/Authentication/AuthenticationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ function __construct( IAuthenticatedEndpoint $endpoint ) {
* @return MessageInterface|RequestInterface
*/
public function onRequest( RequestInterface $request ) {
return $request->withHeader( 'Authorization', 'Bearer ' . $this->getEndpoint()->getApiKey() );
$apiKey = $this->getEndpoint()->getApiKey();

if($apiKey) {
return $request->withHeader( 'Authorization', 'Bearer '.$apiKey );
}

return $request;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/V2/Authentication/IAuthenticatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ interface IAuthenticatedEndpoint extends IEndpoint {
* @return string
*/
public function getApiKey();


/**
* Set the API key that should be used to request this endpoint.
*
* @param string $apiKey
* @return static
*/
public function auth($apiKey);
}
25 changes: 16 additions & 9 deletions src/V2/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

abstract class Endpoint implements IEndpoint {
/** @var GW2Api $api */
protected $api;
abstract class Endpoint implements IEndpoint, IParent {
/** @var GW2Api $parent */
protected $parent;

/** @var ApiHandler[] */
protected $handlers = [];

/**
* @param GW2Api $api
* @param IParent $parent
*/
public function __construct( GW2Api $api ) {
$this->api = $api;
public function __construct( IParent $parent ) {
$this->parent = $parent;

$this->api->attachRegisteredHandlers( $this );
$this->getApi()->attachRegisteredHandlers( $this );
}

/**
Expand All @@ -36,11 +36,18 @@ protected function getClient() {
return $this->getApi()->getClient();
}

/**
* @return IParent
*/
public function getParent() {
return $this->parent;
}

/**
* @return GW2Api
*/
protected function getApi() {
return $this->api;
public function getApi() {
return $this->getParent()->getApi();
}

/**
Expand Down
23 changes: 8 additions & 15 deletions src/V2/Endpoint/Account/AccountEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

namespace GW2Treasures\GW2Api\V2\Endpoint\Account;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\Authentication\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Authentication\IAuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint;

class AccountEndpoint extends Endpoint implements IAuthenticatedEndpoint {
use AuthenticatedEndpoint;

public function __construct( GW2Api $api, $apiKey ) {
parent::__construct( $api );

$this->apiKey = $apiKey;
}

/**
* {@inheritdoc}
*/
Expand All @@ -38,7 +31,7 @@ public function get() {
* @return AchievementEndpoint
*/
public function achievements() {
return new AchievementEndpoint( $this->api, $this->apiKey );
return new AchievementEndpoint( $this->parent );
}

/**
Expand All @@ -47,7 +40,7 @@ public function achievements() {
* @return BankEndpoint
*/
public function bank() {
return new BankEndpoint( $this->api, $this->apiKey );
return new BankEndpoint( $this->parent );
}

/**
Expand All @@ -56,7 +49,7 @@ public function bank() {
* @return DyeEndpoint
*/
public function dyes() {
return new DyeEndpoint( $this->api, $this->apiKey );
return new DyeEndpoint( $this->parent );
}

/**
Expand All @@ -65,7 +58,7 @@ public function dyes() {
* @return InventoryEndpoint
*/
public function inventory() {
return new InventoryEndpoint( $this->api, $this->apiKey );
return new InventoryEndpoint( $this->parent );
}

/**
Expand All @@ -74,7 +67,7 @@ public function inventory() {
* @return MaterialEndpoint
*/
public function materials() {
return new MaterialEndpoint( $this->api, $this->apiKey );
return new MaterialEndpoint( $this->parent );
}

/**
Expand All @@ -83,7 +76,7 @@ public function materials() {
* @return MiniEndpoint
*/
public function minis() {
return new MiniEndpoint( $this->api, $this->apiKey );
return new MiniEndpoint( $this->parent );
}

/**
Expand All @@ -92,7 +85,7 @@ public function minis() {
* @return SkinEndpoint
*/
public function skins() {
return new SkinEndpoint( $this->api, $this->apiKey );
return new SkinEndpoint( $this->parent );
}

/**
Expand All @@ -101,6 +94,6 @@ public function skins() {
* @return WalletEndpoint
*/
public function wallet() {
return new WalletEndpoint( $this->api, $this->apiKey );
return new WalletEndpoint( $this->parent );
}
}
7 changes: 0 additions & 7 deletions src/V2/Endpoint/Account/AchievementEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

namespace GW2Treasures\GW2Api\V2\Endpoint\Account;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\Authentication\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Authentication\IAuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint;

class AchievementEndpoint extends Endpoint implements IAuthenticatedEndpoint {
use AuthenticatedEndpoint;

public function __construct( GW2Api $api, $apiKey ) {
parent::__construct( $api );

$this->apiKey = $apiKey;
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 0 additions & 7 deletions src/V2/Endpoint/Account/BankEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

namespace GW2Treasures\GW2Api\V2\Endpoint\Account;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\Authentication\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Authentication\IAuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint;

class BankEndpoint extends Endpoint implements IAuthenticatedEndpoint {
use AuthenticatedEndpoint;

public function __construct( GW2Api $api, $apiKey ) {
parent::__construct( $api );

$this->apiKey = $apiKey;
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 0 additions & 7 deletions src/V2/Endpoint/Account/DyeEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

namespace GW2Treasures\GW2Api\V2\Endpoint\Account;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\Authentication\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Authentication\IAuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint;

class DyeEndpoint extends Endpoint implements IAuthenticatedEndpoint {
use AuthenticatedEndpoint;

public function __construct( GW2Api $api, $apiKey ) {
parent::__construct( $api );

$this->apiKey = $apiKey;
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 0 additions & 7 deletions src/V2/Endpoint/Account/InventoryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

namespace GW2Treasures\GW2Api\V2\Endpoint\Account;

use GW2Treasures\GW2Api\GW2Api;
use GW2Treasures\GW2Api\V2\Authentication\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Authentication\IAuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Endpoint;

class InventoryEndpoint extends Endpoint implements IAuthenticatedEndpoint {
use AuthenticatedEndpoint;

public function __construct( GW2Api $api, $apiKey ) {
parent::__construct( $api );

$this->apiKey = $apiKey;
}

/**
* {@inheritdoc}
*/
Expand Down
Loading