Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Tsyganov committed Oct 25, 2021
0 parents commit 96d3a75
Show file tree
Hide file tree
Showing 18 changed files with 1,066 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
.idea
vendor
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"require": {
"guzzlehttp/guzzle": "^6.3.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"ProviderTmsApiSdk\\": "src/"
}
}
}
57 changes: 57 additions & 0 deletions examples/simple-example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
require_once '../vendor/autoload.php';


use ProviderTmsApiSdk\TmsConfig;
use ProviderTmsApiSdk\TmsAccount;
use ProviderTmsApiSdk\TmsException;


$config = TmsConfig::getInstance();
$config->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);
22 changes: 22 additions & 0 deletions src/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace ProviderTmsApiSdk;


class Singleton
{
private static $instances = [];

protected function __construct() { }

protected function __clone() { }

public static function getInstance()
{
$subclass = static::class;
if (!isset(self::$instances[$subclass])) {

self::$instances[$subclass] = new static();
}
return self::$instances[$subclass];
}
}
96 changes: 96 additions & 0 deletions src/TmsAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace ProviderTmsApiSdk;


class TmsAccount extends TmsExtendedModel
{
protected $path = 'accounts';

/**
* @var string
*/
public $login;
/**
* @var string
*/
public $fullname;

/**
* @var integer
*/
public $id = null;

/**
* @var string
*/
public $account_desc = "";
/**
* @var string
*/
public $contract_info = "";
/**
* @var integer
*/
public $devices_per_account_limit = null;
/**
* @var bool
*/
public $enabled = true;
/**
* @var string
*/
public $main_address = "";
/**
* @var string
*/
public $pin_md5 = "";
/**
* @var string
*/
public $remote_custom_field = "";
/**
* @var integer
*/
public $provider = null;
/**
* @var integer
*/
public $region_tag = null;

public function serialize($jsonData)
{
$account = json_decode($jsonData);
foreach ($account as $key => $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 ];
}
}
65 changes: 65 additions & 0 deletions src/TmsAccountSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace ProviderTmsApiSdk;

class TmsAccountSubscription extends TmsExtendedModel
{
protected $path = "account_subscriptions";

/**
* @var integer
*/
public $account;

/**
* @var string
*/
public $start;

/**
* @var integer
*/
public $tarif;

/**
* @var integer
*/
public $id = null;

/**
* @var string
*/
public $stop = null;

public function serialize($jsonData)
{
$account = json_decode($jsonData);
foreach ($account as $key => $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 ];
}
}
86 changes: 86 additions & 0 deletions src/TmsBaseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace ProviderTmsApiSdk;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

abstract class TmsBaseModel
{
/**
* @var string
*/
protected $path;

/**
* @param $id integer
* @throws TmsException
*/
public function get($id) {
$config = TmsConfig::getInstance();
$client = new Client();
try {
$response = $client->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);

}
Loading

0 comments on commit 96d3a75

Please sign in to comment.