alexdodonov /
mezon-custom-client
| 1 | <?php |
||
| 2 | namespace Mezon\CustomClient; |
||
| 3 | |||
| 4 | /** |
||
| 5 | * Class CustomClient |
||
| 6 | * |
||
| 7 | * @package Mezon |
||
| 8 | * @subpackage CustomClient |
||
| 9 | * @author Dodonov A.A. |
||
| 10 | * @version v.1.0 (2019/08/07) |
||
| 11 | * @copyright Copyright (c) 2019, aeon.org |
||
| 12 | */ |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Custom API client class |
||
| 16 | */ |
||
| 17 | class CustomClient |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Server host |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $url = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Headers |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $headers = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Idempotence key |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $idempotencyKey = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @param string $url |
||
| 45 | * Service URL |
||
| 46 | * @param array $headers |
||
| 47 | * HTTP headers |
||
| 48 | */ |
||
| 49 | public function __construct(string $url, array $headers = []) |
||
| 50 | { |
||
| 51 | if ($url === false || $url === '') { |
||
| 52 | throw (new \Exception( |
||
| 53 | 'Service URL must be set in class ' . __CLASS__ . ' extended in ' . get_called_class() . |
||
| 54 | ' and called from ' . ($_SERVER['SERVER_NAME'] ?? 'console') . ($_SERVER['REQUEST_URI'] ?? ''), |
||
| 55 | - 23)); |
||
| 56 | } |
||
| 57 | |||
| 58 | $this->url = rtrim($url, '/'); |
||
| 59 | |||
| 60 | $this->headers = $headers; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Method send request to the URL |
||
| 65 | * |
||
| 66 | * @param string $url |
||
| 67 | * URL |
||
| 68 | * @param array $headers |
||
| 69 | * Headers |
||
| 70 | * @param string $method |
||
| 71 | * Request HTTP Method |
||
| 72 | * @param array $data |
||
| 73 | * Request data |
||
| 74 | * @return array Response body and HTTP code |
||
| 75 | * @codeCoverageIgnore |
||
| 76 | */ |
||
| 77 | protected function sendRequest(string $url, array $headers, string $method, array $data = []): array |
||
| 78 | { |
||
| 79 | return \Mezon\CustomClient\CurlWrapper::sendRequest($url, $headers, $method, $data); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Method gets result and validates it. |
||
| 84 | * |
||
| 85 | * @param string $url |
||
| 86 | * Request URL |
||
| 87 | * @param int $code |
||
| 88 | * Response HTTP code |
||
| 89 | * @return mixed Request result |
||
| 90 | */ |
||
| 91 | protected function dispatchResult(string $url, int $code): void |
||
| 92 | { |
||
| 93 | if ($code == 404) { |
||
| 94 | throw (new \Exception("URL: $url not found")); |
||
| 95 | } elseif ($code == 400) { |
||
| 96 | throw (new \Exception("Bad request on URL $url")); |
||
| 97 | } elseif ($code == 403) { |
||
| 98 | throw (new \Exception("Auth error")); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Method returns common headers |
||
| 104 | * |
||
| 105 | * @return array Headers |
||
| 106 | */ |
||
| 107 | protected function getCommonHeaders(): array |
||
| 108 | { |
||
| 109 | $result = []; |
||
| 110 | |||
| 111 | if ($this->headers !== false) { |
||
| 112 | $result = $this->headers; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($this->idempotencyKey !== '') { |
||
| 116 | $result[] = 'Idempotency-Key: ' . $this->idempotencyKey; |
||
| 117 | } |
||
| 118 | |||
| 119 | $result[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'; |
||
| 120 | |||
| 121 | return $result; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Method compiles post headers |
||
| 126 | * |
||
| 127 | * @return array Header |
||
| 128 | */ |
||
| 129 | protected function getFormHeaders(): array |
||
| 130 | { |
||
| 131 | $fullHeaders = $this->getCommonHeaders(); |
||
| 132 | |||
| 133 | $fullHeaders[] = 'Content-type: application/x-www-form-urlencoded'; |
||
| 134 | |||
| 135 | return $fullHeaders; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Method sends request on server |
||
| 140 | * |
||
| 141 | * @param string $method |
||
| 142 | * HTTP method (POST|PUT|DELETE) |
||
| 143 | * @param string $endpoint |
||
| 144 | * Calling endpoint |
||
| 145 | * @param array $data |
||
| 146 | * Request data |
||
| 147 | */ |
||
| 148 | protected function sendFormRequest(string $method, string $endpoint, array $data = []): string |
||
| 149 | { |
||
| 150 | $fullURL = $this->url . '/' . ltrim($endpoint, '/'); |
||
| 151 | |||
| 152 | list ($body, $code) = $this->sendRequest($fullURL, $this->getFormHeaders(), $method, $data); |
||
| 153 | |||
| 154 | $this->dispatchResult($fullURL, $code); |
||
| 155 | |||
| 156 | return $body; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Method sends POST request to server |
||
| 161 | * |
||
| 162 | * @param string $endpoint |
||
| 163 | * Calling endpoint |
||
| 164 | * @param array $data |
||
| 165 | * Request data |
||
| 166 | * @return mixed Result of the request |
||
| 167 | */ |
||
| 168 | public function sendPostRequest(string $endpoint, array $data = []): string |
||
| 169 | { |
||
| 170 | return $this->sendFormRequest('POST', $endpoint, $data); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Method sends PUT request to server |
||
| 175 | * |
||
| 176 | * @param string $endpoint |
||
| 177 | * Calling endpoint |
||
| 178 | * @param array $data |
||
| 179 | * Request data |
||
| 180 | * @return mixed Result of the request |
||
| 181 | */ |
||
| 182 | public function sendPutRequest(string $endpoint, array $data = []): string |
||
| 183 | { |
||
| 184 | return $this->sendFormRequest('PUT', $endpoint, $data); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Method sends DELETE request to server |
||
| 189 | * |
||
| 190 | * @param string $endpoint |
||
| 191 | * Calling endpoint |
||
| 192 | * @param array $data |
||
| 193 | * Request data |
||
| 194 | * @return mixed Result of the request |
||
| 195 | */ |
||
| 196 | public function sendDeleteRequest(string $endpoint, array $data = []): string |
||
| 197 | { |
||
| 198 | return $this->sendFormRequest('DELETE', $endpoint, $data); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Method sends GET request to server. |
||
| 203 | * |
||
| 204 | * @param string $endpoint |
||
| 205 | * Calling endpoint. |
||
| 206 | * @return mixed Result of the remote call. |
||
| 207 | */ |
||
| 208 | public function sendGetRequest(string $endpoint): string |
||
| 209 | { |
||
| 210 | $fullURL = $this->url . '/' . ltrim($endpoint, '/'); |
||
| 211 | |||
| 212 | $fullURL = str_replace(' ', '%20', $fullURL); |
||
| 213 | |||
| 214 | list ($body, $code) = $this->sendRequest($fullURL, $this->getCommonHeaders(), 'GET'); |
||
| 215 | |||
| 216 | $this->dispatchResult($fullURL, $code); |
||
| 217 | |||
| 218 | return $body; |
||
|
0 ignored issues
–
show
|
|||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Method sets idempotence key. |
||
| 223 | * To remove the key just call this method the second time with the '' parameter |
||
| 224 | * |
||
| 225 | * @param string $key |
||
| 226 | * Idempotence key |
||
| 227 | */ |
||
| 228 | public function setIdempotencyKey(string $key): void |
||
| 229 | { |
||
| 230 | $this->idempotencyKey = $key; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Method returns idempotency key |
||
| 235 | * |
||
| 236 | * @return string Idempotency key |
||
| 237 | */ |
||
| 238 | public function getIdempotencyKey(): string |
||
| 239 | { |
||
| 240 | return $this->idempotencyKey; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Method returns URL |
||
| 245 | * |
||
| 246 | * @return string URL |
||
| 247 | */ |
||
| 248 | public function getUrl(): string |
||
| 249 | { |
||
| 250 | return $this->url; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Method returns headers |
||
| 255 | * |
||
| 256 | * @return array Headers |
||
| 257 | */ |
||
| 258 | public function getHeaders(): array |
||
| 259 | { |
||
| 260 | return $this->headers; |
||
| 261 | } |
||
| 262 | } |
||
| 263 |