Skip to content

Commit

Permalink
Merge pull request #7 from exodus4d/develop
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
exodus4d authored Oct 22, 2017
2 parents f8bbf41 + 83b5724 commit 96cdc75
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 55 deletions.
29 changes: 29 additions & 0 deletions app/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ public function setWaypoint(int $systemId, string $accessToken, array $options =
*/
public function getCorporationRoles(int $corporationId, string $accessToken): array;

/**
* @return array
*/
public function getRegions(): array;

/**
* @param int $regionId
* @return array
*/
public function getRegionData(int $regionId): array;

/**
* @return array
*/
public function getConstellations(): array;

/**
* @param int $constellationId
* @return array
*/
public function getConstellationData(int $constellationId): array;

/**
* @param array $universeIds
* @param array $additionalOptions
Expand All @@ -109,6 +131,13 @@ public function getUniverseJumps(): array;
*/
public function getUniverseKills(): array;

/**
* @param int $typeId
* @param array $additionalOptions
* @return array
*/
public function getUniverseTypesData(int $typeId, array $additionalOptions = []): array;

/**
* @param int $targetId
* @param string $accessToken
Expand Down
15 changes: 15 additions & 0 deletions app/Config/ESIConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ class ESIConf extends \Prefab {
],
'system_kills' => [
'GET' => ' /v1/universe/system_kills/'
],
'regions' => [
'GET' => '/v1/universe/regions/{x}/',
'list' => [
'GET' => '/v1/universe/regions/'
]
],
'constellations' => [
'GET' => '/v1/universe/constellations/{x}/',
'list' => [
'GET' => '/v1/universe/constellations/'
]
],
'types' => [
'GET' => '/v3/universe/types/{x}/'
]
],
'ui' => [
Expand Down
154 changes: 128 additions & 26 deletions app/ESI.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class ESI implements ApiInterface {
*/
private $esiUrl, $esiUserAgent, $esiDatasource, $endpointVersion = '';

/**
* debugLevel
* @var int
*/
private $debugLevel = 0;

/**
* ESI constructor.
*/
Expand Down Expand Up @@ -54,6 +60,13 @@ public function setDatasource(string $datasource){
$this->esiDatasource = $datasource;
}

/**
* @param int $debug
*/
public function setDebugLevel(int $debug){
$this->debugLevel = $debug;
}

/**
* @param string $version
*/
Expand Down Expand Up @@ -82,6 +95,13 @@ public function getDatasource(): string{
return $this->esiDatasource;
}

/**
* @return int
*/
public function getDebugLevel(): int {
return $this->debugLevel;
}

/**
* @return string
*/
Expand Down Expand Up @@ -287,6 +307,68 @@ public function getCorporationRoles(int $corporationId, string $accessToken): ar
return $rolesData;
}

/**
* @return array
*/
public function getRegions(): array{
$url = $this->getEndpointURL(['universe', 'regions', 'list', 'GET']);
$regionData = [];
$response = $this->request($url, 'GET');

if( !empty($response) ){
$regionData = array_unique( array_map('intval', $response) );
}

return $regionData;
}

/**
* @param int $regionId
* @return array
*/
public function getRegionData(int $regionId): array{
$url = $this->getEndpointURL(['universe', 'regions', 'GET'], [$regionId]);
$regionData = [];
$response = $this->request($url, 'GET');

if( !empty($response) ){
$regionData = (new namespace\Mapper\Region($response))->getData();
}

return $regionData;
}

/**
* @return array
*/
public function getConstellations(): array{
$url = $this->getEndpointURL(['universe', 'constellations', 'list', 'GET']);
$constellationData = [];
$response = $this->request($url, 'GET');

if( !empty($response) ){
$constellationData = array_unique( array_map('intval', $response) );
}

return $constellationData;
}

/**
* @param int $constellationId
* @return array
*/
public function getConstellationData(int $constellationId): array{
$url = $this->getEndpointURL(['universe', 'constellations', 'GET'], [$constellationId]);
$constellationData = [];
$response = $this->request($url, 'GET');

if( !empty($response) ){
$constellationData = (new namespace\Mapper\Constellation($response))->getData();
}

return $constellationData;
}

/**
* @param array $universeIds
* @param array $additionalOptions
Expand Down Expand Up @@ -362,6 +444,23 @@ public function getUniverseKills(): array{
return $systemKills;
}

/**
* @param int $typeId
* @param array $additionalOptions
* @return array
*/
public function getUniverseTypesData(int $typeId, array $additionalOptions = []): array {
$url = $this->getEndpointURL(['universe', 'types', 'GET'], [$typeId]);
$typesData = [];
$response = $this->request($url, 'GET', '', $additionalOptions);

if( !empty($response) ){
$typesData = (new namespace\Mapper\Universe\Type($response))->getData();
}

return $typesData;
}

/**
* @param int $targetId
* @param string $accessToken
Expand Down Expand Up @@ -452,35 +551,38 @@ protected function request(string $url, string $method = 'GET', string $accessTo
$responseBody = null;
$method = strtoupper($method);

$webClient = namespace\Lib\WebClient::instance();
$webClient = namespace\Lib\WebClient::instance($this->getDebugLevel());

if( \Audit::instance()->url($url) ){
if( $webClient->checkRequestMethod($method) ){
$requestOptions = [
'timeout' => self::ESI_TIMEOUT,
'method' => $method,
'user_agent' => $this->getUserAgent(),
'header' => [
'Accept: application/json'
]
];

// add auth token if available (required for some endpoints)
if( !empty($accessToken) ){
$requestOptions['header'][] = 'Authorization: Bearer ' . $accessToken;
// check if url is blocked (error limit exceeded)
if(!$webClient->isBlockedUrl($url)){
if( $webClient->checkRequestMethod($method) ){
$requestOptions = [
'timeout' => self::ESI_TIMEOUT,
'method' => $method,
'user_agent' => $this->getUserAgent(),
'header' => [
'Accept: application/json'
]
];

// add auth token if available (required for some endpoints)
if( !empty($accessToken) ){
$requestOptions['header'][] = 'Authorization: Bearer ' . $accessToken;
}

if( !empty($additionalOptions['content']) ){
// "Content-Type" Header is required for POST requests
$requestOptions['header'][] = 'Content-Type: application/json';

$requestOptions['content'] = json_encode($additionalOptions['content'], JSON_UNESCAPED_SLASHES);
unset($additionalOptions['content']);
}

$responseBody = $webClient->request($url, $requestOptions, $additionalOptions);
}else{
$webClient->getLogger('err_server')->write(sprintf(self::ERROR_ESI_METHOD, $method, $url));
}

if( !empty($additionalOptions['content']) ){
// "Content-Type" Header is required for POST requests
$requestOptions['header'][] = 'Content-Type: application/json';

$requestOptions['content'] = json_encode($additionalOptions['content'], JSON_UNESCAPED_SLASHES);
unset($additionalOptions['content']);
}

$responseBody = $webClient->request($url, $requestOptions, $additionalOptions);
}else{
$webClient->getLogger('err_server')->write(sprintf(self::ERROR_ESI_METHOD, $method, $url));
}
}else{
$webClient->getLogger('err_server')->write(sprintf(self::ERROR_ESI_URL, $url));
Expand Down
Loading

0 comments on commit 96cdc75

Please sign in to comment.