solutionDrive /
HipchatAPIv2Client
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SolutionDrive\HipchatAPIv2Client; |
||
| 4 | |||
| 5 | use Buzz\Browser; |
||
| 6 | use Buzz\Client\Curl; |
||
| 7 | use SolutionDrive\HipchatAPIv2Client\Auth\AuthInterface; |
||
| 8 | use SolutionDrive\HipchatAPIv2Client\Exception\RequestException; |
||
| 9 | |||
| 10 | class Client implements ClientInterface |
||
| 11 | { |
||
| 12 | protected $baseUrl; |
||
| 13 | |||
| 14 | /** @var AuthInterface */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var Browser */ |
||
| 18 | protected $browser; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Client constructor |
||
| 22 | * |
||
| 23 | * @param AuthInterface $auth Authentication you want to use to access the api |
||
| 24 | * @param Browser $browser Client you want to use, by default browser with curl will be used |
||
| 25 | * @param string $baseUrl URL to the HipChat server endpoint |
||
| 26 | * |
||
| 27 | * @return self |
||
| 28 | */ |
||
| 29 | 5 | public function __construct(AuthInterface $auth, Browser $browser = null, $baseUrl = 'https://api.hipchat.com') |
|
| 30 | { |
||
| 31 | 5 | $this->auth = $auth; |
|
| 32 | 5 | $this->baseUrl = $baseUrl; |
|
| 33 | 5 | if ($browser === null) { |
|
| 34 | $client = new Curl(); |
||
| 35 | $this->browser = new Browser($client); |
||
| 36 | } else { |
||
| 37 | 5 | $this->browser = $browser; |
|
| 38 | } |
||
| 39 | 5 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritdoc |
||
| 43 | */ |
||
| 44 | 1 | public function get($resource, $query = array()) |
|
| 45 | { |
||
| 46 | 1 | $url = $this->baseUrl . $resource; |
|
| 47 | 1 | $url = $this->addQuery($query, $url); |
|
| 48 | |||
| 49 | 1 | $headers = array("Authorization" => $this->auth->getCredential()); |
|
| 50 | |||
| 51 | try { |
||
| 52 | 1 | $response = $this->browser->get($url, $headers); |
|
| 53 | } catch (\Buzz\Exception\ClientException $e) { |
||
| 54 | throw new RequestException($e->getMessage(), $e->getCode(), '', $e); |
||
| 55 | } |
||
| 56 | |||
| 57 | 1 | $this->checkBrowserResponse(); |
|
| 58 | |||
| 59 | 1 | return json_decode($response->getContent(), true); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritdoc |
||
| 64 | */ |
||
| 65 | 1 | public function post($resource, $content) |
|
| 66 | { |
||
| 67 | 1 | $url = $this->baseUrl . $resource; |
|
| 68 | |||
| 69 | $headers = array( |
||
| 70 | 1 | 'Content-Type' => 'application/json', |
|
| 71 | 1 | 'Authorization' => $this->auth->getCredential() |
|
| 72 | ); |
||
| 73 | |||
| 74 | try { |
||
| 75 | 1 | $response = $this->browser->post($url, $headers, json_encode($content)); |
|
| 76 | } catch (\Buzz\Exception\ClientException $e) { |
||
| 77 | throw new RequestException($e->getMessage(), $e->getCode(), '', $e); |
||
| 78 | } |
||
| 79 | |||
| 80 | 1 | $this->checkBrowserResponse(); |
|
| 81 | |||
| 82 | 1 | return json_decode($response->getContent(), true); |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @inheritdoc |
||
| 87 | */ |
||
| 88 | 1 | public function put($resource, $content = array()) |
|
| 89 | { |
||
| 90 | 1 | $url = $this->baseUrl . $resource; |
|
| 91 | $headers = array( |
||
| 92 | 1 | 'Content-Type' => 'application/json', |
|
| 93 | 1 | 'Authorization' => $this->auth->getCredential() |
|
| 94 | ); |
||
| 95 | |||
| 96 | try { |
||
| 97 | 1 | $response = $this->browser->put($url, $headers, json_encode($content)); |
|
| 98 | } catch (\Buzz\Exception\ClientException $e) { |
||
| 99 | throw new RequestException($e->getMessage(), $e->getCode(), '', $e); |
||
| 100 | } |
||
| 101 | |||
| 102 | 1 | $this->checkBrowserResponse(); |
|
| 103 | |||
| 104 | 1 | return json_decode($response->getContent(), true); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | */ |
||
| 110 | 1 | public function delete($resource) |
|
| 111 | { |
||
| 112 | 1 | $url = $this->baseUrl . $resource; |
|
| 113 | |||
| 114 | $headers = array( |
||
| 115 | 1 | 'Authorization' => $this->auth->getCredential() |
|
| 116 | ); |
||
| 117 | |||
| 118 | try { |
||
| 119 | 1 | $response = $this->browser->delete($url, $headers); |
|
| 120 | } catch (\Buzz\Exception\ClientException $e) { |
||
| 121 | throw new RequestException($e->getMessage(), $e->getCode(), '', $e); |
||
| 122 | } |
||
| 123 | |||
| 124 | 1 | $this->checkBrowserResponse(); |
|
| 125 | |||
| 126 | 1 | return json_decode($response->getContent(), true); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add query string if necessary |
||
| 131 | * |
||
| 132 | * @param $query |
||
| 133 | * @param $url |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 1 | private function addQuery($query, $url) |
|
| 137 | { |
||
| 138 | 1 | if (count($query) > 0) { |
|
| 139 | $url .= "?"; |
||
| 140 | } |
||
| 141 | |||
| 142 | 1 | foreach ($query as $key => $value) { |
|
| 143 | $url .= "$key=$value&"; |
||
| 144 | } |
||
| 145 | 1 | return $url; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Checks if response was successful |
||
| 150 | * |
||
| 151 | * @throws RequestException |
||
| 152 | */ |
||
| 153 | 4 | private function checkBrowserResponse() |
|
| 154 | { |
||
| 155 | 4 | if ($this->browser->getLastResponse()->getStatusCode() > 299) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 156 | $response = json_decode($this->browser->getLastResponse()->getContent(), true); |
||
| 157 | $code = isset($response['error']['code']) ? $response['error']['code'] : 0; |
||
| 158 | $message = isset($response['error']['message']) ? $response['error']['message'] : ''; |
||
| 159 | $type = isset($response['error']['type']) ? $response['error']['type'] : ''; |
||
| 160 | throw new RequestException($message, $code, $type); |
||
| 161 | } |
||
| 162 | 4 | } |
|
| 163 | } |
||
| 164 |