1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\DoneDone; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use TestMonitor\DoneDone\Exceptions\Exception; |
8
|
|
|
use TestMonitor\DoneDone\Exceptions\NotFoundException; |
9
|
|
|
use TestMonitor\DoneDone\Exceptions\ValidationException; |
10
|
|
|
use TestMonitor\DoneDone\Exceptions\FailedActionException; |
11
|
|
|
use TestMonitor\DoneDone\Exceptions\UnauthorizedException; |
12
|
|
|
|
13
|
|
|
class Client |
14
|
|
|
{ |
15
|
|
|
use Actions\ManagesAccounts, |
|
|
|
|
16
|
|
|
Actions\ManagesPriorities, |
17
|
|
|
Actions\ManagesProjects, |
18
|
|
|
Actions\ManagesStatuses, |
19
|
|
|
Actions\ManagesTasks; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $username; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $token; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $url; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \GuzzleHttp\Client |
38
|
|
|
*/ |
39
|
|
|
protected $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new client instance. |
43
|
|
|
* |
44
|
|
|
* @param string $username |
45
|
|
|
* @param string $token |
46
|
|
|
*/ |
47
|
32 |
|
public function __construct(string $username, string $token) |
48
|
|
|
{ |
49
|
32 |
|
$this->username = $username; |
50
|
32 |
|
$this->token = $token; |
51
|
32 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns an DoneDone client instance. |
55
|
|
|
* |
56
|
|
|
* @return \GuzzleHttp\Client |
57
|
|
|
*/ |
58
|
32 |
|
protected function client() |
59
|
|
|
{ |
60
|
32 |
|
return $this->client ?? new GuzzleClient([ |
61
|
|
|
'auth' => [ |
62
|
32 |
|
$this->username, |
63
|
|
|
$this->token, |
64
|
|
|
], |
65
|
|
|
'base_uri' => 'https://2.donedone.com/public-api/', |
66
|
|
|
'headers' => [ |
67
|
|
|
'Accept' => 'application/json', |
68
|
|
|
'Content-Type' => 'application/json; charset=utf-8', |
69
|
|
|
], |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \GuzzleHttp\Client $client |
75
|
|
|
*/ |
76
|
32 |
|
public function setClient(GuzzleClient $client) |
77
|
|
|
{ |
78
|
32 |
|
$this->client = $client; |
79
|
32 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Make a GET request to DoneDone servers and return the response. |
83
|
|
|
* |
84
|
|
|
* @param string $uri |
85
|
|
|
* |
86
|
|
|
* @param array $payload |
87
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
88
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\FailedActionException |
89
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\NotFoundException |
90
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\ValidationException |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
32 |
|
protected function get($uri, array $payload = []) |
94
|
|
|
{ |
95
|
32 |
|
return $this->request('GET', $uri, $payload); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Make a POST request to DoneDone servers and return the response. |
100
|
|
|
* |
101
|
|
|
* @param string $uri |
102
|
|
|
* @param array $payload |
103
|
|
|
* |
104
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
105
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\FailedActionException |
106
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\NotFoundException |
107
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\ValidationException |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
1 |
|
protected function post($uri, array $payload = []) |
111
|
|
|
{ |
112
|
1 |
|
return $this->request('POST', $uri, $payload); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Make request to DoneDone servers and return the response. |
117
|
|
|
* |
118
|
|
|
* @param string $verb |
119
|
|
|
* @param string $uri |
120
|
|
|
* @param array $payload |
121
|
|
|
* |
122
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
123
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\FailedActionException |
124
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\NotFoundException |
125
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\ValidationException |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
32 |
|
protected function request($verb, $uri, array $payload = []) |
129
|
|
|
{ |
130
|
32 |
|
$response = $this->client()->request( |
131
|
32 |
|
$verb, |
132
|
|
|
$uri, |
133
|
|
|
$payload |
134
|
|
|
); |
135
|
|
|
|
136
|
32 |
|
if (! in_array($response->getStatusCode(), [200, 201, 204, 206])) { |
137
|
24 |
|
return $this->handleRequestError($response); |
138
|
|
|
} |
139
|
|
|
|
140
|
8 |
|
$responseBody = (string) $response->getBody(); |
141
|
|
|
|
142
|
8 |
|
return json_decode($responseBody, true) ?: $responseBody; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
147
|
|
|
* |
148
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\ValidationException |
149
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\NotFoundException |
150
|
|
|
* @throws \TestMonitor\DoneDone\Exceptions\FailedActionException |
151
|
|
|
* @throws \Exception |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
24 |
|
protected function handleRequestError(ResponseInterface $response) |
155
|
|
|
{ |
156
|
24 |
|
if ($response->getStatusCode() == 422) { |
157
|
7 |
|
throw new ValidationException(json_decode((string) $response->getBody(), true)); |
158
|
|
|
} |
159
|
|
|
|
160
|
17 |
|
if ($response->getStatusCode() == 404) { |
161
|
5 |
|
throw new NotFoundException(); |
162
|
|
|
} |
163
|
|
|
|
164
|
12 |
|
if ($response->getStatusCode() == 401 || $response->getStatusCode() == 403) { |
165
|
5 |
|
throw new UnauthorizedException(); |
166
|
|
|
} |
167
|
|
|
|
168
|
7 |
|
if ($response->getStatusCode() == 400) { |
169
|
5 |
|
throw new FailedActionException((string) $response->getBody()); |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
throw new Exception((string) $response->getStatusCode()); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|