Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose response and API usage headers #275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BroHandTW
Copy link

@BroHandTW BroHandTW commented May 17, 2022

according to rate limit doc https://developers.facebook.com/docs/graph-api/overview/rate-limiting, we can get app usage or Business Use Case(buc) usage from API request.

so i add two features here -

  1. make deliver method can get request headers
response = Facebook::Messenger::Bot.deliver(message_obj, ... , return_response: true)
response.body 
# => "{\"recipient_id\":\"...\",\"message_id\":\"...\"}"
response.headers['x-business-use-case-usage']
# => "{\"YOUR_PAGE_ID\":[{\"type\":\"messenger\",\"call_count\":1,\"total_cputime\":1,\"total_time\":1,\"estimated_time_to_regain_access\":0}]}"
  1. ensure exception object can get usage headers
# case 1
begin
  response = Facebook::Messenger::Bot.deliver({}, ...)
  # <Facebook::Messenger::Bot::BadParameterError: (#100) The parameter recipient is required (subcode: )>
rescue Facebook::Messenger::FacebookError => e
  e.code
  # => 100
  e.buc_usage
  # => nil
  e.app_usage
  # => "{\"call_count\":0,\"total_cputime\":0,\"total_time\":0}"
end

# case 2
begin
  response = Facebook::Messenger::Bot.deliver(message_obj, ... )
  # (#613) Calls to this api have exceeded the rate limit
rescue Facebook::Messenger::LimitError => e
  e.code
  # => 613
  e.buc_usage
  # => {\"YOUR_PAGE_ID\":[{\"type\":\"pages\",\"call_count\":107,\"total_cputime\":1,\"total_time\":125,\"estimated_time_to_regain_access\":1406}]}

  # NOTE: estimated_time_to_regain_access -> Time, in minutes, until calls will not longer be throttled.

and this PR should resolve this issue #127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant