From de35d6e1d85c35bd84169b87ed33f4ad193f00f7 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Mon, 23 Nov 2020 08:41:43 -0500 Subject: [PATCH 1/4] Catch rate limit properly --- CHANGELOG.md | 3 +++ README.md | 4 ++-- src/Klaviyo.php | 2 +- src/KlaviyoAPI.php | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2984383..ea0fcbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## CHANGELOG +### 2.2.4 +- Fix - Catch KlaviyoRateLimitException properly + ### 2.2.3 - Fix - ProfileModel file to convert specialAttributes properties to camel case before executing property_exists method inside jsonSerialize diff --git a/README.md b/README.md index f7e614c..a6c21e9 100644 --- a/README.md +++ b/README.md @@ -152,5 +152,5 @@ $client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' ); ``` ## Rate Limiting - If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException - This will contain a detail key with a string value mentioning the time to back off in seconds + If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException. + You will use getMessage() to get the detail key with a string value mentioning the time to back off in seconds. diff --git a/src/Klaviyo.php b/src/Klaviyo.php index ee57b6c..67322f4 100644 --- a/src/Klaviyo.php +++ b/src/Klaviyo.php @@ -21,7 +21,7 @@ class Klaviyo /** * @var string */ - const VERSION = '2.2.3'; + const VERSION = '2.2.4'; /** * Constructor for Klaviyo. diff --git a/src/KlaviyoAPI.php b/src/KlaviyoAPI.php index 4841913..3500617 100644 --- a/src/KlaviyoAPI.php +++ b/src/KlaviyoAPI.php @@ -179,7 +179,7 @@ private function handleResponse( $response, $statusCode, $isPublic ) } else if ( $statusCode == 404 ) { throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST); } else if ( $statusCode == 429 ) { - throw new KlaviyoRateLimitException( $this->decodeJsonResponse( $response ) ); + throw new KlaviyoRateLimitException( $response ); } else if ( $statusCode != 200 ) { throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) ); } From ef3dc051417ded7c61fe48920f17e3a08fbe99b4 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Mon, 23 Nov 2020 09:52:09 -0500 Subject: [PATCH 2/4] return more helpful rate limiting values --- CHANGELOG.md | 3 ++- README.md | 3 ++- src/KlaviyoAPI.php | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea0fcbe..0d40a48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ ## CHANGELOG ### 2.2.4 -- Fix - Catch KlaviyoRateLimitException properly +- Fix - Instantiate KlaviyoRateLimitException properly +- Update - Add retryAfter as an array key for the KlaviyoRateLimitException message ### 2.2.3 - Fix - ProfileModel file to convert specialAttributes properties to camel case before executing property_exists method inside jsonSerialize diff --git a/README.md b/README.md index a6c21e9..cdf4fd6 100644 --- a/README.md +++ b/README.md @@ -153,4 +153,5 @@ $client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' ); ## Rate Limiting If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException. - You will use getMessage() to get the detail key with a string value mentioning the time to back off in seconds. + After you catch this exception you can use getMessage() and it will return an array: + `{"detail":"Request was throttled. Expected available in 26.0 seconds.","retryAfter":26}` diff --git a/src/KlaviyoAPI.php b/src/KlaviyoAPI.php index 3500617..5e023ef 100644 --- a/src/KlaviyoAPI.php +++ b/src/KlaviyoAPI.php @@ -179,7 +179,9 @@ private function handleResponse( $response, $statusCode, $isPublic ) } else if ( $statusCode == 404 ) { throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST); } else if ( $statusCode == 429 ) { - throw new KlaviyoRateLimitException( $response ); + throw new KlaviyoRateLimitException( + $this->returnRateLimit( $this->decodeJsonResponse( $response ) ) + ); } else if ( $statusCode != 200 ) { throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) ); } @@ -329,6 +331,25 @@ private function decodeJsonResponse( $response ) return json_decode( '{}', true ); } + /** + * Return jsone encoded rate limit array with details and the retryAfter value parsed. + * We build an easier object that tells you how long to retry after. + * + * @param mixed $response + * @return string + */ + private function returnRateLimit ( $response ) + { + $arr = explode(" ", $response['detail'] ); + foreach ($arr as $value) { + if (intval($value) > 0) { + $response['retryAfter'] = intval($value); + } + } + return json_encode($response); + } + + /** * Return formatted options. * From 5875f8b8fc9dff24aa09b378e532e98ab62f60aa Mon Sep 17 00:00:00 2001 From: remstone7 Date: Mon, 23 Nov 2020 10:08:11 -0500 Subject: [PATCH 3/4] pr feedback, cleanup --- src/KlaviyoAPI.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/KlaviyoAPI.php b/src/KlaviyoAPI.php index 5e023ef..c3a8a71 100644 --- a/src/KlaviyoAPI.php +++ b/src/KlaviyoAPI.php @@ -332,7 +332,7 @@ private function decodeJsonResponse( $response ) } /** - * Return jsone encoded rate limit array with details and the retryAfter value parsed. + * Return json encoded rate limit array with details and the retryAfter value parsed. * We build an easier object that tells you how long to retry after. * * @param mixed $response @@ -340,8 +340,8 @@ private function decodeJsonResponse( $response ) */ private function returnRateLimit ( $response ) { - $arr = explode(" ", $response['detail'] ); - foreach ($arr as $value) { + $responseDetail = explode(" ", $response['detail'] ); + foreach ($responseDetail as $value) { if (intval($value) > 0) { $response['retryAfter'] = intval($value); } From 0992033f88c1e57eacd321a71020a32902d86345 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Mon, 23 Nov 2020 10:18:16 -0500 Subject: [PATCH 4/4] readme updates --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cdf4fd6..565df5a 100644 --- a/README.md +++ b/README.md @@ -153,5 +153,5 @@ $client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' ); ## Rate Limiting If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException. - After you catch this exception you can use getMessage() and it will return an array: + After you catch this exception you can use getMessage() and it will return a JSON encoded array: `{"detail":"Request was throttled. Expected available in 26.0 seconds.","retryAfter":26}`