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

Commit

Permalink
Merge pull request #38 from klaviyo/202011_fix_rate_limit
Browse files Browse the repository at this point in the history
Catch rate limit properly
  • Loading branch information
remstone7 authored Nov 23, 2020
2 parents 3da6d90 + 0992033 commit 097f9ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 2.2.4
- 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

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,6 @@ $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.
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}`
2 changes: 1 addition & 1 deletion src/Klaviyo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Klaviyo
/**
* @var string
*/
const VERSION = '2.2.3';
const VERSION = '2.2.4';

/**
* Constructor for Klaviyo.
Expand Down
23 changes: 22 additions & 1 deletion src/KlaviyoAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( $this->decodeJsonResponse( $response ) );
throw new KlaviyoRateLimitException(
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
);
} else if ( $statusCode != 200 ) {
throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) );
}
Expand Down Expand Up @@ -329,6 +331,25 @@ private function decodeJsonResponse( $response )
return json_decode( '{}', true );
}

/**
* 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
* @return string
*/
private function returnRateLimit ( $response )
{
$responseDetail = explode(" ", $response['detail'] );
foreach ($responseDetail as $value) {
if (intval($value) > 0) {
$response['retryAfter'] = intval($value);
}
}
return json_encode($response);
}


/**
* Return formatted options.
*
Expand Down

0 comments on commit 097f9ef

Please sign in to comment.