We have detected an error in your notification set-up
(Event-ID dab39dc24f564ec7bd4628d1305fd03c).
Currently, we cannot inform you about inspection progress.
Please check that the user
557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or
update the API account.
| 1 | <?php |
||
| 24 | class Client extends ClientListener implements ClientInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $options = array( |
||
| 30 | 'base_url' => 'https://api.bitbucket.org', |
||
| 31 | 'api_version' => '1.0', |
||
| 32 | 'api_versions' => array('1.0', '2.0'), // supported versions |
||
| 33 | 'format' => 'json', |
||
| 34 | 'formats' => array('json', 'xml'), // supported response formats |
||
| 35 | 'user_agent' => 'bitbucket-api-php/1.0.0-dev (https://bitbucket.org/gentlero/bitbucket-api)', |
||
| 36 | 'timeout' => 10, |
||
| 37 | 'verify_peer' => true |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BuzzClientInterface |
||
| 42 | */ |
||
| 43 | protected $client; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RequestInterface |
||
| 47 | */ |
||
| 48 | private $lastRequest; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var MessageInterface |
||
| 52 | */ |
||
| 53 | private $lastResponse; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var MessageInterface |
||
| 57 | */ |
||
| 58 | protected $responseObj; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var RequestInterface |
||
| 62 | */ |
||
| 63 | protected $requestObj; |
||
| 64 | |||
| 65 | 271 | public function __construct(array $options = array(), BuzzClientInterface $client = null) |
|
| 66 | { |
||
| 67 | 271 | $this->client = (null === $client) ? new Curl() : $client; |
|
| 68 | 271 | $this->options = array_merge($this->options, $options); |
|
| 69 | |||
| 70 | 271 | $this->client->setTimeout($this->options['timeout']); |
|
| 71 | 271 | $this->client->setVerifyPeer($this->options['verify_peer']); |
|
| 72 | 271 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 5 | public function get($endpoint, $params = array(), $headers = array()) |
|
| 78 | { |
||
| 79 | 5 | if (is_array($params) && count($params) > 0) { |
|
| 80 | 1 | $endpoint .= (strpos($endpoint, '?') === false ? '?' : '&').http_build_query($params, '', '&'); |
|
| 81 | 1 | $params = array(); |
|
| 82 | 1 | } |
|
| 83 | |||
| 84 | 5 | return $this->request($endpoint, $params, 'GET', $headers); |
|
| 85 | 1 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 2 | public function post($endpoint, $params = array(), $headers = array()) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 1 | public function put($endpoint, $params = array(), $headers = array()) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 2 | public function delete($endpoint, $params = array(), $headers = array()) |
|
| 107 | { |
||
| 108 | 2 | return $this->request($endpoint, $params, 'DELETE', $headers); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 10 | public function request($endpoint, $params = array(), $method = 'GET', array $headers = array()) |
|
| 115 | { |
||
| 116 | 10 | $request = $this->createRequest($method, $endpoint); |
|
| 117 | |||
| 118 | // add a default content-type if none was set |
||
| 119 | 10 | if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) { |
|
| 120 | 3 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 121 | 3 | } |
|
| 122 | |||
| 123 | 10 | if (count($headers) > 0) { |
|
| 124 | 8 | $request->addHeaders($headers); |
|
| 125 | 8 | } |
|
| 126 | |||
| 127 | 10 | if (count($params) > 0) { |
|
| 128 | 5 | $request->setContent(is_array($params) ? http_build_query($params) : $params); |
|
| 129 | 5 | } |
|
| 130 | |||
| 131 | 10 | $response = is_object($this->responseObj) ? $this->responseObj : new Response(); |
|
| 132 | |||
| 133 | 10 | $this->executeListeners($request, 'preSend'); |
|
| 134 | |||
| 135 | 9 | $this->client->send($request, $response); |
|
| 136 | |||
| 137 | 9 | $this->executeListeners($request, 'postSend', $response); |
|
| 138 | |||
| 139 | 9 | $this->lastRequest = $request; |
|
| 140 | 9 | $this->lastResponse = $response; |
|
| 141 | |||
| 142 | 9 | return $response; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @access public |
||
| 147 | * @return BuzzClientInterface |
||
| 148 | */ |
||
| 149 | 1 | public function getClient() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | 12 | public function getResponseFormat() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | 4 | public function setResponseFormat($format) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | 12 | public function getApiVersion() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 66 | public function setApiVersion($version) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Check if specified API version is the one currently in use. |
||
| 200 | * |
||
| 201 | * @access public |
||
| 202 | * @param float $version |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 1 | public function isApiVersion($version) |
|
| 206 | { |
||
| 207 | 1 | return abs($this->options['api_version'] - $version) < 0.00001; |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | 11 | public function getApiBaseUrl() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @access public |
||
| 220 | * @return MessageInterface |
||
| 221 | */ |
||
| 222 | 5 | public function getLastRequest() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @access public |
||
| 229 | * @return RequestInterface |
||
| 230 | */ |
||
| 231 | 2 | public function getLastResponse() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @access public |
||
| 238 | * @param MessageInterface $response |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function setResponse(MessageInterface $response) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @access public |
||
| 248 | * @param RequestInterface $request |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function setRequest(RequestInterface $request) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @access protected |
||
| 258 | * @param string $method |
||
| 259 | * @param string $url |
||
| 260 | * @return RequestInterface |
||
| 261 | */ |
||
| 262 | 10 | protected function createRequest($method, $url) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Execute all available listeners. |
||
| 287 | * |
||
| 288 | * $when can be: preSend or postSend |
||
| 289 | * |
||
| 290 | * @access protected |
||
| 291 | * @param RequestInterface $request |
||
| 292 | * @param string $when When to execute the listener |
||
| 293 | * @param MessageInterface $response |
||
| 294 | */ |
||
| 295 | 10 | protected function executeListeners(RequestInterface $request, $when = 'preSend', MessageInterface $response = null) |
|
| 320 | } |
||
| 321 |