forked from gentlero/bitbucket-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.php
257 lines (220 loc) · 7.86 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* This file is part of the bitbucket-api package.
*
* (c) Alexandru G. <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bitbucket\API\Http;
use Bitbucket\API\Http\Plugin\ApiVersionPlugin;
use Bitbucket\API\Http\Plugin\HistoryPlugin;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
/**
* @author Alexandru G. <[email protected]>
* @phpstan-type ClientOption array{base_url: string, api_version: string, api_versions: string[], format: string, formats: string[], user_agent: string, timeout: int, verify_peer: bool}
* @phpstan-type OptionalClientOption array{base_url?: string, api_version?: string, api_versions?: string[], format?: string, formats?: string[], user_agent?: string, timeout?: int, verify_peer?: bool}
*/
class Client implements ClientInterface
{
/** @var ClientOption */
protected $options = array(
'base_url' => 'https://api.bitbucket.org',
'api_version' => '2.0',
'api_versions' => array('1.0', '2.0'), // supported versions
'format' => 'json',
'formats' => array('json', 'xml'), // supported response formats
'user_agent' => 'bitbucket-api-php/2.0.0 (https://bitbucket.org/gentlero/bitbucket-api)',
'timeout' => 10,
'verify_peer' => true
);
/** @var HttpPluginClientBuilder */
private $httpClientBuilder;
/** @var RequestFactoryInterface */
private $requestFactory;
/** @var StreamFactoryInterface */
private $streamFactory;
/** @var HistoryPlugin */
private $responseHistory;
/**
* @param OptionalClientOption $options
*/
public function __construct(array $options = array(), HttpPluginClientBuilder $httpClientBuilder = null)
{
$this->responseHistory = new HistoryPlugin();
$this->options = array_merge(array_merge($this->options, $options));
$this->httpClientBuilder = $httpClientBuilder ?: new HttpPluginClientBuilder();
$this->httpClientBuilder->addPlugin(
new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($this->options['base_url']))
);
$this->httpClientBuilder->addPlugin(new Plugin\RedirectPlugin());
$this->httpClientBuilder->addPlugin(new Plugin\HeaderDefaultsPlugin([
'User-Agent' => $this->options['user_agent'],
]));
$this->httpClientBuilder->addPlugin(new Plugin\HistoryPlugin($this->responseHistory));
$this->setApiVersion($this->options['api_version']);
$this->requestFactory = $this->httpClientBuilder->getRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}
/**
* {@inheritdoc}
*/
public function get($endpoint, $params = array(), $headers = array())
{
if (is_array($params) && count($params) > 0) {
$endpoint .= (strpos($endpoint, '?') === false ? '?' : '&').http_build_query($params, '', '&');
$params = array();
}
return $this->request($endpoint, $params, 'GET', $headers);
}
/**
* {@inheritdoc}
*/
public function post($endpoint, $params = array(), $headers = array())
{
return $this->request($endpoint, $params, 'POST', $headers);
}
/**
* {@inheritdoc}
*/
public function put($endpoint, $params = array(), $headers = array())
{
return $this->request($endpoint, $params, 'PUT', $headers);
}
/**
* {@inheritdoc}
*/
public function delete($endpoint, $params = array(), $headers = array())
{
return $this->request($endpoint, $params, 'DELETE', $headers);
}
/**
* {@inheritdoc}
*/
public function request($endpoint, $params = array(), $method = 'GET', array $headers = array())
{
// add a default content-type if none was set
if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
$paramsString = null;
if (is_array($params) && count($params) > 0) {
if (isset($headers['Content-Type']) && $headers['Content-Type'] === 'application/json') {
$paramsString = json_encode($params);
} else {
$paramsString = http_build_query($params);
}
}
$body = '';
if (is_string($paramsString) && $paramsString !== null) {
$body = $paramsString;
}
if (is_string($params) && $params !== null) {
trigger_deprecation('private-packagist/bitbucket-api', '2.2', 'Calling Client::request() with a string as params argument is deprecated. Pass an array instead.');
$body = $params;
}
// change the response format
if ($this->getApiVersion() === '1.0' && strpos($endpoint, 'format=') === false) {
$endpoint .= (strpos($endpoint, '?') === false ? '?' : '&').'format='.$this->getResponseFormat();
}
$request = $this->requestFactory
->createRequest($method, $endpoint)
->withBody($this->streamFactory->createStream($body));
foreach ($headers as $name => $value) {
$request = $request->withHeader($name, $value);
}
return $this->getClient()->sendRequest($request);
}
/**
* @return HttpMethodsClient
*/
public function getClient()
{
return $this->httpClientBuilder->getHttpClient();
}
/**
* @return HttpPluginClientBuilder
*/
public function getClientBuilder()
{
return $this->httpClientBuilder;
}
/**
* {@inheritdoc}
*/
public function getResponseFormat()
{
return $this->options['format'];
}
/**
* {@inheritdoc}
*/
public function setResponseFormat($format)
{
if (!in_array($format, $this->options['formats'], true)) {
throw new \InvalidArgumentException(sprintf('Unsupported response format %s', $format));
}
$this->options['format'] = $format;
return $this;
}
/**
* {@inheritdoc}
*/
public function getApiVersion()
{
return $this->options['api_version'];
}
/**
* {@inheritdoc}
*/
public function setApiVersion($version)
{
if (!in_array($version, $this->options['api_versions'], true)) {
throw new \InvalidArgumentException(sprintf('Unsupported API version %s', $version));
}
if (!$this->isApiVersion($version) || !$this->httpClientBuilder->hasPlugin(ApiVersionPlugin::class)) {
$this->options['api_version'] = $version;
$this->httpClientBuilder->removePlugin(ApiVersionPlugin::class);
$this->httpClientBuilder->addPlugin(new ApiVersionPlugin($this->options['api_version']));
}
return $this;
}
/**
* Check if specified API version is the one currently in use.
*
* @param string $version
* @return bool
*/
public function isApiVersion($version)
{
return (float) $version === (float) $this->options['api_version'];
}
/**
* @return string
*/
public function getApiBaseUrl()
{
return $this->options['base_url'].'/'.$this->getApiVersion();
}
/**
* @return RequestInterface
*/
public function getLastRequest()
{
return $this->responseHistory->getLastRequest();
}
/**
* @return ResponseInterface
*/
public function getLastResponse()
{
return $this->responseHistory->getLastResponse();
}
}