helpscout / helpscout-api-php

PHP Wrapper for the Help Scout API
MIT License
99 stars 62 forks source link

Rate Limit #295

Closed mlboudreau closed 2 years ago

mlboudreau commented 2 years ago

Thank you for taking the time to submit an issue with all the details shown below. Our engineering team monitors issues submitted and strives to respond with 1-2 business days.

How can I check if the rate limit is about to be reached?

miguelrs commented 2 years ago

Hey @mlboudreau!

The Mailbox API includes some headers in the responses related to rate limits, as explained in this doc. Specifically, the X-RateLimit-Remaining-Minute header gives you the number of requests remaining. Unfortunately, this SDK does not expose those headers for you to read them in an easy way. This is a feature we haven't been asked for before, but it'd be definitely an interesting addition, and we will consider it for our roadmap 👍

In the meanwhile, note that you can access the Response (and its headers) from the Exceptions thrown, so you could try something like this:

try {
    $invalidConvoId = -1;
    $response = $client->conversations()->get($invalidConvoId, new ConversationRequest());
} catch (ClientException $exception) {
    $remaining = $exception->getResponse()->getHeader('X-RateLimit-Remaining-Minute')[0];
    echo 'There are ' . $remaining . ' requests remaining...';
}

Obviously this is not ideal though. Probably the correct approach would be to create a new Middleware that can explore the Response (similar to the RateLimitHandler for example). Then you'd need to extend our RestClientBuilder to be able to register your Middleware. Finally, you'd need to create your own ApiClientFactory to use the RestClientBuilder you created instead of the default.

Hope this can help you move forward for now. Please feel free to ask any other questions you have!