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