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