Skip to content

Commit

Permalink
Support faraday 2 & streaming API (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyacheslav authored Oct 22, 2022
1 parent 8ab3890 commit a8941f3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
ruby-version: 2.7
bundler-cache: true # 'bundle install' and cache
env:
BUNDLE_WITHOUT: "development:test"
BUNDLE_WITH: lint

- name: Rubocop
Expand All @@ -34,7 +33,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ['2.5', '2.6', '2.7']
ruby: [ '2.6', '2.7', '3.0', '3.1' ]

steps:
- uses: actions/checkout@v1
Expand Down
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ require:
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRubyVersion: 2.5
TargetRubyVersion: 2.6
NewCops: enable
SuggestExtensions: false

Style/DoubleNegation:
Enabled: false
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ source 'https://rubygems.org'
gemspec

# Required gems to run all Faraday tests
gem 'multipart-parser'

group :lint, :development do
gem 'rubocop', '~> 0.91.1'
gem 'rubocop', '~> 1.37'
gem 'rubocop-packaging', '~> 0.5'
gem 'rubocop-performance', '~> 1.0'
end
5 changes: 3 additions & 2 deletions faraday-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/lostisland/faraday-http'
spec.license = 'MIT'

spec.required_ruby_version = '>= 2.5'
spec.required_ruby_version = '>= 2.6'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/lostisland/faraday-http'
spec.metadata['changelog_uri'] = 'https://github.com/lostisland/faraday-http/releases'
spec.metadata['rubygems_mfa_required'] = 'true'

spec.files = Dir.glob('lib/**/*') + %w[README.md LICENSE]
spec.require_paths = ['lib']

spec.add_dependency 'faraday', '~> 1.0'
spec.add_dependency 'faraday', '~> 2.5'
spec.add_dependency 'http', '>= 4.0', '< 6'

spec.add_development_dependency 'bundler', '~> 2.0'
Expand Down
59 changes: 34 additions & 25 deletions lib/faraday/adapter/http.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'http'

module Faraday
class Adapter
# HTTP.rb adapter.
class HTTP < Faraday::Adapter
dependency 'http'

# Takes the environment and performs the request.
#
# @param env [Faraday::Env] the request environment.
Expand All @@ -20,13 +20,22 @@ def call(env)
private

def perform_request(env)
conn = setup_connection(env)
connection = setup_connection(env)

resp = conn.request env[:method], env[:url],
body: env[:body],
ssl_context: ssl_context(env[:ssl])
response = connection.request env[:method], env[:url],
body: env[:body],
ssl_context: ssl_context(env[:ssl])

if env.stream_response?
env.stream_response do |&on_data|
request_with_wrapped_block(response, env, &on_data)
end
body = ''
else
body = response.body.to_s
end

save_response(env, resp.code, resp.body.to_s, resp.headers, resp.status.reason)
save_response(env, response.code, body, response.headers, response.status.reason)
rescue ::HTTP::TimeoutError
raise Faraday::TimeoutError, $ERROR_INFO
rescue ::HTTP::ConnectionError
Expand All @@ -37,6 +46,14 @@ def perform_request(env)
raise
end

def request_with_wrapped_block(response, env)
return unless block_given?

while (chunk = response.body.readpartial)
yield(chunk, env)
end
end

def setup_connection(env)
conn = ::HTTP

Expand Down Expand Up @@ -97,27 +114,19 @@ def ssl_context(ssl)
end

def ssl_client_cert(cert)
case cert
when NilClass then nil
when String
OpenSSL::X509::Certificate.new(File.read(cert))
when OpenSSL::X509::Certificate
cert
else
raise Faraday::Error, "invalid ssl.client_cert: #{cert.inspect}"
end
return nil if cert.nil?
return OpenSSL::X509::Certificate.new(File.read(cert)) if cert.is_a(String)
return cert if cert.is_a?(OpenSSL::X509::Certificate)

raise Faraday::Error, "invalid ssl.client_cert: #{cert.inspect}"
end

def ssl_client_key(cert)
case cert
when NilClass then nil
when String
OpenSSL::PKey::RSA.new(File.read(cert))
when OpenSSL::PKey::RSA, OpenSSL::PKey::DSA
cert
else
raise Faraday::Error, "invalid ssl.client_key: #{cert.inspect}"
end
return nil if cert.nil?
return OpenSSL::PKey::RSA.new(File.read(cert)) if cert.is_a(String)
return cert if cert.is_a?(OpenSSL::PKey::RSA, OpenSSL::PKey::DSA)

raise Faraday::Error, "invalid ssl.client_key: #{cert.inspect}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/http/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Faraday
module Http
VERSION = '1.1.0'
VERSION = '2.0.0'
end
end
5 changes: 2 additions & 3 deletions spec/faraday/http/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Faraday::Adapter::HTTP do
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method,
:skip_response_body_on_head, :local_socket_binding
:skip_response_body_on_head, :local_socket_binding, :streaming

it_behaves_like 'an adapter'

Expand All @@ -20,10 +20,9 @@

let(:conn) do
conn_options[:ssl] ||= {}
conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
conn_options[:ssl][:ca_file] ||= ENV.fetch('SSL_FILE', nil)

Faraday.new(remote, conn_options) do |conn|
conn.request :multipart
conn.request :url_encoded
conn.response :raise_error
conn.adapter described_class, *adapter_options
Expand Down

0 comments on commit a8941f3

Please sign in to comment.