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

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 16 additions & 3 deletions lib/koala/api/graph_batch_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ def execute(http_options = {})
end

original_api.graph_call("/", args, "post", http_options) do |response|
raise bad_response if response.nil?
raise bad_response("Facebook returned an empty body") if response.nil?

# 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]
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
response = JSON.load(response.body)
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
raise bad_response("Facebook returned an invalid body") unless response.is_a?(Array)
end

batch_results += generate_results(response, batch)
end
Expand Down Expand Up @@ -81,9 +89,9 @@ def generate_results(response, batch)
end
end

def bad_response
def bad_response(message)
# Facebook sometimes reportedly returns an empty body at times
BadFacebookResponse.new(200, "", "Facebook returned an empty body")
BadFacebookResponse.new(200, "", message)
end

def result_from_response(response, options)
Expand Down Expand Up @@ -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(
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
response["code"].to_i,
response["body"],
headers)
else GraphCollection.evaluate(result, original_api)
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/cases/graph_api_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@
expect(result[0]).to eq({"Content-Type" => "text/javascript; charset=UTF-8"})
end

it "returns the complete response if http_component is response" do
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '[{"code":203,"headers":[{"name":"Content-Type","value":"text/javascript; charset=UTF-8"}],"body":"{\"id\":\"1234\"}"}]', {}))
result = @api.batch do |batch_api|
batch_api.get_object(KoalaTest.user1, {}, :http_component => :response)
end
expect result[0].status == 203
expect result[0].body == "{\"id\":\"1234\"}"
expect result[0].headers == {"Content-Type"=>"text/javascript; charset=UTF-8"}
end

describe "if it errors" do
it "raises an APIError if the response is not 200" do
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(500, "[]", {}))
Expand All @@ -406,6 +416,13 @@
Koala::Facebook::API.new("foo").batch {|batch_api| batch_api.get_object('me') }
}.to raise_exception(Koala::Facebook::BadFacebookResponse)
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
end

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') }
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
}.to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an invalid body \[HTTP 200\]/)
end

context "with error info" do
before :each do
Expand Down
Loading