cloudflare/Cloudflare-WordPress
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress
src/API/Host.php
84lines · modecode
10 years ago
| 1 | <?php |
| 2 | namespace CF\API; |
| 3 | |
| 4 | use GuzzleHttp; |
| 5 | |
| 6 | class Host extends AbstractAPIClient |
| 7 | { |
| 8 | |
| 9 | const CF_INTEGRATION_HEADER = "CF-Integration"; |
| 10 | const CF_INTEGRTATION_VERSION_HEADER = "CF-Integration-Version"; |
| 11 | const HOST_API_NAME = "HOST API"; |
| 12 | //self::ENDPOINT_BASE_URL . self::ENDPOINT_PATH isn't a thing so you have to update it twice if it changes. |
| 13 | const ENDPOINT_BASE_URL = "https://api.cloudflare.com/"; |
| 14 | const ENDPOINT_PATH = "host-gw.html"; |
| 15 | const ENDPOINT = "https://api.cloudflare.com/host-gw.html"; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * @param Request $request |
| 20 | * @return Request |
| 21 | */ |
| 22 | public function beforeSend(Request $request) |
| 23 | { |
| 24 | //Host API isn't restful so path must always self::ENDPOINT_PATH |
| 25 | $request->setUrl(self::ENDPOINT_PATH); |
| 26 | |
| 27 | $headers = array( |
| 28 | self::CF_INTEGRATION_HEADER => $this->config->getValue("integrationName"), |
| 29 | self::CF_INTEGRTATION_VERSION_HEADER => $this->config->getValue("version"), |
| 30 | ); |
| 31 | $request->setHeaders($headers); |
| 32 | |
| 33 | $body = $request->getBody(); |
| 34 | $user_key_actions = array('zone_set', 'full_zone_set'); |
| 35 | if (in_array(strtolower($body['act']), $user_key_actions)) { |
| 36 | $body["user_key"] = $this->data_store->getHostAPIUserKey(); |
| 37 | } |
| 38 | $body['host_key'] = $this->integrationAPI->getHostAPIKey(); |
| 39 | $request->setBody($body); |
| 40 | |
| 41 | return $request; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param $host_api_response |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function responseOk($host_api_response) |
| 49 | { |
| 50 | return ($host_api_response['result'] === 'success'); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return string |
| 55 | */ |
| 56 | public function getEndpoint() |
| 57 | { |
| 58 | return self::ENDPOINT; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return string |
| 63 | */ |
| 64 | public function getAPIClientName() |
| 65 | { |
| 66 | return self::HOST_API_NAME; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param $message |
| 71 | * @return array |
| 72 | */ |
| 73 | public function createAPIError($message) |
| 74 | { |
| 75 | return array( |
| 76 | 'request' => array( |
| 77 | 'act' => '' |
| 78 | ), |
| 79 | 'result' => 'error', |
| 80 | 'msg' => $message, |
| 81 | 'err_code' => '' |
| 82 | ); |
| 83 | } |
| 84 | } |