From 7991eefc780eb5f0a48ff8ad138f7dfe37a24b7b Mon Sep 17 00:00:00 2001 From: michel-pi Date: Tue, 3 Dec 2019 06:58:27 +0100 Subject: [PATCH] making endpoints use the same http client and api key --- .../Endpoints/CosmeticsEndpoint.php | 29 +++++--- .../Endpoints/CreatorCodeEndpoint.php | 21 ++++-- src/Components/Endpoints/NewsEndpoint.php | 23 +++++-- src/Components/Endpoints/ShopEndpoint.php | 19 +++++- src/Components/HttpClient.php | 24 ------- src/Config/Config.php | 46 ------------- src/FortniteApi.php | 68 ++++++++++++++++--- test/index.php | 2 +- 8 files changed, 133 insertions(+), 99 deletions(-) delete mode 100644 src/Config/Config.php diff --git a/src/Components/Endpoints/CosmeticsEndpoint.php b/src/Components/Endpoints/CosmeticsEndpoint.php index 13eb89e..61c747e 100644 --- a/src/Components/Endpoints/CosmeticsEndpoint.php +++ b/src/Components/Endpoints/CosmeticsEndpoint.php @@ -2,18 +2,31 @@ namespace FortniteApi\Components\Endpoints; -use FortniteApi\Components\HttpClient; use FortniteApi\Components\Objects\Cosmetic; use FortniteApi\Components\Tasks\CosmeticArrayTask; use FortniteApi\Components\Tasks\CosmeticTask; use FortniteApi\FortniteApi; use FortniteApi\FortniteApiError; +use GuzzleHttp\Client; + /** * Provides access to the /cosmetics/ endpoint */ class CosmeticsEndpoint { + /** + * Undocumented variable + * + * @var Client + */ + private $httpClient; + + public function __construct($httpClient) + { + $this->httpClient = $httpClient; + } + /** * Returns the requested cosmetic. * @@ -64,9 +77,9 @@ public function getAsync($cosmeticId, $language = null) } if (count($query) == 0) { - $promise = HttpClient::getInstance()->getAsync($path); + $promise = $this->httpClient->getAsync($path); } else { - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); } @@ -116,9 +129,9 @@ public function getAllAsync($language = null) } if (count($query) == 0) { - $promise = HttpClient::getInstance()->getAsync($path); + $promise = $this->httpClient->getAsync($path); } else { - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); } @@ -167,7 +180,7 @@ public function searchAsync($query, $language = null) $query["language"] = $language; } - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); @@ -215,7 +228,7 @@ public function searchAllAsync($query, $language = null) $query["language"] = $language; } - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); @@ -277,7 +290,7 @@ public function searchIdsAsync($ids, $language = null) $query["language"] = $language; } - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); diff --git a/src/Components/Endpoints/CreatorCodeEndpoint.php b/src/Components/Endpoints/CreatorCodeEndpoint.php index 66389aa..f880ccf 100644 --- a/src/Components/Endpoints/CreatorCodeEndpoint.php +++ b/src/Components/Endpoints/CreatorCodeEndpoint.php @@ -2,16 +2,29 @@ namespace FortniteApi\Components\Endpoints; -use FortniteApi\Components\HttpClient; use FortniteApi\Components\Tasks\CreatorCodeArrayTask; use FortniteApi\Components\Tasks\CreatorCodeTask; use FortniteApi\FortniteApiError; +use GuzzleHttp\Client; + /** * Provides access to the /creatorcode endpoint. */ class CreatorCodeEndpoint { + /** + * Undocumented variable + * + * @var Client + */ + private $httpClient; + + public function __construct($httpClient) + { + $this->httpClient = $httpClient; + } + /** * Returns the creator code data for a given slug. * @@ -51,7 +64,7 @@ public function getAsync($slug) "slug" => $slug ]; - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); @@ -97,7 +110,7 @@ public function searchAsync($slug) "slug" => $slug ]; - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); @@ -143,7 +156,7 @@ public function searchAllAsync($slug) "slug" => $slug ]; - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); diff --git a/src/Components/Endpoints/NewsEndpoint.php b/src/Components/Endpoints/NewsEndpoint.php index 1190631..3f33143 100644 --- a/src/Components/Endpoints/NewsEndpoint.php +++ b/src/Components/Endpoints/NewsEndpoint.php @@ -2,17 +2,30 @@ namespace FortniteApi\Components\Endpoints; -use FortniteApi\Components\HttpClient; use FortniteApi\Components\Tasks\NewsEntryTask; use FortniteApi\Components\Tasks\NewsTask; use FortniteApi\FortniteApi; use FortniteApi\FortniteApiError; +use GuzzleHttp\Client; + /** * Provides access to the /news/ endpoint. */ class NewsEndpoint { + /** + * Undocumented variable + * + * @var Client + */ + private $httpClient; + + public function __construct($httpClient) + { + $this->httpClient = $httpClient; + } + /** * Returns the data of the current battle royale, save the world and creative news. * @@ -55,9 +68,9 @@ public function getAsync($language = null) } if (count($query) == 0) { - $promise = HttpClient::getInstance()->getAsync($path); + $promise = $this->httpClient->getAsync($path); } else { - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); } @@ -168,9 +181,9 @@ private function internalGetAsync($section, $language = null) } if (count($query) == 0) { - $promise = HttpClient::getInstance()->getAsync($path); + $promise = $this->httpClient->getAsync($path); } else { - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); } diff --git a/src/Components/Endpoints/ShopEndpoint.php b/src/Components/Endpoints/ShopEndpoint.php index e7d1096..1dedae8 100644 --- a/src/Components/Endpoints/ShopEndpoint.php +++ b/src/Components/Endpoints/ShopEndpoint.php @@ -2,16 +2,29 @@ namespace FortniteApi\Components\Endpoints; -use FortniteApi\Components\HttpClient; use FortniteApi\Components\Tasks\ShopTask; use FortniteApi\FortniteApi; use FortniteApi\FortniteApiError; +use GuzzleHttp\Client; + /** * Provides access to the /shop/ endpoint. */ class ShopEndpoint { + /** + * Undocumented variable + * + * @var Client + */ + private $httpClient; + + public function __construct($httpClient) + { + $this->httpClient = $httpClient; + } + /** * Returns the current battle royale shop. * @@ -54,9 +67,9 @@ public function getAsync($language = null) } if (count($query) == 0) { - $promise = HttpClient::getInstance()->getAsync($path); + $promise = $this->httpClient->getAsync($path); } else { - $promise = HttpClient::getInstance()->getAsync($path, [ + $promise = $this->httpClient->getAsync($path, [ "query" => $query ]); } diff --git a/src/Components/HttpClient.php b/src/Components/HttpClient.php index af76098..0a38fa8 100644 --- a/src/Components/HttpClient.php +++ b/src/Components/HttpClient.php @@ -2,18 +2,8 @@ namespace FortniteApi\Components; -use GuzzleHttp\Client; -use FortniteApi\Config\Config; - class HttpClient { - /** - * Undocumented variable - * - * @var Client - */ - private static $_client; - public static function isSuccess($statusCode) { if (empty($statusCode) || !is_int($statusCode)) { @@ -22,18 +12,4 @@ public static function isSuccess($statusCode) return $statusCode >= 200 && $statusCode < 400; } - - /** - * Undocumented function - * - * @return Client - */ - public static function getInstance() - { - if (empty(self::$_client)) { - self::$_client = new Client(Config::getHttpClientConfig()); - } - - return self::$_client; - } } diff --git a/src/Config/Config.php b/src/Config/Config.php deleted file mode 100644 index 6d542c9..0000000 --- a/src/Config/Config.php +++ /dev/null @@ -1,46 +0,0 @@ - "https://fortnite-api.com", - "allow_redirects" => true, - "connect_timeout" => 30, - "timeout" => 30 - ]; - - public static function getSupportedLanguages() - { - return self::$supportedLanguages; - } - - public static function getHttpClientConfig() - { - return self::$httpClientConfig; - } - - public static function getBaseUri() - { - return self::$httpClientConfig["base_uri"]; - } -} diff --git a/src/FortniteApi.php b/src/FortniteApi.php index d0d6c91..ac0bc91 100644 --- a/src/FortniteApi.php +++ b/src/FortniteApi.php @@ -6,13 +6,28 @@ use FortniteApi\Components\Endpoints\CreatorCodeEndpoint; use FortniteApi\Components\Endpoints\NewsEndpoint; use FortniteApi\Components\Endpoints\ShopEndpoint; -use FortniteApi\Config\Config; + +use GuzzleHttp\Client; /** * Provides access to https://fortnite-api.com */ class FortniteApi { + /** + * Undocumented variable + * + * @var string|null + */ + private $apiKey; + + /** + * Undocumented variable + * + * @var Client + */ + private $httpClient; + /** * @inheritDoc * @@ -42,12 +57,33 @@ class FortniteApi /** * Constructs a new FortniteApi instance. */ - public function __construct() + public function __construct($apiKey) + { + if ($apiKey === null || $apiKey === false) { + $apiKey = ""; + } + + $this->httpClient = new Client([ + "base_uri" => self::getBaseUri(), + "allow_redirects" => true, + "connect_timeout" => 30, + "timeout" => 30, + "headers" => [ + "x-api-key" => $apiKey + ] + ]); + + $this->apiKey = $apiKey; + + $this->cosmetics = new CosmeticsEndpoint($this->httpClient); + $this->shop = new ShopEndpoint($this->httpClient); + $this->news = new NewsEndpoint($this->httpClient); + $this->creatorCode = new CreatorCodeEndpoint($this->httpClient); + } + + public function getApiKey() { - $this->cosmetics = new CosmeticsEndpoint(); - $this->shop = new ShopEndpoint(); - $this->news = new NewsEndpoint(); - $this->creatorCode = new CreatorCodeEndpoint(); + return $this->apiKey; } /** @@ -57,7 +93,7 @@ public function __construct() */ public static function getBaseUri() { - return Config::getBaseUri(); + return "https://fortnite-api.com"; } /** @@ -67,6 +103,22 @@ public static function getBaseUri() */ public static function getSupportedLanguages() { - return Config::getSupportedLanguages(); + return [ + "ar", + "de", + "en", + "es", + "es-419", + "fr", + "it", + "ja", + "ko", + "pl", + "pt-BR", + "ru", + "tr", + "zh-CN", + "zh-Hant" + ]; } } diff --git a/test/index.php b/test/index.php index 6350612..748ef5b 100644 --- a/test/index.php +++ b/test/index.php @@ -8,7 +8,7 @@ header("Content-Type: application/json"); -$api = new FortniteApi(); +$api = new FortniteApi(null); $awaitables = [ "cosmetic" => $api->cosmetics->getAsync("bannertoken_001_cattus"),