Created
September 15, 2016 02:40
-
-
Save dimmduh/ddec3389aee612ae43e27d8791aa570e to your computer and use it in GitHub Desktop.
app2top.org api client lib 1.1 (still in progress and deprecated (cause app2top will be updated in late 2016)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\components; | |
use GuzzleHttp; | |
use yii\base\InvalidParamException; | |
use yii\helpers\Json; | |
class App2TopApi | |
{ | |
private $client; | |
const SERVER_URL = 'http://app2top/index.php?r=api/'; | |
const ACTION_GET = 'view'; | |
const ACTION_CREATE = 'create'; | |
const ACTION_DELETE = 'delete'; | |
const MODEL_ORDER = 'order'; | |
const MODEL_ORDERS = 'orders'; | |
/** | |
* @param string $apiKey Api key to access to app2top api | |
* @link https://app2top.org/index.php?r=api/index | |
*/ | |
function __construct($apiKey) | |
{ | |
$this->client = new GuzzleHttp\Client([ | |
'timeout' => 15, | |
'allow_redirects' => false, | |
'http_errors' => false, | |
'headers' => [ | |
'ApiKey' => $apiKey, | |
] | |
]); | |
} | |
public function createOrder(App2TopCreateOrderData $orderData){ | |
return $this->postRequest($this->buildUrl('create', 'order'), $orderData->getData()); | |
} | |
public function viewOrders($packageName) | |
{ | |
return $this->getRequest($this->buildUrl('view', 'orders', ['package_name' => $packageName])); | |
} | |
public function viewOrder($id) | |
{ | |
return $this->getRequest($this->buildUrl('view', 'order', ['id' => $id])); | |
} | |
public function viewUser() | |
{ | |
$result = $this->getRequest($this->buildUrl('view', 'user')); | |
d($result); | |
} | |
private function buildUrl($action, $model, $params = []) | |
{ | |
return self::SERVER_URL . $action . '&model=' . $model . '&' . http_build_query($params); | |
} | |
private function getRequest($url) | |
{ | |
return $this->request($url, 'GET'); | |
} | |
private function postRequest($url, $data) | |
{ | |
return $this->request($url, 'POST', Json::encode($data)); | |
} | |
private function request($url, $method, $body = '') | |
{ | |
$response = $this->client->request($method, $url, ['body' => $body]); | |
return Json::decode($response->getBody()->__toString()); | |
} | |
} | |
//@todo add set startTime | |
//@see http://codebeautify.org/jsonviewer/cbe48e50 | |
class App2TopCreateOrderData { | |
private $installs = []; | |
private $stars = []; | |
private $packageName; | |
private $pay; | |
private $deliveryTime; | |
/** | |
* App2TopOrderData constructor. | |
* @param $packageName | |
* @param int $deliveryTime | |
* @param bool $pay | |
*/ | |
public function __construct($packageName, $deliveryTime = 0, $pay = true) | |
{ | |
$this->packageName = $packageName; | |
$this->deliveryTime = $deliveryTime; | |
$this->pay = $pay; | |
} | |
public function addInstalls($countryCode, $count, $countByDay = []) | |
{ | |
$data = [ | |
'lang' => $countryCode, | |
'count' => $count, | |
]; | |
if (!empty($countByDay)) { | |
if ($count != array_sum($countByDay)) { | |
throw new InvalidParamException("Total count is not equal to sum of count by days"); | |
} | |
if ($this->deliveryTime != count($countByDay)) { | |
throw new InvalidParamException("Array count of count by day is not equal to delivery time days"); | |
} | |
$data['count_by_day'] = $countByDay; | |
} | |
$this->installs [] = $data; | |
} | |
public function addStars($languageCode, $count, $rating) | |
{ | |
$data = [ | |
'lang' => $languageCode, | |
'count' => $count, | |
'mark' => $rating, | |
]; | |
$this->stars [] = $data; | |
} | |
public function getData(){ | |
return [ | |
'pay' => $this->pay, | |
'lasting_time' => $this->deliveryTime, | |
'package_name' => $this->packageName, | |
'start_time' => '14-09-2016', | |
'installs' => $this->installs, | |
'stars' => $this->stars, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment