Skip to content

Commit

Permalink
fix httpx adapter issues
Browse files Browse the repository at this point in the history
treating DNS resolution errors as ConnectionFailed; removing multipart header set by the openapi-generated code, as it does not contain boundary, and interferes with the generation from httpx, which appropriately deals with mime-types already
  • Loading branch information
HoneyryderChuck committed Oct 9, 2023
1 parent 10d54b8 commit e8be017
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end

Expand Down Expand Up @@ -73,14 +73,11 @@
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body

data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end

def session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def call_api(http_method, path, opts = {})
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end

Expand Down Expand Up @@ -100,13 +100,11 @@ def build_request(http_method, path, opts = {})
if config.debugging
config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
else
body_params = {}
end
req_opts = {
:headers => HTTPX::Headers.new(header_params),
**body_params
:headers => HTTPX::Headers.new(header_params)
}
req_opts.merge!(body_params) if body_params
req_opts[:params] = query_params if query_params && !query_params.empty?
session.request(http_method, url, **req_opts)
end
Expand All @@ -121,14 +119,11 @@ def build_request_body(header_params, form_params, body)
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body

data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end

def session
Expand Down
11 changes: 4 additions & 7 deletions samples/client/petstore/ruby-httpx/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def call_api(http_method, path, opts = {})
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end

Expand Down Expand Up @@ -119,14 +119,11 @@ def build_request_body(header_params, form_params, body)
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body

data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end

def session
Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore/ruby-httpx/spec/custom/pet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
it "should fetch a pet object with http info" do
pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id)
expect(status_code).to eq(200)
expect(headers['Content-Type']).to eq('application/json')
expect(headers['content-type']).to eq('application/json')
expect(pet).to be_a(Petstore::Pet)
expect(pet.id).to eq(@pet_id)
expect(pet.name).to eq("RUBY UNIT TESTING")
Expand All @@ -123,8 +123,8 @@
# skip the check as the response contains a timestamp that changes on every response
# expect(e.message).to eq("Error message: the server returns an error\nHTTP status code: 404\nResponse headers: {\"Date\"=>\"Tue, 26 Feb 2019 04:35:40 GMT\", \"Access-Control-Allow-Origin\"=>\"*\", \"Access-Control-Allow-Methods\"=>\"GET, POST, DELETE, PUT\", \"Access-Control-Allow-Headers\"=>\"Content-Type, api_key, Authorization\", \"Content-Type\"=>\"application/json\", \"Connection\"=>\"close\", \"Server\"=>\"Jetty(9.2.9.v20150224)\"}\nResponse body: {\"code\":1,\"type\":\"error\",\"message\":\"Pet not found\"}")
expect(e.response_body).to eq('{"code":1,"type":"error","message":"Pet not found"}')
expect(e.response_headers).to include('Content-Type')
expect(e.response_headers['Content-Type']).to eq('application/json')
expect(e.response_headers).to include('content-type')
expect(e.response_headers['content-type']).to eq('application/json')
end
end

Expand Down

0 comments on commit e8be017

Please sign in to comment.