Skip to content

Commit

Permalink
Merge pull request #9 from exodus4d/develop
Browse files Browse the repository at this point in the history
v1.2.2
  • Loading branch information
exodus4d authored Dec 30, 2017
2 parents 57210c2 + 338fa04 commit e46f7f4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 24 deletions.
8 changes: 4 additions & 4 deletions app/Config/ESIConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class ESIConf extends \Prefab {
'GET' => '/v1/status/'
],
'alliances' => [
'GET' => '/v2/alliances/{x}/'
'GET' => '/v3/alliances/{x}/'
],
'corporations' => [
'GET' => '/v3/corporations/{x}/',
'GET' => '/v4/corporations/{x}/',
'npccorps' => [
'GET' => '/v1/corporations/npccorps/'
],
Expand All @@ -39,7 +39,7 @@ class ESIConf extends \Prefab {
'GET' => '/v1/characters/{x}/ship/'
],
'online' => [
'GET' => '/v1/characters/{x}/online/'
'GET' => '/v2/characters/{x}/online/'
]
],
'universe' => [
Expand All @@ -50,7 +50,7 @@ class ESIConf extends \Prefab {
'GET' => ' /v1/universe/system_jumps/'
],
'system_kills' => [
'GET' => ' /v1/universe/system_kills/'
'GET' => ' /v2/universe/system_kills/'
],
'regions' => [
'GET' => '/v1/universe/regions/{x}/',
Expand Down
8 changes: 1 addition & 7 deletions app/ESI.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,11 @@ public function getCharacterShipData(int $characterId, string $accessToken, arra
public function getCharacterOnlineData(int $characterId, string $accessToken, array $additionalOptions = []): array{
$url = $this->getEndpointURL(['characters', 'online', 'GET'], [$characterId]);
$onlineData = [];

$response = $this->request($url, 'GET', $accessToken, $additionalOptions);

$onlineData = [
'online' => is_bool($response) ? $response : null
];

/* v2 endpoint (WIP)
if( !empty($response) ){
$onlineData = (new namespace\Mapper\Online($response))->getData();
} */
}

return $onlineData;
}
Expand Down
66 changes: 56 additions & 10 deletions app/Lib/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
class WebClient extends \Web {

const CACHE_KEY_ERROR_LIMIT = 'CACHED_ERROR_LIMIT';
const CACHE_KEY_LOGGABLE_LIMIT = 'CACHED_LOGGABLE_LIMIT';

const ERROR_STATUS_LOG = 'HTTP %s: \'%s\' | url: %s \'%s\'%s';
const ERROR_RESOURCE_LEGACY = 'Resource: %s has been marked as legacy. (%s)';
const ERROR_RESOURCE_DEPRECATED = 'Resource: %s has been marked as deprecated. (%s)';
const ERROR_LIMIT_CRITICAL = 'Error rate reached critical amount. url: %s | errorCount: %s | errorRemainCount: %s';
const ERROR_RESOURCE_LEGACY = 'Resource: \'%s\' has been marked as legacy. url: \'%s\' | header: \'%s\'';
const ERROR_RESOURCE_DEPRECATED = 'Resource: \'%s\' has been marked as deprecated. url: \'%s\' | header: \'%s\'';
const ERROR_LIMIT_CRITICAL = 'Error rate reached critical amount. url: \'%s\' | errorCount: %s | errorRemainCount: %s';
const ERROR_LIMIT_EXCEEDED = 'Error rate limit exceeded! We are blocked for (%s seconds)';
const DEBUG_URI_BLOCKED = 'Debug request blocked. Error limit exceeded. url: %s blocked for %2ss';
const DEBUG_URI_BLOCKED = 'Debug request blocked. Error limit exceeded. url: \'%s\' blocked for %2ss';

const REQUEST_METHODS = ['GET', 'POST', 'PUT', 'DELETE'];

// error limits ---------------------------------------------------------------------------------------------------
// ESI calls will return special headers in case a client hits a "error limit" for a single endpoint

// log error when this error count is reached for a single API endpoint in the current error window
const ERROR_COUNT_MAX_URL = 30;

Expand All @@ -32,6 +36,17 @@ class WebClient extends \Web {
// ->this is because CREST is not very stable
const RETRY_COUNT_MAX = 2;

// loggable limits ------------------------------------------------------------------------------------------------
// ESI endpoints that return warning headers (e.g. "resource_legacy", "resource_deprecated") will get logged
// To prevent big file I/O on these log files, errors get "throttled" and not all of them get logged

// Time interval used for error inspection (seconds)
const LOGGABLE_COUNT_INTERVAL = 60;

// Log first "2" errors that occur for an endpoint within "60" (LOGGABLE_COUNT_INTERVAL) seconds interval
const LOGGABLE_COUNT_MAX_URL = 2;


/**
* debugLevel used for internal error/warning logging
* @var int
Expand Down Expand Up @@ -153,12 +168,17 @@ protected function checkResponseHeaders(array $headers, string $url){
$warningHeaders = array_filter($headers, function($key){
return preg_match('/^warning/i', $key);
}, ARRAY_FILTER_USE_KEY);
foreach($warningHeaders as $key => $value){
if( preg_match('/^199/i', $value) ){
$this->getLogger('resource_legacy')->write(sprintf(self::ERROR_RESOURCE_LEGACY, $url, $value));
}
if( preg_match('/^299/i', $value) ){
$this->getLogger('resource_deprecated')->write(sprintf(self::ERROR_RESOURCE_DEPRECATED, $url, $value));

if(count($warningHeaders)){
// get "normalized" url path without params/placeholders
$urlPath = $this->getNormalizedUrlPath($url);
foreach($warningHeaders as $key => $value){
if( preg_match('/^199/i', $value) && $this->isLoggable('legacy', $url) ){
$this->getLogger('resource_legacy')->write(sprintf(self::ERROR_RESOURCE_LEGACY, $urlPath, $url, $key . ': ' . $value));
}
if( preg_match('/^299/i', $value) && $this->isLoggable('deprecated', $url) ){
$this->getLogger('resource_deprecated')->write(sprintf(self::ERROR_RESOURCE_DEPRECATED, $urlPath, $url, $key . ': ' . $value));
}
}
}

Expand Down Expand Up @@ -230,6 +250,32 @@ protected function getNormalizedUrlPath($url): string {
return parse_url(strtok(preg_replace('/\/(\d+)\//', '/{x}/', $url), '?'), PHP_URL_PATH);
}

/**
* @param string $type
* @param string $urlPath
* @return bool
*/
protected function isLoggable(string $type, string $urlPath) : bool {
$loggable = false;

$f3 = \Base::instance();
if(!$f3->exists(self::CACHE_KEY_LOGGABLE_LIMIT, $loggableLimit)){
$loggableLimit = [];
}

// increase counter
$count = (int)$loggableLimit[$urlPath][$type]['count']++;

// check counter for given $urlPath
if($count < self::LOGGABLE_COUNT_MAX_URL){
// loggable error count exceeded...
$loggable = true;
$f3->set(self::CACHE_KEY_LOGGABLE_LIMIT, $loggableLimit, self::LOGGABLE_COUNT_INTERVAL);
}

return $loggable;
}

/**
* check whether a HTTP request method is valid/given
* @param $method
Expand Down
2 changes: 1 addition & 1 deletion app/Mapper/Alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
class Alliance extends mapper\AbstractIterator {

protected static $map = [
'alliance_name' => 'name'
'name' => 'name'
];
}
2 changes: 1 addition & 1 deletion app/Mapper/Corporation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
class Corporation extends mapper\AbstractIterator {

protected static $map = [
'corporation_name' => 'name'
'name' => 'name'
];
}
2 changes: 1 addition & 1 deletion app/Mapper/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class Region extends mapper\AbstractIterator {
'region_id' => 'id',
'name' => 'name',
'description' => 'description',
'constellations' => 'constellationIds'
'constellations' => 'constellations'
];
}

0 comments on commit e46f7f4

Please sign in to comment.