Skip to content
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
4 changes: 2 additions & 2 deletions docs/examples/CommunityOAuthExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Include composer autoload file
require_once __DIR__.'/../../vendor/autoload.php';

// Create a new Blizzard client with Blizzard API key and secret
$client = new \BlizzardApi\BlizzardClient('apiKey', 'apiSecret');
// Create a new Blizzard client with registered Blizzard Client ID and Client Secret
$client = new \BlizzardApi\BlizzardClient('clientId', 'clientSecret');

// Create a new Diablo service with configured Blizzard client
$diablo = new \BlizzardApi\Service\Diablo($client);
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/DiabloExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Include composer autoload file
require_once __DIR__.'/../../vendor/autoload.php';

// Create a new Blizzard client with Blizzard API key and secret
$client = new \BlizzardApi\BlizzardClient('apiKey', 'apiSecret');
// Create a new Blizzard client with registered Blizzard Client ID and Client Secret
$client = new \BlizzardApi\BlizzardClient('clientId', 'clientSecret');

// Create a new Diablo service with configured Blizzard client
$diablo = new \BlizzardApi\Service\Diablo($client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Include composer autoload file
require_once __DIR__.'/../../vendor/autoload.php';

// Create a new Blizzard client with Blizzard API key and secret
$client = new \BlizzardApi\BlizzardClient('apiKey', 'apiSecret');
// Create a new Blizzard client with registered Blizzard Client ID and Client Secret
$client = new \BlizzardApi\BlizzardClient('clientId', 'clientSecret');

// Create a new GameData service with configured Blizzard client
$gameData = new \BlizzardApi\Service\GameData($client);
$gameData = new \BlizzardApi\Service\DiabloGameData($client);

// Use API method for getting specific data
$response = $gameData->getEraLeaderboard(1, 'rift-barbarian');
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/StarcraftExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Include composer autoload file
require_once __DIR__.'/../../vendor/autoload.php';

// Create a new Blizzard client with Blizzard API key and secret
$client = new \BlizzardApi\BlizzardClient('apiKey', 'apiSecret');
// Create a new Blizzard client with registered Blizzard Client ID and Client Secret
$client = new \BlizzardApi\BlizzardClient('clientId', 'clientSecret');

// Create a new Starcraft service with configured Blizzard client
$starcraft = new \BlizzardApi\Service\Starcraft($client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
// Include composer autoload file
require_once __DIR__.'/../../vendor/autoload.php';

// Create a new Blizzard client with Blizzard API key and secret
$client = new \BlizzardApi\BlizzardClient('apiKey', 'apiSecret');
// Create a new Blizzard client with registered Blizzard Client ID and Client Secret
$client = new \BlizzardApi\BlizzardClient('clientId', 'clientSecret');

// Create a new World Of Warcraft service with configured Blizzard client
$wow = new \BlizzardApi\Service\WorldOfWarcraft($client);
$wow = new \BlizzardApi\Service\WorldOfWarcraftCommunity($client);

// Use API method for getting specific data
$response = $wow->getGuild('test-realm', 'test-guild', [
'fields' => 'achievements,challenge',
]);
$response = $wow->getGuildProfile('test-realm', 'test-guild', implode(',', ['achievements', 'challenge']));

// Accessing response status code
$response->getStatusCode();
Expand Down
125 changes: 57 additions & 68 deletions src/BlizzardClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace BlizzardApi;

use BlizzardApi\Service\OAuth;
use BlizzardApi\Tokens\Access;
use GuzzleHttp\Client;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -15,7 +15,8 @@
*/
class BlizzardClient
{
const API_URL_PATTERN = 'https://region.api.battle.net';
const API_URL_PATTERN = 'https://region.api.blizzard.com';
const CHINA_API_URL = 'https://gateway.battlenet.com.cn';
const API_ACCESS_TOKEN_URL_PATTERN = 'https://region.battle.net/oauth/token';

/**
Expand All @@ -29,9 +30,14 @@ class BlizzardClient
private $apiAccessTokenUrl;

/**
* @var string $apiKey API key
* @var string $clientId Client ID
*/
private $apiKey;
private $clientId;

/**
* @var string $clientSecret Client Secret
*/
private $clientSecret;

/**
* @var Access[] $accessTokens Access tokens
Expand All @@ -51,27 +57,27 @@ class BlizzardClient
/**
* Constructor
*
* @param string $apiKey API key
* @param string $apiSecret API Secret key
* @param string $region Region
* @param string $locale Locale
* @param string $clientId Access token
* @param string $clientSecret API Secret key
* @param string $region Region
* @param string $locale Locale
*/
public function __construct($apiKey, $apiSecret, $region = 'us', $locale = 'en_us')
public function __construct($clientId, $clientSecret, $region = 'us', $locale = 'en_us')
{
$options = [
'apiKey' => $apiKey,
'apiSecret' => $apiSecret,
'region' => strtolower($region),
'locale' => strtolower($locale),
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'region' => strtolower($region),
'locale' => strtolower($locale),
];

$resolver = new OptionsResolver();
$this->configureOptions($resolver, $options['region']);

$options = $resolver->resolve($options);

$this->apiKey = $options['apiKey'];
$this->apiSecret = $options['apiSecret'];
$this->clientId = $options['clientId'];
$this->clientSecret = $options['clientSecret'];
$this->region = $options['region'];
$this->locale = $options['locale'];

Expand Down Expand Up @@ -100,49 +106,49 @@ public function getApiAccessTokenUrl()
}

/**
* Get api key
* Get client ID
*
* @return string Api key
* @return string Client ID
*/
public function getApiKey()
public function getClientId()
{
return $this->apiKey;
return $this->clientId;
}

/**
* Set api key
* Set client ID
*
* @param string $apiKey Api key
* @param string $clientId Client ID
*
* @return $this
*/
public function setApiKey($apiKey)
public function setClientId($clientId)
{
$this->apiKey = $apiKey;
$this->clientId = $clientId;

return $this;
}

/**
* Get api secret
* Get client secret
*
* @return string Api secret
* @return string Client secret
*/
public function getApiSecret()
public function getClientSecret()
{
return $this->apiSecret;
return $this->clientSecret;
}

/**
* Set api secret
* Set client secret
*
* @param string $apiSecret Api secret
* @param string $clientSecret Client secret
*
* @return $this
*/
public function setApiSecret($apiSecret)
public function setClientSecret($clientSecret)
{
$this->apiSecret = $apiSecret;
$this->clientSecret = $clientSecret;

return $this;
}
Expand All @@ -154,7 +160,7 @@ public function setApiSecret($apiSecret)
*/
public function getRegion()
{
return strtolower($this->region);
return $this->region;
}

/**
Expand All @@ -181,7 +187,7 @@ public function setRegion($region)
*/
public function getLocale()
{
return strtolower($this->locale);
return $this->locale;
}

/**
Expand All @@ -202,6 +208,9 @@ public function setLocale($locale)
* Get access token
*
* @return null|string Access token
*
* @throws \BlizzardApi\Tokens\Exceptions\Expired
* @throws \HttpResponseException
*/
public function getAccessToken()
{
Expand All @@ -210,7 +219,9 @@ public function getAccessToken()
}

if (!array_key_exists($this->getRegion(), $this->accessTokens)) {
$this->accessTokens[$this->getRegion()] = $this->requestAccessToken();
$oauth = new OAuth($this);

$this->accessTokens[$this->getRegion()] = $oauth->requestAccessToken();
}

return $this->accessTokens[$this->getRegion()]->getToken();
Expand Down Expand Up @@ -241,7 +252,11 @@ public function setAccessToken(Access $accessToken)
*/
private function updateApiUrl($region)
{
$this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN);
if ($region === 'cn') {
$this->apiUrl = self::CHINA_API_URL;
} else {
$this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN);
}

return $this;
}
Expand Down Expand Up @@ -284,38 +299,12 @@ private function configureOptions(OptionsResolver $resolver, $region)
);
}

