|
| 1 | +<?php |
| 2 | + |
| 3 | +namespaceCoding; |
| 4 | + |
| 5 | +useCoding\Exceptions\ApiError; |
| 6 | +useCoding\Exceptions\ValidationException; |
| 7 | +useGuzzleHttp\ClientasGuzzleClient; |
| 8 | +useGuzzleHttp\ClientInterface; |
| 9 | +useInvalidArgumentException; |
| 10 | + |
| 11 | +class Client |
| 12 | +{ |
| 13 | +privateconstAPI_URL = 'https://e.coding.net/open-api'; |
| 14 | + |
| 15 | +protectedarray$config; |
| 16 | +protectedstring$token; |
| 17 | +publicbool$usePersonalToken = false; |
| 18 | +protected ?ClientInterface$http = null; |
| 19 | + |
| 20 | +publicfunction__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 | +publicfunctionsetHttpClient(ClientInterface$http): void |
| 31 | +{ |
| 32 | +$this->http = $http; |
| 33 | + } |
| 34 | + |
| 35 | +publicfunctiongetHttpClient(): ClientInterface |
| 36 | +{ |
| 37 | +if (null === $this->http){ |
| 38 | +$this->http = newGuzzleClient(); |
| 39 | + } |
| 40 | + |
| 41 | +return$this->http; |
| 42 | + } |
| 43 | + |
| 44 | +publicfunctionrequest(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 | +thrownewApiError($result['Response']['Error']['Message']); |
| 58 | + } |
| 59 | +return$result['Response']; |
| 60 | + } |
| 61 | + |
| 62 | +publicfunctionrequestProjectApi(string$action, array$data = []) |
| 63 | +{ |
| 64 | +if (empty($this->config['project_name'])){ |
| 65 | +thrownewValidationException('Need set project name first.'); |
| 66 | + } |
| 67 | +$data['ProjectName'] = $this->config['project_name']; |
| 68 | +return$this->request($action, $data); |
| 69 | + } |
| 70 | + |
| 71 | +publicfunctionsetPersonalToken(string$token) |
| 72 | +{ |
| 73 | +$this->config['personal_token'] = $token; |
| 74 | + } |
| 75 | + |
| 76 | +publicfunctionsetProjectName(string$projectName) |
| 77 | +{ |
| 78 | +$this->config['project_name'] = $projectName; |
| 79 | + } |
| 80 | + |
| 81 | +publicfunctionsetProjectToken(string$token) |
| 82 | +{ |
| 83 | +$this->config['project_token'] = $token; |
| 84 | + } |
| 85 | + |
| 86 | +privatefunctiongetToken() |
| 87 | +{ |
| 88 | +if ($this->usePersonalToken){ |
| 89 | +if (empty($this->config['personal_token'])){ |
| 90 | +thrownewInvalidArgumentException('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