From db7973d49de3c2ffca03e38387450e15885f9846 Mon Sep 17 00:00:00 2001 From: Jordan Leslie Date: Tue, 19 Sep 2023 14:52:42 -0500 Subject: [PATCH] Move V3 logic to api wrapper --- Helper/Data.php | 46 +++---------- .../Exception/KlaviyoApiException.php | 2 +- .../KlaviyoAuthenticationException.php | 2 +- KlaviyoV3Sdk/Exception/KlaviyoException.php | 4 +- .../Exception/KlaviyoRateLimitException.php | 2 +- .../KlaviyoResourceNotFoundException.php | 2 +- KlaviyoV3Sdk/KlaviyoV3Api.php | 67 +++++++++---------- 7 files changed, 48 insertions(+), 77 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index fe4c303..2b2b19f 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -59,44 +59,16 @@ public function unsetObserverAtcPayload() public function getKlaviyoLists($api_key = null) { - if (!$api_key) { - $api_key = $this->_klaviyoScopeSetting->getPrivateApiKey(); - } - $v3_list_api = self::KLAVIYO_HOST . self::LIST_V3_API . '?api_key=' . $api_key; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $v3_list_api); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - - - $output = json_decode(curl_exec($ch)); - $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); - curl_close($ch); - - if ($statusCode !== 200) { - if ($statusCode === 403) { - $reason = self::ERROR_INVALID_API_KEY; - } elseif ($statusCode === 401) { - $reason = self::ERROR_INVALID_API_KEY; - } else { - $reason = self::ERROR_NON_200_STATUS; - } - - $result = [ - 'success' => false, - 'reason' => $reason - ]; - } else { - usort($output, function ($a, $b) { - return strtolower($a->list_name) > strtolower($b->list_name) ? 1 : -1; - }); - - $result = [ - 'success' => true, - 'lists' => $output - ]; - } + $response = $this->getLists(); + + usort($response, function ($a, $b) { + return strtolower($a->list_name) > strtolower($b->list_name) ? 1 : -1; + }); - return $result; + return [ + 'success' => true, + 'lists' => $response + ]; } /** diff --git a/KlaviyoV3Sdk/Exception/KlaviyoApiException.php b/KlaviyoV3Sdk/Exception/KlaviyoApiException.php index 46b6f86..4a14ece 100644 --- a/KlaviyoV3Sdk/Exception/KlaviyoApiException.php +++ b/KlaviyoV3Sdk/Exception/KlaviyoApiException.php @@ -1,6 +1,6 @@ _klaviyoScopeSetting->getVersion(); + + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface'); + $m2Version = $productMetadata->getVersion(); $headers = array( CURLOPT_HTTPHEADER => [ self::REVISION_KEY_HEADER . ': ' . self::KLAVIYO_V3_REVISION, self::ACCEPT_KEY_HEADER . ': ' . self::APPLICATION_JSON_HEADER_VALUE, - self::KLAVIYO_USER_AGENT_KEY . ': ' . 'prestashop-klaviyo/' . $klaviyops->version . 'Prestashop/' . _PS_VERSION_ . 'PHP/' . phpversion() + self::KLAVIYO_USER_AGENT_KEY . ': ' . 'prestashop-klaviyo/' . $klVersion() . 'Magento2/' . $m2Version . 'PHP/' . phpversion() ] ); @@ -114,9 +121,9 @@ public function getHeaders($clientEvent) * * @return array */ - public function getLists($api_key) + public function getKlaviyoLists() { - + $this->sendApiRequest(self::LIST_V3_API, false, 'GET'); } /** @@ -137,7 +144,7 @@ public function track($config): array ) ); - return $this->make_request('/client/events/?company_id=' . $this->public_key, true, $body); + return $this->sendApiRequest(self::EVENT_V3_API . '?company_id=' . $this->public_key, true, $body); } /** @@ -183,7 +190,7 @@ public function unsubscribeEmailFromKlaviyoList($email): array|string|null 'emails' => [(string)$email], ]; - return $this->sendApiRequest($path, $fields, 'POST'); + return $this->sendApiRequest($path, false, 'POST', $fields); } /** @@ -206,7 +213,7 @@ protected function sendApiRequest($path, $clientEvent, $method = null, $body = n $curl = curl_init(); $options = array( CURLOPT_URL => self::KLAVIYO_HOST . $path, - ) + $this->getHeaders($clientEvent) + $this->getDefaultCurlOptions(); + ) + $this->getHeaders($clientEvent) + $this->getDefaultCurlOptions($method); if ($body !== null) { $options[CURLOPT_POSTFIELDS][] = $body; @@ -289,7 +296,7 @@ protected function getDefaultCurlOptions($method = null) CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, - CURLOPT_CUSTOMREQUEST => (!empty($method)) ? $method : 'POST',, + CURLOPT_CUSTOMREQUEST => (!empty($method)) ? $method : 'POST', CURLOPT_USERAGENT => self::USER_AGENT, ); } @@ -306,29 +313,21 @@ protected function getDefaultCurlOptions($method = null) * @throws KlaviyoRateLimitException */ protected - function handleAPIResponse($response, $statusCode, $clientEvent = false) + function handleAPIResponse($response, $statusCode, $clientEvent = false): array|string|null { - $decoded_response = $this->decodeJsonResponse($response); - if ($statusCode == 403) { - throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode); - } - - if ($statusCode == 401) { - throw new KlaviyoAuthenticationException(self::ERROR_EXPIRED_API_KEY, $statusCode); - } - - if ($statusCode === 429) { - throw new KlaviyoAuthenticationException(self::ERROR_UNVERIFIABLE_API_KEY, $statusCode); - } - - if ($statusCode == 429) { - throw new KlaviyoRateLimitException( - $this->returnRateLimit($decoded_response) - ); - } - - if ($statusCode < 200 || $statusCode >= 300) { - throw new KlaviyoApiException(isset($decoded_response['detail']) ? $decoded_response['detail'] : sprintf(self::ERROR_NON_200_STATUS, $statusCode), $statusCode); + try { + $decoded_response = $this->decodeJsonResponse($response); + } catch (\Exception $error) { + switch ($statusCode) { + case 403: + case 401: + throw new KlaviyoAuthenticationException($error['detail'], $statusCode); + case 429: + throw new KlaviyoRateLimitException($error['detail'], $statusCode); + default: + $errorMessage = isset($decoded_response['detail']) ? $decoded_response['detail'] : sprintf(self::ERROR_NON_200_STATUS, $statusCode); + throw new KlaviyoApiException($errorMessage, $statusCode); + } } if ($clientEvent) {