$resolver->setRequired(['apiKey', 'apiSecret', 'region', 'locale'])
->setAllowedTypes('apiKey', 'string')
->setAllowedTypes('apiSecret', 'string')
->setAllowedTypes('region', 'string')
->setAllowedValues('region', array_keys(GeoData::$list))
->setAllowedTypes('locale', 'string')
->setAllowedValues('locale', $locales);
}

/**
* Request an Access Token from Blizzard
*
* @return Access
*
* @throws \HttpResponseException
*/
protected function requestAccessToken()
{
$options = [
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $this->getApiKey(),
'client_secret' => $this->getApiSecret(),
],
];

$result = (new Client())->post($this->getApiAccessTokenUrl(), $options);

if (200 === $result->getStatusCode()) {
return Access::fromJson(json_decode($result->getBody()->getContents()));
} else {
throw new \HttpResponseException('Invalid Response');
}
$resolver->setRequired(['clientId', 'clientSecret', 'region', 'locale'])
->setAllowedTypes('clientId', 'string')
->setAllowedTypes('clientSecret', 'string')
->setAllowedTypes('region', 'string')
->setAllowedValues('region', array_keys(GeoData::$list))
->setAllowedTypes('locale', 'string')
->setAllowedValues('locale', $locales);
}
}
13 changes: 6 additions & 7 deletions src/GeoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ class GeoData
static public $list = [
'eu' => [
'en_gb',
'de_de',
'es_es',
'fr_fr',
'it_it',
'pl_pl',
'pt_pt',
'ru_ru',
'de_de',
'pt_pt',
'it_it',
],
'us' => [
'en_us',
'pt_br',
'es_mx',
'pt_br',
],
'kr' => [
'ko_kr',
],
'tw' => [
'zh_tw',
],
'sea' => [
'en_us',
'cn' => [
'zh_cn',
],
];
}
2 changes: 1 addition & 1 deletion src/Service/CommunityOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Psr\Http\Message\ResponseInterface;

/**
* Class CommunityOAuth
* Class Community OAuth
*
* @author Oleg Kachinsky <logansoleg@gmail.com>
*/
Expand Down
Loading