@@ -8,130 +8,130 @@ |
||
| 8 | 8 | |
| 9 | 9 | class GuzzleHttpClient implements HttpClient |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var Client |
|
| 13 | - */ |
|
| 14 | - protected $client; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - protected $responseHeaders; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @param Client $client |
|
| 23 | - */ |
|
| 24 | - public function __construct(Client $client) |
|
| 25 | - { |
|
| 26 | - $this->client = $client; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param string $uri |
|
| 31 | - * @param array $params |
|
| 32 | - * @param array $headers |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function get($uri, array $params = [], array $headers = []) |
|
| 36 | - { |
|
| 37 | - if (!empty($params)) { |
|
| 38 | - $uri .= '?' . http_build_query($params); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $response = $this |
|
| 42 | - ->client |
|
| 43 | - ->get($uri, ['headers' => $headers]); |
|
| 44 | - |
|
| 45 | - return $this->parseResponse($response); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param string $uri |
|
| 50 | - * @param array $body |
|
| 51 | - * @param array $headers |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function post($uri, array $body = [], array $headers = []) |
|
| 55 | - { |
|
| 56 | - $response = $this |
|
| 57 | - ->client |
|
| 58 | - ->post( |
|
| 59 | - $uri, [ |
|
| 60 | - 'headers' => $headers, |
|
| 61 | - 'form_params' => $body, |
|
| 62 | - ] |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - return $this->parseResponse($response); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param string $uri |
|
| 70 | - * @param array $body |
|
| 71 | - * @param array $headers |
|
| 72 | - * @return mixed |
|
| 73 | - */ |
|
| 74 | - public function put($uri, array $body = [], array $headers = []) |
|
| 75 | - { |
|
| 76 | - $response = $this |
|
| 77 | - ->client |
|
| 78 | - ->put( |
|
| 79 | - $uri, [ |
|
| 80 | - 'headers' => $headers, |
|
| 81 | - 'form_params' => $body, |
|
| 82 | - ] |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - return $this->parseResponse($response); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string $uri |
|
| 90 | - * @param array $body |
|
| 91 | - * @param array $headers |
|
| 92 | - * @return mixed |
|
| 93 | - */ |
|
| 94 | - public function delete($uri, array $body = [], array $headers = []) |
|
| 95 | - { |
|
| 96 | - $response = $this |
|
| 97 | - ->client |
|
| 98 | - ->delete($uri, [ |
|
| 99 | - 'headers' => $headers, |
|
| 100 | - 'form_params' => $body, |
|
| 101 | - ]); |
|
| 102 | - |
|
| 103 | - return $this->parseResponse($response); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param string $url |
|
| 108 | - * @return $this |
|
| 109 | - */ |
|
| 110 | - public function setBaseUrl($url) |
|
| 111 | - { |
|
| 112 | - $this->client->setBaseUrl($url); |
|
| 113 | - |
|
| 114 | - return $this; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param ResponseInterface $response |
|
| 119 | - * @return array|null |
|
| 120 | - */ |
|
| 121 | - protected function parseResponse(ResponseInterface $response) |
|
| 122 | - { |
|
| 123 | - $responseContents = $response->getBody()->getContents(); |
|
| 124 | - |
|
| 125 | - $this->responseHeaders = $response->getHeaders(); |
|
| 126 | - |
|
| 127 | - return json_decode($responseContents, true); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return array |
|
| 132 | - */ |
|
| 133 | - public function getResponseHeaders() |
|
| 134 | - { |
|
| 135 | - return $this->responseHeaders; |
|
| 136 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Client |
|
| 13 | + */ |
|
| 14 | + protected $client; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + protected $responseHeaders; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @param Client $client |
|
| 23 | + */ |
|
| 24 | + public function __construct(Client $client) |
|
| 25 | + { |
|
| 26 | + $this->client = $client; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param string $uri |
|
| 31 | + * @param array $params |
|
| 32 | + * @param array $headers |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function get($uri, array $params = [], array $headers = []) |
|
| 36 | + { |
|
| 37 | + if (!empty($params)) { |
|
| 38 | + $uri .= '?' . http_build_query($params); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $response = $this |
|
| 42 | + ->client |
|
| 43 | + ->get($uri, ['headers' => $headers]); |
|
| 44 | + |
|
| 45 | + return $this->parseResponse($response); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param string $uri |
|
| 50 | + * @param array $body |
|
| 51 | + * @param array $headers |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function post($uri, array $body = [], array $headers = []) |
|
| 55 | + { |
|
| 56 | + $response = $this |
|
| 57 | + ->client |
|
| 58 | + ->post( |
|
| 59 | + $uri, [ |
|
| 60 | + 'headers' => $headers, |
|
| 61 | + 'form_params' => $body, |
|
| 62 | + ] |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + return $this->parseResponse($response); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param string $uri |
|
| 70 | + * @param array $body |
|
| 71 | + * @param array $headers |
|
| 72 | + * @return mixed |
|
| 73 | + */ |
|
| 74 | + public function put($uri, array $body = [], array $headers = []) |
|
| 75 | + { |
|
| 76 | + $response = $this |
|
| 77 | + ->client |
|
| 78 | + ->put( |
|
| 79 | + $uri, [ |
|
| 80 | + 'headers' => $headers, |
|
| 81 | + 'form_params' => $body, |
|
| 82 | + ] |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + return $this->parseResponse($response); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string $uri |
|
| 90 | + * @param array $body |
|
| 91 | + * @param array $headers |
|
| 92 | + * @return mixed |
|
| 93 | + */ |
|
| 94 | + public function delete($uri, array $body = [], array $headers = []) |
|
| 95 | + { |
|
| 96 | + $response = $this |
|
| 97 | + ->client |
|
| 98 | + ->delete($uri, [ |
|
| 99 | + 'headers' => $headers, |
|
| 100 | + 'form_params' => $body, |
|
| 101 | + ]); |
|
| 102 | + |
|
| 103 | + return $this->parseResponse($response); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param string $url |
|
| 108 | + * @return $this |
|
| 109 | + */ |
|
| 110 | + public function setBaseUrl($url) |
|
| 111 | + { |
|
| 112 | + $this->client->setBaseUrl($url); |
|
| 113 | + |
|
| 114 | + return $this; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param ResponseInterface $response |
|
| 119 | + * @return array|null |
|
| 120 | + */ |
|
| 121 | + protected function parseResponse(ResponseInterface $response) |
|
| 122 | + { |
|
| 123 | + $responseContents = $response->getBody()->getContents(); |
|
| 124 | + |
|
| 125 | + $this->responseHeaders = $response->getHeaders(); |
|
| 126 | + |
|
| 127 | + return json_decode($responseContents, true); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return array |
|
| 132 | + */ |
|
| 133 | + public function getResponseHeaders() |
|
| 134 | + { |
|
| 135 | + return $this->responseHeaders; |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -31,125 +31,125 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Api |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $organizationId; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var HttpClient |
|
| 41 | - */ |
|
| 42 | - private $httpClient; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var Endpoint[] |
|
| 46 | - */ |
|
| 47 | - private $endpoints; |
|
| 48 | - |
|
| 49 | - public function __construct(HttpClient $httpClient, Endpoint ...$endpoints) |
|
| 50 | - { |
|
| 51 | - $this->httpClient = $httpClient; |
|
| 52 | - foreach ($endpoints as $endpoint) { |
|
| 53 | - $this->endpoints[$endpoint->endpoint()] = $endpoint; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Magic method to access different endpoints. |
|
| 59 | - * |
|
| 60 | - * @param string $endpoint |
|
| 61 | - * |
|
| 62 | - * @return Endpoint |
|
| 63 | - * @throws WrongEndpoint |
|
| 64 | - */ |
|
| 65 | - public function __get($endpoint) |
|
| 66 | - { |
|
| 67 | - $endpoint = $this->resolveEndpoint($endpoint); |
|
| 68 | - |
|
| 69 | - if (method_exists($endpoint, 'setOrganizationId')) { |
|
| 70 | - $endpoint->setOrganizationId($this->organizationId); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $endpoint; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $organizationName |
|
| 78 | - * @return $this |
|
| 79 | - * @throws WrongOrganizationName |
|
| 80 | - */ |
|
| 81 | - public function setOrganization($organizationName) |
|
| 82 | - { |
|
| 83 | - if($organization = $this->getOrganizationByName($organizationName)) { |
|
| 84 | - $this->setOrganizationId($organization['organizationId']); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $this; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $organizationId |
|
| 92 | - * @return $this |
|
| 93 | - */ |
|
| 94 | - public function setOrganizationId($organizationId) |
|
| 95 | - { |
|
| 96 | - $this->organizationId = $organizationId; |
|
| 97 | - |
|
| 98 | - return $this; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function getOrganizationId() |
|
| 105 | - { |
|
| 106 | - return $this->organizationId; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param string $organizationName |
|
| 111 | - * @return array|bool |
|
| 112 | - * @throws WrongOrganizationName |
|
| 113 | - */ |
|
| 114 | - private function getOrganizationByName($organizationName) |
|
| 115 | - { |
|
| 116 | - $organizations = $this->organizations->getAll(); |
|
| 117 | - foreach ($organizations['entities'] as $entity) { |
|
| 118 | - if ($entity['name'] == $organizationName) { |
|
| 119 | - return $entity; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - throw new WrongOrganizationName("Organization $organizationName not found!"); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $endpoint |
|
| 128 | - * @return Endpoint |
|
| 129 | - * @throws WrongEndpoint |
|
| 130 | - */ |
|
| 131 | - private function resolveEndpoint($endpoint) |
|
| 132 | - { |
|
| 133 | - $endpoint = strtolower($endpoint); |
|
| 134 | - |
|
| 135 | - if(isset($this->endpoints[$endpoint])) { |
|
| 136 | - return $this->endpoints[$endpoint]; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - throw new WrongEndpoint("There is no endpoint called $endpoint."); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return array |
|
| 144 | - */ |
|
| 145 | - public function getRateInfo() |
|
| 146 | - { |
|
| 147 | - $responseHeaders = $this->httpClient->getResponseHeaders(); |
|
| 148 | - |
|
| 149 | - return [ |
|
| 150 | - 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
| 151 | - 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
| 152 | - 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
| 153 | - ]; |
|
| 154 | - } |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $organizationId; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var HttpClient |
|
| 41 | + */ |
|
| 42 | + private $httpClient; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var Endpoint[] |
|
| 46 | + */ |
|
| 47 | + private $endpoints; |
|
| 48 | + |
|
| 49 | + public function __construct(HttpClient $httpClient, Endpoint ...$endpoints) |
|
| 50 | + { |
|
| 51 | + $this->httpClient = $httpClient; |
|
| 52 | + foreach ($endpoints as $endpoint) { |
|
| 53 | + $this->endpoints[$endpoint->endpoint()] = $endpoint; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Magic method to access different endpoints. |
|
| 59 | + * |
|
| 60 | + * @param string $endpoint |
|
| 61 | + * |
|
| 62 | + * @return Endpoint |
|
| 63 | + * @throws WrongEndpoint |
|
| 64 | + */ |
|
| 65 | + public function __get($endpoint) |
|
| 66 | + { |
|
| 67 | + $endpoint = $this->resolveEndpoint($endpoint); |
|
| 68 | + |
|
| 69 | + if (method_exists($endpoint, 'setOrganizationId')) { |
|
| 70 | + $endpoint->setOrganizationId($this->organizationId); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $endpoint; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $organizationName |
|
| 78 | + * @return $this |
|
| 79 | + * @throws WrongOrganizationName |
|
| 80 | + */ |
|
| 81 | + public function setOrganization($organizationName) |
|
| 82 | + { |
|
| 83 | + if($organization = $this->getOrganizationByName($organizationName)) { |
|
| 84 | + $this->setOrganizationId($organization['organizationId']); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $this; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $organizationId |
|
| 92 | + * @return $this |
|
| 93 | + */ |
|
| 94 | + public function setOrganizationId($organizationId) |
|
| 95 | + { |
|
| 96 | + $this->organizationId = $organizationId; |
|
| 97 | + |
|
| 98 | + return $this; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function getOrganizationId() |
|
| 105 | + { |
|
| 106 | + return $this->organizationId; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param string $organizationName |
|
| 111 | + * @return array|bool |
|
| 112 | + * @throws WrongOrganizationName |
|
| 113 | + */ |
|
| 114 | + private function getOrganizationByName($organizationName) |
|
| 115 | + { |
|
| 116 | + $organizations = $this->organizations->getAll(); |
|
| 117 | + foreach ($organizations['entities'] as $entity) { |
|
| 118 | + if ($entity['name'] == $organizationName) { |
|
| 119 | + return $entity; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + throw new WrongOrganizationName("Organization $organizationName not found!"); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $endpoint |
|
| 128 | + * @return Endpoint |
|
| 129 | + * @throws WrongEndpoint |
|
| 130 | + */ |
|
| 131 | + private function resolveEndpoint($endpoint) |
|
| 132 | + { |
|
| 133 | + $endpoint = strtolower($endpoint); |
|
| 134 | + |
|
| 135 | + if(isset($this->endpoints[$endpoint])) { |
|
| 136 | + return $this->endpoints[$endpoint]; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + throw new WrongEndpoint("There is no endpoint called $endpoint."); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return array |
|
| 144 | + */ |
|
| 145 | + public function getRateInfo() |
|
| 146 | + { |
|
| 147 | + $responseHeaders = $this->httpClient->getResponseHeaders(); |
|
| 148 | + |
|
| 149 | + return [ |
|
| 150 | + 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
| 151 | + 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
| 152 | + 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
| 153 | + ]; |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -4,46 +4,46 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface HttpClient |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param string $uri |
|
| 9 | - * @param array $params |
|
| 10 | - * @param array $headers |
|
| 11 | - * @return array |
|
| 12 | - */ |
|
| 13 | - public function get($uri, array $params = [], array $headers = []); |
|
| 7 | + /** |
|
| 8 | + * @param string $uri |
|
| 9 | + * @param array $params |
|
| 10 | + * @param array $headers |
|
| 11 | + * @return array |
|
| 12 | + */ |
|
| 13 | + public function get($uri, array $params = [], array $headers = []); |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param string $uri |
|
| 17 | - * @param array $body |
|
| 18 | - * @param array $headers |
|
| 19 | - * @return array |
|
| 20 | - */ |
|
| 21 | - public function post($uri, array $body = [], array $headers = []); |
|
| 15 | + /** |
|
| 16 | + * @param string $uri |
|
| 17 | + * @param array $body |
|
| 18 | + * @param array $headers |
|
| 19 | + * @return array |
|
| 20 | + */ |
|
| 21 | + public function post($uri, array $body = [], array $headers = []); |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param string $uri |
|
| 25 | - * @param array $body |
|
| 26 | - * @param array $headers |
|
| 27 | - * @return mixed |
|
| 28 | - */ |
|
| 29 | - public function put($uri, array $body = [], array $headers = []); |
|
| 23 | + /** |
|
| 24 | + * @param string $uri |
|
| 25 | + * @param array $body |
|
| 26 | + * @param array $headers |
|
| 27 | + * @return mixed |
|
| 28 | + */ |
|
| 29 | + public function put($uri, array $body = [], array $headers = []); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param string $uri |
|
| 33 | - * @param array $body |
|
| 34 | - * @param array $headers |
|
| 35 | - * @return mixed |
|
| 36 | - */ |
|
| 37 | - public function delete($uri, array $body = [], array $headers = []); |
|
| 31 | + /** |
|
| 32 | + * @param string $uri |
|
| 33 | + * @param array $body |
|
| 34 | + * @param array $headers |
|
| 35 | + * @return mixed |
|
| 36 | + */ |
|
| 37 | + public function delete($uri, array $body = [], array $headers = []); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $url |
|
| 41 | - * @return $this |
|
| 42 | - */ |
|
| 43 | - public function setBaseUrl($url); |
|
| 39 | + /** |
|
| 40 | + * @param string $url |
|
| 41 | + * @return $this |
|
| 42 | + */ |
|
| 43 | + public function setBaseUrl($url); |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function getResponseHeaders(); |
|
| 45 | + /** |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function getResponseHeaders(); |
|
| 49 | 49 | } |