Skip to content

Commit

Permalink
Updated some class and objects
Browse files Browse the repository at this point in the history
Updated :
-API class with new methods
-Cache optimized
-New objects
  • Loading branch information
MrFiregore authored Mar 5, 2018
1 parent 7bb7777 commit bec687e
Show file tree
Hide file tree
Showing 34 changed files with 1,451 additions and 448 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
"illuminate/support": "~5.0",
"league/event": "^2.1",
"vlucas/phpdotenv": "^2.4",
"kint-php/kint": "^2.0",
"thadafinser/user-agent-parser" : "^2.0",
"zendframework/zendframework": "^3.0"
"kint-php/kint": "^2.0"
},
"autoload":{
"psr-4":{
Expand Down
279 changes: 206 additions & 73 deletions src/Api.php

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/CRCache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?php
/**************************************************************************************************************************************************************************************************************************************************************
* *
* Copyright (c) 2018 by Firegore (https://firegore.es) (git:firegore2). *
* This file is part of clash-royale-php. *
* *
* clash-royale-php is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. *
* clash-royale-php is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Affero General Public License for more details. *
* You should have received a copy of the GNU General Public License along with clash-royale-php. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************************************************************************************************************************************************************************************************/

namespace CR;

/**
Expand Down Expand Up @@ -55,6 +68,7 @@ static public function setPath(string $path)
self::$path = $path;
return $this;
}

/**
* Creates a directory
*
Expand Down Expand Up @@ -121,7 +135,7 @@ protected function isRemote($file)
* @return bool
* @throws \Exception
*/
protected function checkConditions($cacheFile, array $conditions = array())
protected static function checkConditions($cacheFile, array $conditions = array())
{
// Implicit condition: the cache file should exist
if (!file_exists($cacheFile)) {
Expand Down Expand Up @@ -176,7 +190,6 @@ protected function checkConditions($cacheFile, array $conditions = array())
static public function exists($filename, array $conditions = array())
{
$cacheFile = self::getCacheFile($filename, true);

return self::checkConditions($cacheFile, $conditions);
}
/**
Expand Down
85 changes: 72 additions & 13 deletions src/CRClient.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
<?php
/**************************************************************************************************************************************************************************************************************************************************************
* *
* Copyright (c) 2018 by Firegore (https://firegore.es) (git:firegore2). *
* This file is part of clash-royale-php. *
* *
* clash-royale-php is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. *
* clash-royale-php is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Affero General Public License for more details. *
* You should have received a copy of the GNU General Public License along with clash-royale-php. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************************************************************************************************************************************************************************************************/


namespace CR;

use GuzzleHttp\TransferStats;

use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;
use CR\Exceptions\CRSDKException;
Expand All @@ -16,10 +31,10 @@ class CRClient
/**
* @const string CR Bot API URL.
*/
const BASE_URL = 'http://api.cr-api.com/';
const BASE_URL = 'http://api.cr-api.com';

/**
* @var HttpClientInterface|null HTTP Client
* @var HttpClientInterface|GuzzleHttpClient HTTP Client
*/
protected $httpClientHandler;

Expand Down Expand Up @@ -63,6 +78,33 @@ public function getBaseUrl()
return static::BASE_URL;
}

/**
* Return the API url
* @method getUrl
* @param CRRequest $request
* @return string the API url
*/

public function getUrl(CRRequest $request)
{
$url = $this->getBaseUrl().$request->getEndpoint();

if (strpos($url,":tag") !== false) {
$params = ( (empty($request->getParams())) ? "" : implode(",",$request->getParams()));
$url = str_replace(":tag",$params,$url);
}

if (!empty($request->getQuerys())) {
$url .= "?";
foreach ($request->getQuerys() as $key => $query) {
$url .= $key."=".( is_array($query) ? implode(",",$query) : $query)."&";
}
$url = substr($url,0,-1);
}

return $url;
}

/**
* Prepares the API request for sending to the client handler.
*
Expand All @@ -72,15 +114,25 @@ public function getBaseUrl()
*/
public function prepareRequest(CRRequest $request)
{
$url = $this->getBaseUrl().$request->getEndpoint()."/".implode(",",$request->getParams());
return [
$url,
$this->getUrl($request),
$request->getMethod(),
$request->getHeaders(),
$request->isAsyncRequest(),
];
}


/**
* Check the server status
* @method ping
* @return bool Return true if is up, otherwise returns false
*/
public function ping()
{
return $this->httpClientHandler->ping(self::BASE_URL);
}

/**
* Send an API request and process the result.
*
Expand All @@ -92,14 +144,20 @@ public function prepareRequest(CRRequest $request)
*/
public function sendRequest(CRRequest $request)
{
list($url, $method, $headers, $isAsyncRequest) = $this->prepareRequest($request);
list($url,$method, $headers, $isAsyncRequest) = $this->prepareRequest($request);
$timeOut = $request->getTimeOut();
$connectTimeOut = $request->getConnectTimeOut();
$options = [];
$con_stats = [];
$options = [
"on_stats"=>function (TransferStats $stats) use (&$con_stats)
{
if ($stats->hasResponse()) {
$con_stats = $stats->getHandlerStats();
}
}
];
$rawResponse = $this->httpClientHandler->send($url, $method, $headers, $options, $timeOut, $isAsyncRequest, $connectTimeOut);

$returnResponse = $this->getResponse($request, $rawResponse);

$returnResponse = $this->getResponse($request, $rawResponse, $con_stats);
if ($returnResponse->isError()) {
throw $returnResponse->getThrownException();
}
Expand All @@ -110,13 +168,14 @@ public function sendRequest(CRRequest $request)
/**
* Creates response object.
*
* @param CRRequest $request
* @param ResponseInterface|PromiseInterface $response
* @param CRRequest $request
* @param ResponseInterface|PromiseInterface $response
* @param array $stats
*
* @return CRResponse
*/
protected function getResponse(CRRequest $request, $response)
protected function getResponse(CRRequest $request, $response, $stats)
{
return new CRResponse($request, $response);
return new CRResponse($request, $response,$stats);
}
}
24 changes: 18 additions & 6 deletions src/CRConstant.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?php
/**************************************************************************************************************************************************************************************************************************************************************
* *
* Copyright (c) 2018 by Firegore (https://firegore.es) (git:firegore2). *
* This file is part of clash-royale-php. *
* *
* clash-royale-php is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. *
* clash-royale-php is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Affero General Public License for more details. *
* You should have received a copy of the GNU General Public License along with clash-royale-php. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************************************************************************************************************************************************************************************************/

namespace CR;
use CR\Exceptions\CRSDKException;

Expand All @@ -8,22 +21,21 @@
*/
class CRConstant
{
const BASE_URL = "https://cr-api.github.io/cr-api-data/json/";
const BASE_URL = "https://cr-api.github.io/cr-api-data/json/{endpoint}.json";
static protected $file_cache = "";
static protected $endpoint = "";
static protected $max_cache_age = 3600;

public function getConstant($endpoint)
public static function getConstant($endpoint)
{
self::$endpoint = $endpoint.".json";
$url = str_replace('{endpoint}',$endpoint,self::BASE_URL);
self::$file_cache= "constant-".$endpoint;
if (CRCache::exists(self::$file_cache,["maxage"=>self::$max_cache_age])) {
$response = CRCache::get(self::$file_cache);
} else {
if ($response = file_get_contents(self::BASE_URL.self::$endpoint)) {
if ($response = file_get_contents($url)) {
CRCache::write(self::$file_cache,$response);
} else {
throw new CRSDKException("Error when try request ".self::BASE_URL.self::$endpoint." constants", 1);
throw new CRSDKException("Error when try request ".$url." constants", 1);
}
}

Expand Down
Loading

0 comments on commit bec687e

Please sign in to comment.