Skip to content

Commit

Permalink
Merge pull request #95 from Erwane/fix-raidhead-datasource
Browse files Browse the repository at this point in the history
Fix raidhead datasource
  • Loading branch information
st3ph authored Jan 18, 2019
2 parents a4e8fde + 31186d5 commit 44bc015
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 64 deletions.
4 changes: 2 additions & 2 deletions app/Config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
*
* In production mode, flash messages redirect after a time interval.
* In development mode, you need to click the flash message to continue.
*/
*/

// TODO : generate config.salt.php


Configure::write('debug', 0);
Configure::write('mushraider', array('version' => '1.7.0.2', 'date' => '2017-02-19'));
Configure::write('mushraider', array('version' => '1.7.0.3', 'date' => '2019-01-17'));

/**
* Configure the Error handler used to handle errors for your application. By default
Expand Down
134 changes: 72 additions & 62 deletions app/Model/Datasource/RaidheadSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,80 @@
* @desc Used for reading from RaidHead API.
*
*/

App::uses('DataSource', 'Model/Datasource');
App::uses('HttpSocket', 'Network/Http');
App::uses('Configure', 'Core');

class RaidheadSource extends DataSource {

public $config = array(
'baseUrl' => 'http://api.raidhead.com/',
'langs' => array('eng', 'fra')
);

private $http;

public function __construct() {
$this->http = new HttpSocket();
$lang = strtolower(Configure::read('Settings.language'));
if(!in_array($lang, $this->config['langs'])) {
$lang = $this->config['langs'][0];
}

$this->config['baseUrl'] .= $lang;
}

public function gets($type = 'all') {
$list = array();
$json = $this->http->get($this->config['baseUrl'].'/games/index.json');
$games = json_decode($json, true);
if(is_null($games)) {
$error = json_last_error();
throw new CakeException($error);
}

if($type == 'list') {
if(!empty($games)) {
foreach($games as $game) {
$list[$game['short']] = $game['title'];
}
}

return $list;
}

return $games;
}

public function get($slug) {
$json = $this->http->get($this->config['baseUrl'].'/games/get/'.$slug.'.json');
$game = json_decode($json, true);
if(is_null($game)) {
$error = json_last_error();
throw new CakeException($error);
}

return $game;
}

public function serverStatus($slug) {
$json = $this->http->get($this->config['baseUrl'].'/server_status/get/'.$slug.'.json');
$serverStatus = json_decode($json, true);
if(is_null($serverStatus)) {
$error = json_last_error();
throw new CakeException($error);
}

return $serverStatus;
}
}
public $config = array(
'baseUrl' => 'https://api.raidhead.com/',
'langs' => array('eng', 'fra')
);

public function __construct() {
$lang = strtolower(Configure::read('Settings.language'));
if(!in_array($lang, $this->config['langs'])) {
$lang = $this->config['langs'][0];
}

$this->config['baseUrl'] .= $lang;
}

/**
* query api and return response as array or false
* @param string $uri api uri
* @return array response as array
* @throws CakeException
*/
private function _get($uri)
{
$client = new HttpSocket(array(
'request' => array(
'redirect' => 2,
),
));

try {
$response = $client->get($this->config['baseUrl'] . $uri, array(), array(
'header' => array(
'User-Agent' => 'Mushraider/' . Configure::read('mushraider.version'),
),
));

$return = json_decode($response->body(), true);

if ($return === false) {
throw new CakeException(json_last_error());
}

return $return;
} catch (Exception $e) {
throw $e;
}
}

public function gets($type = 'all') {
$games = $this->_get('/games/index.json');

if($type == 'list') {
$list = array();
foreach($games as $game) {
$list[$game['short']] = $game['title'];
}

return $list;
}

return $games;
}

public function get($slug) {
return $this->_get('/games/get/' . $slug . '.json');
}

public function serverStatus($slug) {
return $this->_get('/server_status/get/' . $slug . '.json');
}
}

0 comments on commit 44bc015

Please sign in to comment.