diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..493de95 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.lock +.idea +vendor \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f28d302 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# TMS Provider API sdk +The TMS provider api SDK provides PHP APIs to create and manage accounts, devices, subsriptions, etc. + +## Installation +Install using composer: + +`composer require tvip/providerapiphpsdk` + +## Usage: + +See examples folder. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..95db051 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "tvip/tmsproviderapiphpsdk", + "description": "PHP SDK for Tvip TMS Provider API", + "license": "MIT", + "version": "1.0.0", + "authors": [ + { + "name": "Denis Tsyganov", + "email": "td@tvip.ru" + } + ], + "require": { + "guzzlehttp/guzzle": "^6.3.0", + "ext-json": "*" + }, + "autoload": { + "psr-4": { + "ProviderTmsApiSdk\\": "src/" + } + } +} diff --git a/examples/simple-example.php b/examples/simple-example.php new file mode 100644 index 0000000..a7945d7 --- /dev/null +++ b/examples/simple-example.php @@ -0,0 +1,57 @@ +Configure('https://tms.example.com', 'admin_account', 'admin_account_password'); + +# Example create account +$new_account = new TmsAccount(); + +$new_account->login = "test_account"; +$new_account->fullname = "FullName of test account"; + +try { + $account = $new_account->create(); +} catch (TmsException $exception) { + print_r($exception->getMessage()); + $account = null; +} + +# Example update account +if ($account != null) { + print_r($account); + $account->fullname = "new fullname"; + + try { + $account->update(); + } catch (TmsException $exception) { + print_r($exception->getMessage()); + } +} + +#Example get accounts with paging +try { + $apiAccount = new TmsAccount(); + list($accounts, $total) = $apiAccount->getList(); + + + if ($total > count($accounts)) { + while (true) { + if ($total == count($accounts)) { + break; + } + list($page_accounts, $_) = $apiAccount->getList(count($accounts), 50, 'id'); + $accounts = array_merge($accounts, $page_accounts); + } + } +} catch (TmsException $exception){ + print_r($exception->getMessage()); +} + +print_r($accounts); diff --git a/src/Singleton.php b/src/Singleton.php new file mode 100644 index 0000000..b0cec99 --- /dev/null +++ b/src/Singleton.php @@ -0,0 +1,22 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param int $provider + * @param int $enabled + * @param string $login + * @param string $remote_custom_field + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort="", $provider = null, $enabled = null, $login = "", $remote_custom_field = "", $quick_search = "") + { + $params = array( + 'sort' => $sort, + 'provider' => $provider, + 'enabled' => $enabled, + 'login' => $login, + 'remote_custom_field' => $remote_custom_field, + 'quick_search' => $quick_search + ); + + list($accounts, $total) = parent::getList($params, $start, $limit); + + return [ $accounts, $total ]; + } +} \ No newline at end of file diff --git a/src/TmsAccountSubscription.php b/src/TmsAccountSubscription.php new file mode 100644 index 0000000..df2300e --- /dev/null +++ b/src/TmsAccountSubscription.php @@ -0,0 +1,65 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param int $account + * @param int $tarif + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $account = null, $tarif = null, $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'account' => $account, + 'tarif' => $tarif, + 'quick_search' => $quick_search + ); + list($accountSubscriptions, $total) = parent::getList($params, $start, $limit); + + return [ $accountSubscriptions, $total ]; + } +} \ No newline at end of file diff --git a/src/TmsBaseModel.php b/src/TmsBaseModel.php new file mode 100644 index 0000000..118ff4f --- /dev/null +++ b/src/TmsBaseModel.php @@ -0,0 +1,86 @@ +request( + "GET", + $config->baseUrl . "/" . $this->path . '/' . $id, + [ + 'headers' => $config->headers, + 'auth' => $config->auth + ] + ); + } catch (GuzzleException $exception){ + throw new TmsException($exception->getMessage()); + } + + + return $this->serialize($response->getBody()->getContents()); + } + + /** + * @param array $params + * @param int $start + * @param int $limit + */ + public function getList($params, $start=0, $limit=50) + { + $objects = array(); + $config = TmsConfig::getInstance(); + $client = new Client(); + + $query = "?start=" . $start . "&limit=" . $limit; + + foreach ($params as $key => $value) { + if (!empty($value) || $value != null) { + $query .= "&" . $key . "=" . $value; + } + } + + try { + $response = $client->request( + "GET", + $config->baseUrl . "/" . $this->path . $query, + [ + 'headers' => $config->headers, + 'auth' => $config->auth + ] + ); + } catch (GuzzleException $exception) { + throw new TmsException($exception->getMessage()); + } + + $jsonData = $response->getBody()->getContents(); + + foreach (json_decode($jsonData)->data as $data) { + $o = clone ($this->serialize(json_encode($data))); + array_push($objects, $o); + + } + + $totalDevices = json_decode($jsonData)->total; + + return [ $objects, $totalDevices ]; + + } + + abstract function serialize($jsonData); + +} \ No newline at end of file diff --git a/src/TmsChannel.php b/src/TmsChannel.php new file mode 100644 index 0000000..e3c3ca5 --- /dev/null +++ b/src/TmsChannel.php @@ -0,0 +1,79 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param int $tarif + * @param bool $enabled + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $tarif = null, $enabled = null, $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'tarif' => $tarif, + 'enabled' => $enabled, + 'quick_search' => $quick_search + ); + + list($channels, $total) = parent::getList($params, $start, $limit); + + return [ $channels, $total ]; + } + + +} \ No newline at end of file diff --git a/src/TmsConfig.php b/src/TmsConfig.php new file mode 100644 index 0000000..2c0d969 --- /dev/null +++ b/src/TmsConfig.php @@ -0,0 +1,72 @@ +baseUrl = $baseUrl; + $this->username = $username; + $this->password = $password; + $this->token = $token; + + $this->headers = $this->setHeaders(); + $this->auth = $this->setAuth(); + + } + + private function setHeaders() + { + $this->baseUrl = $this->baseUrl . '/api/provider'; + $headers = array( + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + ); + + return $headers; + } + + private function setAuth(){ + + if ($this->username && ($this->password || $this->token)) { + if ($this->password) { + $auth = array( + $this->username, + $this->password + ); + } + if ($this->token) { + $auth = array( + $this->username, + $this->token + ); + } + } + + return $auth; + } + + +} \ No newline at end of file diff --git a/src/TmsDevice.php b/src/TmsDevice.php new file mode 100644 index 0000000..52ceca7 --- /dev/null +++ b/src/TmsDevice.php @@ -0,0 +1,123 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param string $unique_id + * @param int $device_type + * @param int $provider + * @param bool $enabled + * @param string $remote_custom_field + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $unique_id = "", $device_type = null, $provider = null, + $enabled = null, $remote_custom_field="", $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'unique_id'=> $unique_id, + 'device_type' => $device_type, + 'provider' => $provider, + 'enabled' => $enabled, + 'remote_custom_field' => $remote_custom_field, + 'quick_search' => $quick_search + ); + + list($devices, $total) = parent::getList($params, $start, $limit); + + return [ $devices, $total ]; + } +} \ No newline at end of file diff --git a/src/TmsDeviceSubscription.php b/src/TmsDeviceSubscription.php new file mode 100644 index 0000000..0d46c65 --- /dev/null +++ b/src/TmsDeviceSubscription.php @@ -0,0 +1,66 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param int $device + * @param int $tarif + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $device = null, $tarif = null, $quick_search = '') + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'device' => $device, + 'tarif' => $tarif, + 'quick_search' => $quick_search + ); + list($deviceSubscriptions, $total) = parent::getList($params, $start, $limit); + + return [ $deviceSubscriptions, $total ]; + } +} diff --git a/src/TmsException.php b/src/TmsException.php new file mode 100644 index 0000000..5ba886b --- /dev/null +++ b/src/TmsException.php @@ -0,0 +1,14 @@ +request( + "POST", + $config->baseUrl . "/" . $this->path, + [ + 'headers' => $config->headers, + 'auth' => $config->auth, + 'body' => $jsonData + ] + ); + } catch (GuzzleException $exception){ + throw new TmsException($exception->getMessage()); + } + + return $this->serialize($response->getBody()->getContents()); + } + + public function update() + { + if (!isset($this->id)) { + throw new TmsException("Filed id cannoy be empty"); + } + + $config = TmsConfig::getInstance(); + $jsonData = json_encode($this); + + $client = new Client(); + try { + $response = $client->request( + "PUT", + $config->baseUrl . "/" . $this->path . "/" . $this->id, + [ + 'headers' => $config->headers, + 'auth' => $config->auth, + 'body' => $jsonData + ] + ); + } catch (GuzzleException $exception){ + throw new TmsException($exception->getMessage()); + } + + return $this->serialize($response->getBody()->getContents()); + } + + public function delete() + { + if (!isset($this->id)) { + throw new TmsException("Filed id cannoy be empty"); + } + + $config = TmsConfig::getInstance(); + $jsonData = json_encode($this); + + $client = new Client(); + try { + $response = $client->request( + "DELETE", + $config->baseUrl . "/" . $this->path . "/" . $this->id, + [ + 'headers' => $config->headers, + 'auth' => $config->auth, + ] + ); + } catch (GuzzleException $exception){ + throw new TmsException($exception->getMessage()); + } + + return $response->getStatusCode(); + } + +// abstract function serialize($jsonData); +} \ No newline at end of file diff --git a/src/TmsProvider.php b/src/TmsProvider.php new file mode 100644 index 0000000..fb38990 --- /dev/null +++ b/src/TmsProvider.php @@ -0,0 +1,76 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param bool $enabled + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $enabled = null, $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'enabled' => $enabled, + 'quick_search' => $quick_search + ); + + list($providers, $total) = parent::getList($params, $start, $limit); + + return [ $providers, $total ]; + } + +} \ No newline at end of file diff --git a/src/TmsRegion.php b/src/TmsRegion.php new file mode 100644 index 0000000..e866383 --- /dev/null +++ b/src/TmsRegion.php @@ -0,0 +1,52 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'quick_search' => $quick_search + ); + + list($regions, $total) = parent::getList($params, $start, $limit); + + return [ $regions, $total ]; + } + +} \ No newline at end of file diff --git a/src/TmsTarif.php b/src/TmsTarif.php new file mode 100644 index 0000000..6a4c817 --- /dev/null +++ b/src/TmsTarif.php @@ -0,0 +1,76 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + /** + * @param int $start + * @param int $limit + * @param string $sort + * @param int $channel + * @param bool $enabled + * @param int $provider + * @param int $tarif_tag + * @param string $quick_search + * @return array + * @throws TmsException + */ + public function getList($start = 0, $limit = 50, $sort = "", $channel = null, $enabled = null, + $provider = null, $tarif_tag = null, $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'channel' => $channel, + 'enabled' => $enabled, + 'provider' => $provider, + 'tarif_tag' => $tarif_tag, + 'quick_search' => $quick_search + ); + + list($tarifs, $total) = parent::getList($params, $start, $limit); + + return [ $tarifs, $total ]; + } +} \ No newline at end of file diff --git a/src/TmsTarifTag.php b/src/TmsTarifTag.php new file mode 100644 index 0000000..0037244 --- /dev/null +++ b/src/TmsTarifTag.php @@ -0,0 +1,58 @@ + $value){ + $this->{$key} = $value; + } + return $this; + } + + public function getList($start = 0, $limit = 50, $sort = "",$enabled = null, $provider = null, $quick_search = "") + { + $params = array( + 'start' => $start, + 'limit' => $limit, + 'sort' => $sort, + 'enabled' => $enabled, + 'provider' => $provider, + 'quick_search' => $quick_search + ); + + list($tarifTags, $total) = parent::getList($params, $start, $limit); + + return [ $tarifTags, $total ]; + } + +} \ No newline at end of file