1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the bitbucket-api package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandru Guzinschi <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bitbucket\API; |
12
|
|
|
|
13
|
|
|
use Bitbucket\API\Http\Plugin\ApiOneCollectionPlugin; |
14
|
|
|
use Bitbucket\API\Http\ClientInterface; |
15
|
|
|
use Bitbucket\API\Http\Client; |
16
|
|
|
use Bitbucket\API\Http\Plugin\NormalizeArrayPlugin; |
17
|
|
|
use Http\Client\Common\Plugin; |
18
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
19
|
|
|
use Http\Message\Authentication; |
20
|
|
|
use Psr\Http\Message\MessageInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Alexandru Guzinschi <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Api |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Api response codes. |
29
|
|
|
*/ |
30
|
|
|
const HTTP_RESPONSE_OK = 200; |
31
|
|
|
const HTTP_RESPONSE_CREATED = 201; |
32
|
|
|
const HTTP_RESPONSE_NO_CONTENT = 204; |
33
|
|
|
const HTTP_RESPONSE_BAD_REQUEST = 400; |
34
|
|
|
const HTTP_RESPONSE_UNAUTHORIZED = 401; |
35
|
|
|
const HTTP_RESPONSE_FORBIDDEN = 403; |
36
|
|
|
const HTTP_RESPONSE_NOT_FOUND = 404; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClientInterface |
40
|
|
|
*/ |
41
|
|
|
protected $httpClient; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $options |
45
|
|
|
* @param ClientInterface $client |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $options = array(), ClientInterface $client = null) |
48
|
|
|
{ |
49
|
|
|
$this->httpClient = (null !== $client) ? $client : new Client($options, null); |
50
|
289 |
|
|
51
|
|
|
$this->addPlugin(new NormalizeArrayPlugin()); |
52
|
289 |
|
$this->addPlugin(new ApiOneCollectionPlugin()); |
53
|
|
|
} |
54
|
289 |
|
|
55
|
289 |
|
/** |
56
|
289 |
|
* @access public |
57
|
|
|
* @return ClientInterface |
58
|
|
|
*/ |
59
|
|
|
public function getClient() |
60
|
|
|
{ |
61
|
|
|
return $this->httpClient; |
62
|
119 |
|
} |
63
|
|
|
|
64
|
119 |
|
/** |
65
|
|
|
* @access public |
66
|
|
|
* @param ClientInterface $client |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setClient(ClientInterface $client) |
70
|
|
|
{ |
71
|
|
|
$this->httpClient = $client; |
72
|
123 |
|
|
73
|
|
|
return $this; |
74
|
123 |
|
} |
75
|
|
|
|
76
|
123 |
|
/** |
77
|
|
|
* Set API login credentials |
78
|
|
|
* |
79
|
|
|
* @param Authentication $authentication |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function setCredentials(Authentication $authentication) |
83
|
|
|
{ |
84
|
|
|
$this->addPlugin(new AuthenticationPlugin($authentication)); |
85
|
|
|
} |
86
|
11 |
|
|
87
|
|
|
public function addPlugin(Plugin $plugin) |
88
|
|
|
{ |
89
|
11 |
|
$this->httpClient->getClientBuilder()->removePlugin(get_class($plugin)); |
90
|
11 |
|
$this->httpClient->getClientBuilder()->addPlugin($plugin); |
91
|
11 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Make an HTTP GET request to API |
95
|
11 |
|
* |
96
|
11 |
|
* @access public |
97
|
|
|
* @param string $endpoint API endpoint |
98
|
|
|
* @param string|array $params GET parameters |
99
|
|
|
* @param array $headers HTTP headers |
100
|
|
|
* @return MessageInterface |
101
|
|
|
*/ |
102
|
|
|
public function requestGet($endpoint, $params = array(), $headers = array()) |
103
|
|
|
{ |
104
|
|
|
return $this->getClient()->get($endpoint, $params, $headers); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
/** |
108
|
|
|
* Make an HTTP POST request to API |
109
|
2 |
|
* |
110
|
|
|
* @access public |
111
|
|
|
* @param string $endpoint API endpoint |
112
|
|
|
* @param string|array $params POST parameters |
113
|
|
|
* @param array $headers HTTP headers |
114
|
|
|
* @return MessageInterface |
115
|
|
|
*/ |
116
|
|
|
public function requestPost($endpoint, $params = array(), $headers = array()) |
117
|
|
|
{ |
118
|
|
|
return $this->getClient()->post($endpoint, $params, $headers); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
/** |
122
|
|
|
* Make an HTTP PUT request to API |
123
|
1 |
|
* |
124
|
|
|
* @access public |
125
|
|
|
* @param string $endpoint API endpoint |
126
|
|
|
* @param string|array $params POST parameters |
127
|
|
|
* @param array $headers HTTP headers |
128
|
|
|
* @return MessageInterface |
129
|
|
|
*/ |
130
|
|
|
public function requestPut($endpoint, $params = array(), $headers = array()) |
131
|
|
|
{ |
132
|
|
|
return $this->getClient()->put($endpoint, $params, $headers); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
/** |
136
|
|
|
* Make a HTTP DELETE request to API |
137
|
1 |
|
* |
138
|
|
|
* @access public |
139
|
|
|
* @param string $endpoint API endpoint |
140
|
|
|
* @param string|array $params DELETE parameters |
141
|
|
|
* @param array $headers HTTP headers |
142
|
|
|
* @return MessageInterface |
143
|
|
|
*/ |
144
|
|
|
public function requestDelete($endpoint, $params = array(), $headers = array()) |
145
|
|
|
{ |
146
|
|
|
return $this->getClient()->delete($endpoint, $params, $headers); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
/** |
150
|
|
|
* Create HTTP request |
151
|
1 |
|
* |
152
|
|
|
* @access protected |
153
|
|
|
* @param string $method HTTP method |
154
|
|
|
* @param string $endpoint Api endpoint |
155
|
|
|
* @param string|array $params Request parameter(s) |
156
|
|
|
* @param array $headers HTTP headers |
157
|
|
|
* @return MessageInterface |
158
|
|
|
* |
159
|
|
|
* @throws \RuntimeException |
160
|
|
|
*/ |
161
|
|
|
protected function doRequest($method, $endpoint, $params, array $headers) |
162
|
|
|
{ |
163
|
|
|
return $this->getClient()->request($endpoint, $params, $method, $headers); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the preferred format for response |
168
|
|
|
* |
169
|
|
|
* @access public |
170
|
|
|
* @param string $name Format name |
171
|
|
|
* @return self |
172
|
|
|
* |
173
|
|
|
* @throws \InvalidArgumentException |
174
|
|
|
*/ |
175
|
|
|
public function setFormat($name) |
176
|
|
|
{ |
177
|
|
|
$this->getClient()->setResponseFormat($name); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
2 |
|
} |
181
|
|
|
|
182
|
2 |
|
/** |
183
|
|
|
* Get current format used for response |
184
|
2 |
|
* |
185
|
|
|
* @access public |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getFormat() |
189
|
|
|
{ |
190
|
|
|
return $this->getClient()->getResponseFormat(); |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
/** |
194
|
|
|
* @param string $name |
195
|
2 |
|
* @return Api |
196
|
|
|
* |
197
|
|
|
* @throws \InvalidArgumentException |
198
|
|
|
*/ |
199
|
|
|
public function api($name) |
200
|
|
|
{ |
201
|
|
|
if (!is_string($name) || $name === '') { |
202
|
|
|
throw new \InvalidArgumentException('No child specified.'); |
203
|
|
|
} |
204
|
12 |
|
|
205
|
|
|
if (class_exists($name)) { |
206
|
12 |
|
$class = $name; |
207
|
3 |
|
} else { |
208
|
|
|
$class = '\\Bitbucket\\API\\'.$name; |
209
|
|
|
|
210
|
9 |
|
if (!class_exists($class)) { |
211
|
|
|
throw new \InvalidArgumentException(sprintf('No such child class [%s].', $name)); |
212
|
|
|
} |
213
|
|
|
} |
214
|
9 |
|
|
215
|
|
|
/** @var Api $child */ |
216
|
9 |
|
$child = new $class(); |
217
|
2 |
|
$child->setClient($this->getClient()); |
218
|
|
|
|
219
|
|
|
return $child; |
220
|
|
|
} |
221
|
7 |
|
|
222
|
7 |
|
/** |
223
|
|
|
* @access public |
224
|
7 |
|
* @return void |
225
|
6 |
|
*/ |
226
|
|
|
public function __clone() |
227
|
|
|
{ |
228
|
7 |
|
// prevent reference to the same HTTP client. |
229
|
|
|
$this->setClient(clone $this->getClient()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Convert JSON to array with error check |
234
|
|
|
* |
235
|
1 |
|
* @access protected |
236
|
|
|
* @param string $body JSON data |
237
|
|
|
* @return array |
238
|
1 |
|
* |
239
|
1 |
|
* @throws \InvalidArgumentException |
240
|
|
|
*/ |
241
|
|
|
protected function decodeJSON($body) |
242
|
|
|
{ |
243
|
|
|
$params = json_decode($body, true); |
244
|
|
|
|
245
|
|
|
if (!is_array($params) || (JSON_ERROR_NONE !== json_last_error())) { |
246
|
|
|
throw new \InvalidArgumentException('Invalid JSON data provided.'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $params; |
250
|
15 |
|
} |
251
|
|
|
} |
252
|
|
|
|