From 9ea4e6e64f8157939866e745e89d49d4aab2d806 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 09:58:29 +0100 Subject: [PATCH 1/7] update indents to tab --- app/Model/Datasource/RaidheadSource.php | 104 ++++++++++++------------ 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index 42dbab0..38c51fa 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -6,70 +6,70 @@ * @desc Used for reading from RaidHead API. * */ - + App::uses('DataSource', 'Model/Datasource'); App::uses('HttpSocket', 'Network/Http'); class RaidheadSource extends DataSource { - public $config = array( - 'baseUrl' => 'http://api.raidhead.com/', - 'langs' => array('eng', 'fra') - ); + public $config = array( + 'baseUrl' => 'http://api.raidhead.com/', + 'langs' => array('eng', 'fra') + ); + + private $http; - 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]; + } - 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; + } - $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); - } + 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']; - } - } + if($type == 'list') { + if(!empty($games)) { + foreach($games as $game) { + $list[$game['short']] = $game['title']; + } + } - return $list; - } + return $list; + } - return $games; - } + 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); - } + 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; - } + 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); - } + 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; - } -} \ No newline at end of file + return $serverStatus; + } +} From 74e751cd3ee5d2a533cae5dcc68ee691b1dc3ad5 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:02:13 +0100 Subject: [PATCH 2/7] RaidheadSource::_get(string ) method --- app/Model/Datasource/RaidheadSource.php | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index 38c51fa..2532dfe 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -29,6 +29,35 @@ public function __construct() { $this->config['baseUrl'] .= $lang; } + /** + * query api and return response as array or false + * @param string $uri api uri + * @return array|false response as array or false if failed + */ + private function _get($uri) + { + $client = new HttpSocket([ + 'request' => [ + 'redirect' => 2, + ], + ]); + + try { + $response = $client->get([ + 'uri' => $this->config['baseUrl'] . $uri, + 'header' => [ + 'User-Agent' => 'Mushraider', + ], + ]); + + $return = json_decode($response, true); + } catch (Exception $e) { + $return = false; + } + + return $return; + } + public function gets($type = 'all') { $list = array(); $json = $this->http->get($this->config['baseUrl'].'/games/index.json'); From d7c2eb24606314a707289cc3fbfd75f310d9b210 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:06:35 +0100 Subject: [PATCH 3/7] RaidheadSource::gets() use RaidheadSource::_get --- app/Model/Datasource/RaidheadSource.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index 2532dfe..d010f5d 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -9,6 +9,7 @@ App::uses('DataSource', 'Model/Datasource'); App::uses('HttpSocket', 'Network/Http'); +App::uses('Configure', 'Core'); class RaidheadSource extends DataSource { @@ -43,14 +44,13 @@ private function _get($uri) ]); try { - $response = $client->get([ - 'uri' => $this->config['baseUrl'] . $uri, + $response = $client->get($this->config['baseUrl'] . $uri, [], [ 'header' => [ - 'User-Agent' => 'Mushraider', + 'User-Agent' => 'Mushraider/' . Configure::read('mushraider.version'), ], ]); - $return = json_decode($response, true); + $return = json_decode($response->body(), true); } catch (Exception $e) { $return = false; } @@ -61,8 +61,8 @@ private function _get($uri) 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)) { + $games = $this->_get('/games/index.json'); + if($games === false) { $error = json_last_error(); throw new CakeException($error); } From 931feba244122ba69991f6a0919a6c48c99ab03d Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:09:10 +0100 Subject: [PATCH 4/7] all RaidheadSource methods uses _get() --- app/Model/Datasource/RaidheadSource.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index d010f5d..90fdbdc 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -18,10 +18,8 @@ class RaidheadSource extends DataSource { '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]; @@ -60,7 +58,6 @@ private function _get($uri) public function gets($type = 'all') { $list = array(); - $json = $this->http->get($this->config['baseUrl'].'/games/index.json'); $games = $this->_get('/games/index.json'); if($games === false) { $error = json_last_error(); @@ -81,9 +78,8 @@ public function gets($type = 'all') { } public function get($slug) { - $json = $this->http->get($this->config['baseUrl'].'/games/get/'.$slug.'.json'); - $game = json_decode($json, true); - if(is_null($game)) { + $game = $this->_get('/games/get/' . $slug . '.json'); + if($game === false) { $error = json_last_error(); throw new CakeException($error); } @@ -92,9 +88,8 @@ public function get($slug) { } 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)) { + $serverStatus = $this->_get('/server_status/get/' . $slug . '.json'); + if($serverStatus === false) { $error = json_last_error(); throw new CakeException($error); } From 7d78fb58a95290f192811f7eb3aba024bbf5f5bd Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:10:47 +0100 Subject: [PATCH 5/7] RaidheadSource directly use https api --- app/Model/Datasource/RaidheadSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index 90fdbdc..f042a26 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -14,7 +14,7 @@ class RaidheadSource extends DataSource { public $config = array( - 'baseUrl' => 'http://api.raidhead.com/', + 'baseUrl' => 'https://api.raidhead.com/', 'langs' => array('eng', 'fra') ); From 31d9aa379fc63e9a7fc3e7c08be79cc777573754 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:13:52 +0100 Subject: [PATCH 6/7] bump version to 1.7.0.3 --- app/Config/core.php | 4 ++-- app/Model/Datasource/RaidheadSource.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Config/core.php b/app/Config/core.php index 3de63f6..07ec511 100644 --- a/app/Config/core.php +++ b/app/Config/core.php @@ -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 diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index f042a26..b8d26d4 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -18,7 +18,6 @@ class RaidheadSource extends DataSource { 'langs' => array('eng', 'fra') ); - public function __construct() { $lang = strtolower(Configure::read('Settings.language')); if(!in_array($lang, $this->config['langs'])) { From 31186d524ce6bb7ae6102444ff1ff184e64f5acd Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Thu, 17 Jan 2019 10:26:43 +0100 Subject: [PATCH 7/7] RaidheadSource::_get() handle errors and throw exceptions. Code is more clean --- app/Model/Datasource/RaidheadSource.php | 57 ++++++++++--------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index b8d26d4..18920d8 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -30,44 +30,43 @@ public function __construct() { /** * query api and return response as array or false * @param string $uri api uri - * @return array|false response as array or false if failed + * @return array response as array + * @throws CakeException */ private function _get($uri) { - $client = new HttpSocket([ - 'request' => [ + $client = new HttpSocket(array( + 'request' => array( 'redirect' => 2, - ], - ]); + ), + )); try { - $response = $client->get($this->config['baseUrl'] . $uri, [], [ - 'header' => [ + $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) { - $return = false; + throw $e; } - - return $return; } public function gets($type = 'all') { - $list = array(); $games = $this->_get('/games/index.json'); - if($games === false) { - $error = json_last_error(); - throw new CakeException($error); - } if($type == 'list') { - if(!empty($games)) { - foreach($games as $game) { - $list[$game['short']] = $game['title']; - } + $list = array(); + foreach($games as $game) { + $list[$game['short']] = $game['title']; } return $list; @@ -77,22 +76,10 @@ public function gets($type = 'all') { } public function get($slug) { - $game = $this->_get('/games/get/' . $slug . '.json'); - if($game === false) { - $error = json_last_error(); - throw new CakeException($error); - } - - return $game; + return $this->_get('/games/get/' . $slug . '.json'); } public function serverStatus($slug) { - $serverStatus = $this->_get('/server_status/get/' . $slug . '.json'); - if($serverStatus === false) { - $error = json_last_error(); - throw new CakeException($error); - } - - return $serverStatus; + return $this->_get('/server_status/get/' . $slug . '.json'); } }