cloudflare/Cloudflare-WordPress
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress
src/API/AbstractAPIClient.php
92lines · modecode
| 1 | <?php |
| 2 | |
| 3 | namespace CF\API; |
| 4 | |
| 5 | use CF\Integration\IntegrationInterface; |
| 6 | use GuzzleHttp; |
| 7 | use GuzzleHttp\Exception\RequestException; |
| 8 | |
| 9 | abstract class AbstractAPIClient implements APIInterface |
| 10 | { |
| 11 | const CONTENT_TYPE_KEY = 'Content-Type'; |
| 12 | const APPLICATION_JSON_KEY = 'application/json'; |
| 13 | |
| 14 | protected $config; |
| 15 | protected $data_store; |
| 16 | protected $logger; |
| 17 | protected $integrationAPI; |
| 18 | |
| 19 | /** |
| 20 | * @param IntegrationInterface $integration |
| 21 | */ |
| 22 | public function __construct(IntegrationInterface $integration) |
| 23 | { |
| 24 | $this->config = $integration->getConfig(); |
| 25 | $this->data_store = $integration->getDataStore(); |
| 26 | $this->logger = $integration->getLogger(); |
| 27 | $this->integrationAPI = $integration->getIntegrationAPI(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param Request $request |
| 32 | * |
| 33 | * @return array|mixed |
| 34 | */ |
| 35 | public function callAPI(Request $request) |
| 36 | { |
| 37 | try { |
| 38 | $client = new GuzzleHttp\Client(['base_url' => $this->getEndpoint()]); |
| 39 | |
| 40 | $request = $this->beforeSend($request); |
| 41 | |
| 42 | $bodyType = (($request->getHeaders()[self::CONTENT_TYPE_KEY] === self::APPLICATION_JSON_KEY) ? 'json' : 'body'); |
| 43 | |
| 44 | $requestOptions = array( |
| 45 | 'headers' => $request->getHeaders(), |
| 46 | 'query' => $request->getParameters(), |
| 47 | $bodyType => $request->getBody(), |
| 48 | ); |
| 49 | |
| 50 | if ($this->config->getValue('debug')) { |
| 51 | $requestOptions['debug'] = fopen('php://stderr', 'w'); |
| 52 | } |
| 53 | |
| 54 | $apiRequest = $client->createRequest($request->getMethod(), $request->getUrl(), $requestOptions); |
| 55 | |
| 56 | $response = $client->send($apiRequest)->json(); |
| 57 | |
| 58 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 59 | throw new RequestException('Error decoding client API JSON', $response); |
| 60 | } |
| 61 | |
| 62 | if (!$this->responseOk($response)) { |
| 63 | $this->logger->logAPICall($this->getAPIClientName(), array('type' => 'response', 'body' => $response), false); |
| 64 | } |
| 65 | |
| 66 | return $response; |
| 67 | } catch (RequestException $e) { |
| 68 | $this->logger->logAPICall($this->getAPIClientName(), array( |
| 69 | 'type' => 'request', |
| 70 | 'method' => $request->getMethod(), |
| 71 | 'path' => $request->getUrl(), |
| 72 | 'headers' => $request->getHeaders(), |
| 73 | 'params' => $request->getParameters(), |
| 74 | 'body' => $request->getBody(), ), false); |
| 75 | $this->logger->logAPICall($this->getAPIClientName(), array('type' => 'response', 'code' => $e->getCode(), 'body' => $e->getMessage(), 'stacktrace' => $e->getTraceAsString()), false); |
| 76 | |
| 77 | return $this->createAPIError($e->getMessage()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param Request $request |
| 83 | * |
| 84 | * @return mixed |
| 85 | */ |
| 86 | abstract public function beforeSend(Request $request); |
| 87 | |
| 88 | /** |
| 89 | * @return mixed |
| 90 | */ |
| 91 | abstract public function getAPIClientName(); |
| 92 | } |