Created
October 1, 2016 07:13
-
-
Save dimmduh/18e9cc652db3a3c6f6236945ca085c64 to your computer and use it in GitHub Desktop.
Php class for manage Labels (folders) in greate service appMetrica (appmetrika.yandex.ru)
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 Yii; | |
use yii\helpers\Json; | |
use yii\web\HttpException; | |
class AppMetricaApi | |
{ | |
private $curl; | |
const DATA_DEFAULT_TIMEZONE = 'Asia/Irkutsk'; | |
private $oauthToken; | |
function __construct($oauthToken = null) | |
{ | |
if (empty($oauthToken)) { | |
$this->oauthToken = Yii::$app->params['appMetricaApiOauthToken']; | |
} else { | |
$this->oauthToken = $oauthToken; | |
} | |
$this->curl = curl_init(); | |
} | |
private function request($url, $method = "POST", $data = null) | |
{ | |
$url .= '?oauth_token=' . $this->oauthToken; | |
curl_reset($this->curl); | |
curl_setopt($this->curl, CURLOPT_URL, $url); | |
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); | |
if (!empty($data)) { | |
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
)); | |
curl_setopt($this->curl, CURLOPT_POSTFIELDS, Json::encode($data)); | |
} | |
$out = curl_exec($this->curl); | |
$code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); | |
if ($code == 200) { | |
return Json::decode($out); | |
} | |
throw new HttpException("Error in request appmetrica, response code: $code\ndata:\n$out"); | |
} | |
/** | |
* Create label with given name | |
* @param $name string Label name | |
* @return string Label id | |
* @throws HttpException | |
*/ | |
public function createLabel($name) | |
{ | |
$url = 'https://beta.api-appmetrika.yandex.ru/management/v1/labels'; | |
$data = ['label' => ['name' => $name]]; | |
$result = $this->request($url, "POST", $data); | |
return $result['id']; | |
} | |
/** | |
* Get your labels | |
* @return mixed Your labels list | |
* ( | |
* [id] => 12720 | |
* [name] => Traffic Racer | |
* ), | |
* ( | |
* [id] => 13125 | |
* [name] => Floppy | |
* ) | |
* @throws HttpException | |
*/ | |
public function getLabels() | |
{ | |
$url = "https://beta.api-appmetrika.yandex.ru/management/v1/labels"; | |
$result = $this->request($url, "GET"); | |
return isset($result['labels']) ? $result['labels'] : null; | |
} | |
/** | |
* Get label id by name | |
* @param $name string Label name | |
* @return string Label id | |
* @throws HttpException | |
*/ | |
public function getLabelIdByName($name) | |
{ | |
$labels = $this->getLabels(); | |
if (!empty($labels)) { | |
foreach ($labels as $label) { | |
if ($label['name'] == $name) { | |
return $label['id']; | |
} | |
} | |
} | |
return null; | |
} | |
public function deleteLabel($labelId) | |
{ | |
$url = "https://beta.api-appmetrika.yandex.ru/management/v1/label/$labelId"; | |
return $this->request($url, "DELETE"); | |
} | |
public function renameLabel($labelId, $name) | |
{ | |
$url = "https://beta.api-appmetrika.yandex.ru/management/v1/label/$labelId"; | |
$data = ['label' => ['name' => $name]]; | |
$this->request($url, "PUT", $data); | |
} | |
public function moveApp2Label($appId, $labelId) | |
{ | |
$url = "https://beta.api-appmetrika.yandex.ru/management/v1/label/$labelId/link/$appId"; | |
$this->request($url); | |
} | |
public function deleteAppFromLabel($appId) | |
{ | |
$url = "https://beta.api-appmetrika.yandex.ru/management/v1/labels/unlink/$appId"; | |
$this->request($url); | |
} | |
} |
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 | |
//I use with yii2, that why you need adapt api class for your framework (see use section) | |
//very simple usage | |
$appMetricaApi = new AppMetricaApi(); | |
$labelId = $appMetricaApi->createLabel("My Super Label"); | |
$appMetricaApi->getLabels(); | |
$labelId = $appMetricaApi->getLabelIdByName("My Super Label"); | |
$appId = "14350"; | |
$appMetricaApi->moveApp2Label($appId, $labelId); | |
$appMetricaApi->deleteAppFromLabel($appId); | |
$appMetricaApi->deleteLabel($appId); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could not find docs for management labels via API, but support helped, it's just undocumented yet.
Maybe it will changed, be careful.