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 options for http timeout protection (without Faraday) #43

Merged
merged 3 commits into from
May 30, 2018
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Your contribution here!

### Added
- [#43](https://github.com/DripEmail/drip-ruby/pull/43): Additional timeout parameters for client - [@tboyko](https://github.com/tboyko)

## [3.0.0] - 2018-05-29

[3.0.0]: https://github.com/DripEmail/drip-ruby/compare/v2.0.0...v3.0.0
Expand Down
19 changes: 17 additions & 2 deletions lib/drip/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ class Client

REDIRECT_LIMIT = 10

attr_accessor :access_token, :api_key, :account_id, :url_prefix
attr_accessor :access_token, :api_key, :account_id, :url_prefix, :http_open_timeout, :http_timeout

def initialize(options = {})
@account_id = options[:account_id]
@access_token = options[:access_token]
@api_key = options[:api_key]
@url_prefix = options[:url_prefix] || "https://api.getdrip.com/v2/"
@http_open_timeout = options[:http_open_timeout]
@http_timeout = options[:http_timeout]
yield(self) if block_given?
end

Expand Down Expand Up @@ -81,7 +83,7 @@ def make_request(verb_klass, uri, options, step = 0)
raise TooManyRedirectsError, 'too many HTTP redirects' if step >= REDIRECT_LIMIT

build_response do
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
Net::HTTP.start(uri.host, uri.port, connection_options(uri.scheme)) do |http|
if verb_klass.is_a? Net::HTTP::Get
uri.query = URI.encode_www_form(options)
end
Expand Down Expand Up @@ -116,5 +118,18 @@ def build_response(&block)
response = yield
Drip::Response.new(response.code.to_i, response.body)
end

def connection_options(uri_scheme)
options = { use_ssl: uri_scheme == "https" }

if @http_open_timeout
options[:open_timeout] = @http_open_timeout
options[:ssl_timeout] = @http_open_timeout
end

options[:read_timeout] = @http_timeout if @http_timeout

options
end
end
end
6 changes: 5 additions & 1 deletion test/drip/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class Drip::ClientTest < Drip::TestCase
client = Drip::Client.new(
account_id: "1234567",
api_key: "aaaa",
access_token: "bbbb"
access_token: "bbbb",
http_open_timeout: 20,
http_timeout: 25
)

assert_equal "1234567", client.account_id
assert_equal "aaaa", client.api_key
assert_equal "bbbb", client.access_token
assert_equal 20, client.http_open_timeout
assert_equal 25, client.http_timeout
end
end

Expand Down