Permalink
Browse files

Added auction data support

  • Loading branch information...
1 parent ca63aea commit 1654de9f9a11a09a2ecde22a9bd765a1ee9bf681 @AngryDeveloper committed Apr 1, 2012
@@ -1,4 +1,5 @@
<?php
+
/**
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -21,18 +22,12 @@
namespace BattleNet\Api;
-use BattleNet\Http\HttpException;
-
-use BattleNet\Http\Adapter\AbstractAdapter;
-
-use BattleNet\Http\Adapter\CurlAdapter;
-
-use BattleNet\Http\Adapter\FallbackAdapter;
-
+use BattleNet\Http\HttpException;
+use BattleNet\Http\Adapter\AbstractAdapter;
+use BattleNet\Http\Adapter\CurlAdapter;
+use BattleNet\Http\Adapter\FallbackAdapter;
use BattleNet\Cache\Cache;
-
-use InvalidArgumentException;
-
+use InvalidArgumentException;
/**
* Primary API class that all children source APIs extend. Provides functionality for
@@ -44,8 +39,7 @@
*
* @abstract
*/
-abstract class AbstractApi
-{
+abstract class AbstractApi {
/**
* Identification for this library
*/
@@ -54,7 +48,7 @@
/**
* Official WoW API URL.
*/
- const API_URL = 'http://%s.battle.net/api/%s/%s';
+ const API_URL = 'http://%s.battle.net/%s';
/**
* Official WoW API URL for China
@@ -146,8 +140,7 @@
* @return void
* @constructor
*/
- public function __construct(array $config = array())
- {
+ public function __construct(array $config = array()) {
$this->loadConfig($config);
}
@@ -160,25 +153,23 @@ public function __construct(array $config = array())
* @return void
* @throws ApiException
*/
- protected function loadConfig(array $config = array())
- {
- foreach ( $config as $key => $value ) {
- $method = 'set'.ucfirst($key);
- if ( method_exists($this, $method) ) {
+ protected function loadConfig(array $config = array()) {
+ foreach ($config as $key => $value) {
+ $method = 'set' . ucfirst($key);
+ if (method_exists($this, $method)) {
$this->$method($value);
} else {
- throw new InvalidArgumentException('Unknown config parameter "'.$key.'"');
+ throw new InvalidArgumentException('Unknown config parameter "' . $key . '"');
}
}
}
- public function setHttpAdapter($adapter)
- {
- if ( is_string($adapter) ) {
- $adapterName = '\BattleNet\\Http\\Adapter\\'.ucfirst($adapter).'Adapter';
+ public function setHttpAdapter($adapter) {
+ if (is_string($adapter)) {
+ $adapterName = '\BattleNet\\Http\\Adapter\\' . ucfirst($adapter) . 'Adapter';
$adapter = new $adapterName();
}
- if ( !($adapter instanceof AbstractAdapter) ) {
+ if (!($adapter instanceof AbstractAdapter)) {
throw new HttpException('Unknown HTTP Adapter');
}
$this->_httpAdapter = $adapter;
@@ -191,8 +182,7 @@ public function setHttpAdapter($adapter)
* @param Cache $cache
* @return void
*/
- public function setCache(Cache $cache)
- {
+ public function setCache(Cache $cache) {
$this->_cache = $cache;
}
@@ -203,8 +193,7 @@ public function setCache(Cache $cache)
* @param string $region
* @return void
*/
- public function setRegion($region)
- {
+ public function setRegion($region) {
$this->_checkAvailabilityRegionAndLocale($region, $this->locale);
$this->region = $region;
$this->_setApiUrl();
@@ -216,10 +205,9 @@ public function setRegion($region)
* @access private
* @return void
*/
- private function _setApiUrl()
- {
+ private function _setApiUrl() {
$url = self::API_URL;
- if ( $this->region == 'cn' ) {
+ if ($this->region == 'cn') {
$url = self::API_URL_CN;
}
$this->url = $url;
@@ -232,8 +220,7 @@ private function _setApiUrl()
* @param string $locale
* @return void
*/
- public function setLocale($locale)
- {
+ public function setLocale($locale) {
$this->_checkAvailabilityRegionAndLocale($this->region, $locale);
$this->locale = $locale;
}
@@ -247,14 +234,13 @@ public function setLocale($locale)
* @throws ApiException
* @return boolean
*/
- private function _checkAvailabilityRegionAndLocale($region, $locale)
- {
- if ( $region ) {
- if ( !array_key_exists($region, $this->_regionWhitelist) ) {
+ private function _checkAvailabilityRegionAndLocale($region, $locale) {
+ if ($region) {
+ if (!array_key_exists($region, $this->_regionWhitelist)) {
throw new InvalidArgumentException(sprintf('Region "%s" is not available.', $region));
}
- if ( $locale ) {
- if ( !in_array($locale, $this->_regionWhitelist[$region]) ) {
+ if ($locale) {
+ if (!in_array($locale, $this->_regionWhitelist[$region])) {
throw new InvalidArgumentException(sprintf('Locale "%s" is not available for region "%s".', $locale, $region));
}
}
@@ -269,8 +255,7 @@ private function _checkAvailabilityRegionAndLocale($region, $locale)
* @param string $privateApiKey
* @return void
*/
- public function setPrivateApiKey($privateApiKey)
- {
+ public function setPrivateApiKey($privateApiKey) {
$this->privateApiKey = $privateApiKey;
}
@@ -281,51 +266,48 @@ public function setPrivateApiKey($privateApiKey)
* @param string $publicApiKey
* @return void
*/
- public function setPublicApiKey($publicApiKey)
- {
+ public function setPublicApiKey($publicApiKey) {
$this->publicApiKey = $publicApiKey;
}
- public function request(AbstractCall $call)
- {
+ public function request(AbstractCall $call) {
$method = $call->getMethod();
$date = date(DATE_RFC2822);
- $url = sprintf($this->url, $this->region, $this->game, $call->getPath());
-
+ $url = sprintf($this->url, $this->region, $call->getPath());
+
$options = array();
$options['headers']['User-Agent'] = self::USER_AGENT;
$options['headers']['Expect'] = '';
$options['headers']['Accept'] = 'application/json';
$options['headers']['Content-Type'] = 'application/json';
-
- if ( $this->publicApiKey && $this->privateApiKey ) {
+
+ if ($this->publicApiKey && $this->privateApiKey) {
$options['headers']['Date'] = $date;
$options['headers']['Authorization'] = $this->_signRequest($method, $date, $url);
}
-
+
$params = $call->getQueryParams();
- if ( $this->locale ) {
+ if ($this->locale) {
$params['locale'] = $this->locale;
}
-
+
return $this->getHttpAdapter()->request($method, $url, $params, $options);
}
-
- protected function getHttpAdapter()
- {
- if ( !$this->_httpAdapter ) {
+
+ protected function getHttpAdapter() {
+ if (!$this->_httpAdapter) {
if (extension_loaded('curl')) {
$this->setHttpAdapter('curl');
} else {
$this->setHttpAdapter('fallback');
}
- }
- if ( $this->_cache ) {
+ }
+ if ($this->_cache) {
$this->_httpAdapter->setCache($this->_cache);
}
return $this->_httpAdapter;
}
-
+
/**
* Build the Authorization signature
*
@@ -335,18 +317,18 @@ protected function getHttpAdapter()
* @param string $uri
* @return string
*/
- private function _signRequest($method, $date, $url)
- {
+ private function _signRequest($method, $date, $url) {
$data = $method . "\n" . $date . "\n" . $url . "\n";
- $encodedData = base64_encode(hash_hmac('sha1',$this->privateApiKey,$data));
+ $encodedData = base64_encode(hash_hmac('sha1', $this->privateApiKey, $data));
$signature = strtr(self::API_SIGNATURE, array(
- '{publicApiKey}' => $this->publicApiKey,
- '{data}' => $encodedData
- )
+ '{publicApiKey}' => $this->publicApiKey,
+ '{data}' => $encodedData
+ )
);
return $signature;
}
+
}
@@ -37,7 +37,7 @@ class ArenaCall
/**
* {@inheritdoc}
*/
- protected $_path = 'arena/{realm}/{size}/{name}';
+ protected $_path = 'api/wow/arena/{realm}/{size}/{name}';
/**
* name of the realm
@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+namespace BattleNet\Api\Wow\Call;
+
+use BattleNet\Api\AbstractCall;
+use InvalidArgumentException;
+
+/**
+ * Call for the WoW Auction Data Api
+ *
+ * @author Valentin Torikian <valentin.torikian@gmail.com>
+ * @version 0.1.0
+ */
+class AuctionCall extends AbstractCall {
+
+ /**
+ * {@inheritdoc}
+ */
+ protected $_path = 'api/wow/auction/data/{slug}';
+
+ /**
+ * Slug of the requested realm
+ *
+ * @access protected
+ * @var string
+ */
+ protected $slug;
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param string $slug
+ * @return void
+ */
+ public function __construct($slug) {
+ $this->setSlug($slug);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getPath() {
+ return str_replace('{slug}', $this->slug, $this->_path);
+ }
+
+ /**
+ * Set the slug
+ *
+ * @access public
+ * @param $slug
+ * @throws ApiException when the slug is empty or not a string
+ */
+ public function setSlug($slug) {
+ if (empty($slug) || !is_string($slug)) {
+ throw new InvalidArgumentException(sprintf('Item ID "%s" invalid for %s.', $slug, __CLASS__));
+ }
+ $this->slug = $slug;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 1654de9

Please sign in to comment.