-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
99 lines (84 loc) · 2.83 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace Coding;
use Coding\Exceptions\ApiError;
use Coding\Exceptions\ValidationException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use InvalidArgumentException;
class Client
{
private const API_URL = 'https://e.coding.net/open-api';
protected array $config;
protected string $token;
public bool $usePersonalToken = false;
protected ?ClientInterface $http = null;
public function __construct(array $config = [])
{
$this->config = array_merge([
'api_url' => self::API_URL,
'personal_token' => '',
'project_name' => '',
'project_token' => '',
], $config);
}
public function setHttpClient(ClientInterface $http): void
{
$this->http = $http;
}
public function getHttpClient(): ClientInterface
{
if (is_null($this->http)) {
$this->http = new GuzzleClient();
}
return $this->http;
}
public function request(string $action, array $data): array
{
$params = ['Action' => $action];
$response = $this->getHttpClient()->request('POST', $this->config['api_url'], [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'token ' . $this->getToken(),
'Content-Type' => 'application/json'
],
'json' => array_merge($params, $data),
]);
$result = json_decode($response->getBody()->getContents(), true);
if (isset($result['Response']['Error']['Message'])) {
throw new ApiError($result['Response']['Error']['Message']);
}
return $result['Response'];
}
public function requestProjectApi(string $action, array $data = []): array
{
if (empty($this->config['project_name'])) {
throw new ValidationException('Need set project name first.');
}
$data['ProjectName'] = $this->config['project_name'];
return $this->request($action, $data);
}
public function setPersonalToken(string $token): void
{
$this->config['personal_token'] = $token;
}
public function setProjectName(string $projectName): void
{
$this->config['project_name'] = $projectName;
}
public function setProjectToken(string $token): void
{
$this->config['project_token'] = $token;
}
private function getToken(): string
{
if ($this->usePersonalToken) {
if (empty($this->config['personal_token'])) {
throw new InvalidArgumentException('Need set project token first.');
}
return $this->config['personal_token'];
}
return !empty($this->config['project_token']) ?
$this->config['project_token'] : $this->config['personal_token'];
}
}