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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/facebook/messenger/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class << self
#
# Returns a String describing the message ID if the message was sent,
# or raises an exception if it was not.
def deliver(message, page_id:)
def deliver(message, page_id:, return_response: false)
access_token = config.provider.access_token_for(page_id)
app_secret_proof = config.provider.app_secret_proof_for(page_id)

Expand All @@ -59,6 +59,8 @@ def deliver(message, page_id:)

Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response)

return response if return_response

response.body
end

Expand Down
3 changes: 3 additions & 0 deletions lib/facebook/messenger/bot/error_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def raise_errors_from(response)
error_code = error['code']
error_subcode = error['error_subcode']

error.merge!('app-usage' => response.headers['x-app-usage'],
'buc-usage' => response.headers['x-business-use-case-usage'])

raise_code_only_error(error_code, error) if error_subcode.nil?

raise_code_subcode_error(error_code, error_subcode, error)
Expand Down
4 changes: 4 additions & 0 deletions lib/facebook/messenger/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class FacebookError < Error
attr_reader :user_title
attr_reader :user_msg
attr_reader :fbtrace_id
attr_reader :buc_usage
attr_reader :app_usage

#
# Constructor function.
Expand All @@ -26,6 +28,8 @@ def initialize(error)
@user_title = error['error_user_title']
@user_msg = error['error_user_msg']
@fbtrace_id = error['fbtrace_id']
@buc_usage = error['buc-usage']
@app_usage = error['app-usage']
end

#
Expand Down
10 changes: 10 additions & 0 deletions spec/facebook/messenger/bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ def stub_request_to_return(hash)
expect(result).to eq({ recipient_id: recipient_id,
message_id: message_id }.to_json)
end

context 'with option `return_response`' do
it 'sends a message and return response' do
response = subject.deliver(payload, page_id: page_id, return_response: true)

expect(response.body).to eq({ recipient_id: recipient_id,
message_id: message_id }.to_json)
expect(response.headers).to eq(default_graph_api_response_headers)
end
end
end

context 'when Facebook had an internal server error' do
Expand Down
4 changes: 3 additions & 1 deletion spec/helpers/graph_api_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
def default_graph_api_response_headers
{
'Content-Type' => 'text/javascript; charset=UTF-8'
'Content-Type' => 'text/javascript; charset=UTF-8',
'x-business-use-case-usage' => "{\"123456789012345\":[{\"type\":\"messenger\",\"call_count\":1,\"total_cputime\":1,\"total_time\":1,\"estimated_time_to_regain_access\":0}]}",
'x-app-usage' => "{\"call_count\":0,\"total_cputime\":0,\"total_time\":0}"
}
end