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

Add new http_component that would return the body of the response and custom error messages. #685

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

pri1012
Copy link
Contributor

@pri1012 pri1012 commented Jan 10, 2024

Description:

This PR includes changes which will help use the latest koala version when http_component is set

Changes included:

  • Parsing of response: when http_component is set, we receive Koala::Http_service response instead of a hash and generate_results method handles only JSON response. This affects all batch request.

  • Custom Error message when the body is nil

@pri1012 pri1012 changed the title add new http_component that would return the body of the response Add new http_component that would return the body of the response and custom error messages. Jan 10, 2024
@saikambaiyyagari
Copy link

@ylecuyer @arsduo Could you please help review this PR?
Context: #648

@saikambaiyyagari
Copy link

Gentle bump! @ylecuyer @arsduo Could you please help review this PR?
Context: #648

@zenspider
Copy link

@ylecuyer & @arsduo nudge. what if anything can we do to help move these PRs along? Is there anything I can do to help get this moving?

# from graph_call so this needs to be parsed
# as generate_results method handles only JSON response
if http_options[:http_component]
response = JSON.load(response.body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you handle JSONError and add a spec for it. If fb sends a messed up body we will end up with a json error instead of a koala error.

And from experience, fb loves to send html error pages or null instead of well formed json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have added the code change to handle the JSON error and added test coverage as well.

@@ -406,6 +416,13 @@
Koala::Facebook::API.new("foo").batch {|batch_api| batch_api.get_object('me') }
}.to raise_exception(Koala::Facebook::BadFacebookResponse)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}.to raise_exception(Koala::Facebook::BadFacebookResponse)
}.to raise_exception(Koala::Facebook::BadFacebookResponse, /"Facebook returned an empty body"/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have updated the test to check for the error message as well.

# when http_component is set we receive Koala::Http_service response object
# from graph_call so this needs to be parsed
# as generate_results method handles only JSON response
if http_options[:http_component]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we need to add a check for :response because if you set http_component = header or status it will fail see https://github.com/pri1012/koala/blob/c3279139fab69d25d017cb9b83d1de9e18fd5018/lib/koala/api.rb#L57

it "raises a BadFacebookResponse if the body is non-empty, non-array" do
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, "200", {}))
expect {
Koala::Facebook::API.new("foo").batch(http_component: :response) {|batch_api| batch_api.get_object('me') }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test where http_component is status, and another one with headers

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. We have added tests for http_component :status and :headers as well.

@@ -138,6 +146,11 @@ def desired_component(component:, response:, headers:)
# facebook returns the headers as an array of k/v pairs, but we want a regular hash
when :headers then headers
# (see note in regular api method about JSON parsing)
when :response
Koala::HTTPService::Response.new(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's return result which is defined above. And also you are mixing code and status in this initialization

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point @ylecuyer. We have use result defined above.

@saikambaiyyagari
Copy link

@ylecuyer & @arsduo gentle nudge. Folks, we have addressed the review comments. Can we get your reviews and 👍🏾 if everything is OK?

@saikambaiyyagari
Copy link

@ylecuyer & @arsduo 🎗️ Can we get your reviews and 👍🏾 if everything is OK?

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.

None yet

4 participants