Skip to content

Commit 791b485

Browse files
committed
feat: data api client added, namespace changed
1 parent 749a952 commit 791b485

File tree

11 files changed

+477
-15
lines changed

11 files changed

+477
-15
lines changed

README.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# [UIS](https://www.uiscom.ru/) & [CoMagic](https://main.comagic.ru/) API PHP client
1+
# [UIScom](https://www.uiscom.ru/) (formerly [CoMagic](https://main.comagic.ru/)) API PHP client
22
UIS and CoMagic PHP client for the following APIs:
33
- [Call API](https://www.uiscom.ru/academiya/spravochnyj-centr/dokumentatsiya-api/call_api/)
4+
- [Data API](https://www.uiscom.ru/academiya/spravochnyj-centr/dokumentatsiya-api/data_api/)
45

56
## Requirements
67
This package requires PHP 7.4 or above.
78

89
## Installation
910
To get started, install package via the Composer package manager:
1011

11-
`composer require pashamesh/comagic-api`
12+
`composer require pashamesh/uiscom-api-client`
1213

1314
## Usage
1415

@@ -26,8 +27,9 @@ $config = [
2627
```
2728

2829
You also need to change domain if you client of Uiscom by specifying `endpoint`:
30+
2931
```php
30-
use CoMagic\CallApiConfig;
32+
use Uiscom\CallApiConfig;
3133

3234
$config = new CallApiConfig('login', 'password', 'access_token')
3335
```
@@ -37,9 +39,10 @@ password authorization for Call API.
3739

3840
### Call API
3941
API Methods names need to be specified in CamelCase
42+
4043
```php
41-
use CoMagic\CallApiConfig;
42-
use CoMagic\CallApiClient;
44+
use Uiscom\CallApiConfig;
45+
use Uiscom\CallApiClient;
4346

4447
$config = new CallApiConfig('login', 'password', 'access_token');
4548
$callApi = new CallApiClient($config);
@@ -50,3 +53,27 @@ It's possible to get response metadata after API request is made
5053
```php
5154
var_dump($callApi->metadata());
5255
```
56+
57+
### Data API
58+
59+
API Methods names need to be specified in CamelCase
60+
61+
```php
62+
use Uiscom\DataApiConfig;
63+
use Uiscom\DataApiClient;
64+
65+
$config = new DataApiConfig('access_token');
66+
$dataApi = new DataApiClient($config);
67+
var_dump(
68+
$dataApi->getCallsReport([
69+
'date_from' => '2025-01-10 00:00:00',
70+
'date_till' => '2025-01-13 23:59:59'
71+
])
72+
);
73+
```
74+
75+
It's possible to get response metadata after API request is made
76+
77+
```php
78+
var_dump($dataApi->metadata());
79+
```

composer.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
{
2-
"name": "pashamesh/comagic-api",
3-
"description": "php binding for UIScom and CoMagic API",
2+
"name": "pashamesh/uiscom-api-client",
3+
"description": "PHP binding for UIScom (formerly CoMagic) API",
44
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"uis",
8+
"uiscom",
9+
"comagic",
10+
"sdk",
11+
"php",
12+
"api",
13+
"package",
14+
"uiscom sdk",
15+
"uiscom api",
16+
"uiscom dataapi",
17+
"uiscom callapi",
18+
"uiscom package"
19+
],
520
"require": {
621
"php": ">=7.4",
722
"ext-json": "*",
823
"guzzlehttp/guzzle": "^6.2|^7.0"
924
},
1025
"autoload": {
1126
"psr-4": {
12-
"CoMagic\\": "src/CoMagic/"
27+
"Uiscom\\": "src/Uiscom/"
1328
}
1429
},
1530
"authors": [

src/CoMagic/CallApiClient.php renamed to src/Uiscom/CallApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace CoMagic;
5+
namespace Uiscom;
66

77
use GuzzleHttp\Client;
88
use GuzzleHttp\Exception\TransferException;

src/CoMagic/CallApiConfig.php renamed to src/Uiscom/CallApiConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace CoMagic;
5+
namespace Uiscom;
66

77
use InvalidArgumentException;
88

src/Uiscom/DataApiClient.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Uiscom;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\TransferException;
7+
use stdClass;
8+
9+
class DataApiClient
10+
{
11+
private string $version = 'v2.0';
12+
private DataApiConfig $config;
13+
private Client $client;
14+
15+
public function __construct(DataApiConfig $config, ?Client $client = null)
16+
{
17+
$this->client = $client ?? new Client([
18+
'headers' => [
19+
'Accept' => 'application/json',
20+
'Content-type' => 'application/json; charset=UTF-8',
21+
],
22+
]);
23+
$this->config = $config;
24+
}
25+
26+
private function getBaseUri(): string
27+
{
28+
return rtrim($this->config->getEntryPoint(), '/') .
29+
'/' . $this->version;
30+
}
31+
32+
/**
33+
* Get last response metadata
34+
*
35+
*/
36+
public function metadata(): ?stdClass
37+
{
38+
return $this->metadata;
39+
}
40+
41+
/**
42+
* Magic method for API calls
43+
*
44+
*/
45+
public function __call(string $camelCaseMethod, array $arguments)
46+
{
47+
$camelCaseMethod = preg_replace(
48+
'~(.)(?=[A-Z])~',
49+
'$1_',
50+
$camelCaseMethod
51+
);
52+
53+
$method = strtolower(preg_replace('~_~', '.', $camelCaseMethod, 1));
54+
55+
$params = ['access_token' => $this->config->getAccessToken()];
56+
if (isset($arguments[0])) {
57+
$params = array_merge($params, $arguments[0]);
58+
}
59+
60+
return $this->doRequest($method, $params);
61+
}
62+
63+
/**
64+
* @throws \Exception
65+
*/
66+
private function doRequest(string $method, array $params)
67+
{
68+
$payload = [
69+
'jsonrpc' => '2.0',
70+
'id' => time(),
71+
'method' => $method,
72+
'params' => $params,
73+
];
74+
75+
try {
76+
$response = $this->client->post($this->getBaseUri(), ['json' => $payload]);
77+
78+
$responseBody = json_decode($response->getBody()->getContents());
79+
80+
if (isset($responseBody->result)) {
81+
$this->metadata = $responseBody->result->metadata;
82+
}
83+
84+
if (isset($responseBody->error)) {
85+
throw new \Exception(
86+
"{$responseBody->error->code} {$responseBody->error->message}"
87+
);
88+
}
89+
90+
return $responseBody->result->data;
91+
} catch (TransferException $e) {
92+
throw new \Exception($e->getMessage());
93+
}
94+
}
95+
}

src/Uiscom/DataApiConfig.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Uiscom;
6+
7+
use InvalidArgumentException;
8+
9+
final class DataApiConfig
10+
{
11+
private ?string $accessToken;
12+
13+
private string $entryPoint;
14+
15+
public function __construct(
16+
?string $accessToken = null,
17+
?string $entryPoint = null
18+
) {
19+
if (empty($accessToken)) {
20+
throw new InvalidArgumentException(
21+
'Access token is required'
22+
);
23+
}
24+
25+
$this->accessToken = $accessToken;
26+
$this->entryPoint = $entryPoint ?: 'https://dataapi.comagic.ru/';
27+
}
28+
29+
public function getAccessToken(): ?string
30+
{
31+
return $this->accessToken;
32+
}
33+
34+
public function getEntryPoint(): ?string
35+
{
36+
return $this->entryPoint;
37+
}
38+
}

tests/CallApiClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use CoMagic\CallApiClient;
6-
use CoMagic\CallApiConfig;
5+
use Uiscom\CallApiClient;
6+
use Uiscom\CallApiConfig;
77
use GuzzleHttp\Client;
88
use GuzzleHttp\Psr7\Response;
99
use PHPUnit\Framework\TestCase;

tests/CallApiConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use CoMagic\CallApiConfig;
5+
use Uiscom\CallApiConfig;
66
use PHPUnit\Framework\TestCase;
77

88
/** @covers CallApiConfig */

0 commit comments

Comments
 (0)