Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Commit 097f9ef

Browse files
authored
Merge pull request #38 from klaviyo/202011_fix_rate_limit
Catch rate limit properly
2 parents 3da6d90 + 0992033 commit 097f9ef

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## CHANGELOG
22

3+
### 2.2.4
4+
- Fix - Instantiate KlaviyoRateLimitException properly
5+
- Update - Add retryAfter as an array key for the KlaviyoRateLimitException message
6+
37
### 2.2.3
48
- Fix - ProfileModel file to convert specialAttributes properties to camel case before executing property_exists method inside jsonSerialize
59

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,6 @@ $client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' );
152152
```
153153

154154
## Rate Limiting
155-
If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException
156-
This will contain a detail key with a string value mentioning the time to back off in seconds
155+
If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException.
156+
After you catch this exception you can use getMessage() and it will return a JSON encoded array:
157+
`{"detail":"Request was throttled. Expected available in 26.0 seconds.","retryAfter":26}`

src/Klaviyo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Klaviyo
2121
/**
2222
* @var string
2323
*/
24-
const VERSION = '2.2.3';
24+
const VERSION = '2.2.4';
2525

2626
/**
2727
* Constructor for Klaviyo.

src/KlaviyoAPI.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ private function handleResponse( $response, $statusCode, $isPublic )
179179
} else if ( $statusCode == 404 ) {
180180
throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST);
181181
} else if ( $statusCode == 429 ) {
182-
throw new KlaviyoRateLimitException( $this->decodeJsonResponse( $response ) );
182+
throw new KlaviyoRateLimitException(
183+
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
184+
);
183185
} else if ( $statusCode != 200 ) {
184186
throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) );
185187
}
@@ -329,6 +331,25 @@ private function decodeJsonResponse( $response )
329331
return json_decode( '{}', true );
330332
}
331333

334+
/**
335+
* Return json encoded rate limit array with details and the retryAfter value parsed.
336+
* We build an easier object that tells you how long to retry after.
337+
*
338+
* @param mixed $response
339+
* @return string
340+
*/
341+
private function returnRateLimit ( $response )
342+
{
343+
$responseDetail = explode(" ", $response['detail'] );
344+
foreach ($responseDetail as $value) {
345+
if (intval($value) > 0) {
346+
$response['retryAfter'] = intval($value);
347+
}
348+
}
349+
return json_encode($response);
350+
}
351+
352+
332353
/**
333354
* Return formatted options.
334355
*

0 commit comments

Comments
 (0)