|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PowerLink; |
| 4 | + |
| 5 | +abstract class KlaviyoAPI |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Host |
| 9 | + */ |
| 10 | + const BASE_URL = 'https://api.powerlink.co.il/api/'; |
| 11 | + |
| 12 | + /** |
| 13 | + * Request methods |
| 14 | + */ |
| 15 | + const HTTP_GET = 'GET'; |
| 16 | + const HTTP_POST = 'POST'; |
| 17 | + const HTTP_PUT = 'PUT'; |
| 18 | + const HTTP_DELETE = 'DELETE'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Error messages |
| 22 | + */ |
| 23 | + const ERROR_INVALID_API_KEY = 'Invalid TokenID.'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Request options |
| 27 | + */ |
| 28 | + const DATA = 'data'; |
| 29 | + const HEADERS = 'headers'; |
| 30 | + const JSON = 'json'; |
| 31 | + const PROPERTIES = 'properties'; |
| 32 | + const QUERY = 'query'; |
| 33 | + const TOKEN = 'token'; |
| 34 | + const USER_AGENT = 'User-Agent'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $token_id; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $client; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor method for Base class. |
| 48 | + * |
| 49 | + * @param string $token_id Token id for PowerLink account |
| 50 | + */ |
| 51 | + public function __construct($token_id) |
| 52 | + { |
| 53 | + $this->token_id = $token_id; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * Make API request using HTTP client |
| 59 | + * |
| 60 | + * @param string $path Endpoint to call |
| 61 | + * @param array $options API params to add to request |
| 62 | + * @param string $method HTTP method for request |
| 63 | + * @param bool $isPublic to determine if public request |
| 64 | + * @param bool $isV1 to determine if V1 API request |
| 65 | + * |
| 66 | + * @throws KlaviyoException |
| 67 | + */ |
| 68 | + private function request($method, $path, $options) |
| 69 | + { |
| 70 | + $options = $this->prepareAuthentication(); |
| 71 | + |
| 72 | + $setopt_array = ($this->getDefaultCurlOptions($method) + |
| 73 | + $this->getCurlOptUrl($path, $options) + |
| 74 | + $this->getSpecificCurlOptions($options)); |
| 75 | + |
| 76 | + $curl = curl_init(); |
| 77 | + curl_setopt_array($curl, $setopt_array); |
| 78 | + |
| 79 | + $response = curl_exec($curl); |
| 80 | + $phpVersionHttpCode = version_compare(phpversion(), '5.5.0', '>') ? CURLINFO_RESPONSE_CODE : CURLINFO_HTTP_CODE; |
| 81 | + $statusCode = curl_getinfo($curl, $phpVersionHttpCode); |
| 82 | + curl_close($curl); |
| 83 | + |
| 84 | + return $this->handleResponse($response, $statusCode, $isPublic); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Handle response from API call |
| 90 | + */ |
| 91 | + private function handleResponse($response, $statusCode, $isPublic) |
| 92 | + { |
| 93 | + if ($statusCode == 403) { |
| 94 | + throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode); |
| 95 | + } else if ($statusCode == 404) { |
| 96 | + throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST, $statusCode); |
| 97 | + } else if ($statusCode == 429) { |
| 98 | + throw new KlaviyoRateLimitException( |
| 99 | + $this->returnRateLimit($this->decodeJsonResponse($response), $statusCode) |
| 100 | + ); |
| 101 | + } else if ($statusCode != 200) { |
| 102 | + throw new KlaviyoApiException($this->decodeJsonResponse($response)['detail'], $statusCode); |
| 103 | + } |
| 104 | + |
| 105 | + if ($isPublic) { |
| 106 | + return $response; |
| 107 | + } |
| 108 | + |
| 109 | + return $this->decodeJsonResponse($response); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Handle authentication by updating $options passed into request method |
| 114 | + * based on type of API request. |
| 115 | + * |
| 116 | + * @param array $params Options configuration for Request Interface |
| 117 | + * @param bool $isPublic Request type - public |
| 118 | + * @param bool $isV1 Request API version - V1 |
| 119 | + * |
| 120 | + * @return array|array[] |
| 121 | + */ |
| 122 | + private function prepareAuthentication($params, $isPublic, $isV1) |
| 123 | + { |
| 124 | + if ($isPublic) { |
| 125 | + $params = $this->publicAuth($params); |
| 126 | + return $params; |
| 127 | + } |
| 128 | + |
| 129 | + if ($isV1) { |
| 130 | + $params = $this->v1Auth($params); |
| 131 | + return $params; |
| 132 | + } else { |
| 133 | + $params = $this->v2Auth($params); |
| 134 | + return $params; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments