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 the ability to set request timeout options #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions lib/amplitude_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ def track_body(*events)
#
# Send one or more Events to the Amplitude API
def track(*events)
Faraday.post(TRACK_URI_STRING, track_body(events), "Content-Type" => "application/json")
Faraday.post(TRACK_URI_STRING, track_body(events), "Content-Type" => "application/json") do |request|
request.options.open_timeout = config.open_timeout
request.options.read_timeout = config.read_timeout
request.options.write_timeout = config.write_timeout
end
end

# ==== Identification related methods
Expand Down Expand Up @@ -132,7 +136,11 @@ def identify_body(*identifications)
#
# Send one or more Identifications to the Amplitude Identify API
def identify(*identifications)
Faraday.post(IDENTIFY_URI_STRING, identify_body(identifications))
Faraday.post(IDENTIFY_URI_STRING, identify_body(identifications)) do |request|
request.options.open_timeout = config.open_timeout
request.options.read_timeout = config.read_timeout
request.options.write_timeout = config.write_timeout
end
end

# ==== Event Segmentation related methods
Expand All @@ -157,7 +165,7 @@ def identify(*identifications)
#
# @return [ Faraday::Response ]
def segmentation(event, start_time, end_time, **options)
Faraday.get SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: {
params = {
e: event.to_json,
m: options[:m],
start: start_time.strftime("%Y%m%d"),
Expand All @@ -167,6 +175,12 @@ def segmentation(event, start_time, end_time, **options)
g: options[:g],
limit: options[:limit]
}.delete_if { |_, value| value.nil? }

Faraday.get(SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: params) do |request|
request.options.open_timeout = config.open_timeout
request.options.read_timeout = config.read_timeout
request.options.write_timeout = config.write_timeout
end
end

# Delete a user from amplitude
Expand All @@ -190,6 +204,9 @@ def delete(user_ids: nil, amplitude_ids: nil, requester: nil, ignore_invalid_id:

faraday = Faraday.new do |conn|
conn.request :basic_auth, config.api_key, config.secret_key
conn.options.open_timeout = config.open_timeout
conn.options.read_timeout = config.read_timeout
conn.options.write_timeout = config.write_timeout
end

faraday.post(
Expand Down
7 changes: 5 additions & 2 deletions lib/amplitude_api/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Config

attr_accessor :api_key, :secret_key, :whitelist, :time_formatter,
:event_properties_formatter, :user_properties_formatter,
:options
:options, :timeout, :open_timeout, :read_timeout, :write_timeout

def initialize
self.class.defaults.each { |k, v| send("#{k}=", v) }
Expand Down Expand Up @@ -44,7 +44,10 @@ def defaults
whitelist: base_properties + revenue_properties + optional_properties,
time_formatter: ->(time) { time ? time.to_i * 1_000 : nil },
event_properties_formatter: ->(props) { props || {} },
user_properties_formatter: ->(props) { props || {} }
user_properties_formatter: ->(props) { props || {} },
open_timeout: 60, # Net::HTTP default. Number of seconds to wait for the connection to open
read_timeout: 60, # Net::HTTP default. Number of seconds to wait for one block to be read
write_timeout: 60 # Net::HTTP default. Number of seconds to wait for one block to be written
}
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/amplitude_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@
end

describe ".delete" do
let(:connection) { instance_double("Faraday::Connection", post: nil, request: nil) }
let(:connection) { instance_double("Faraday::Connection", post: nil, request: nil, options: options) }
let(:options) { Faraday::RequestOptions.new }

before do
allow(Faraday).to receive(:new).and_yield(connection).and_return(connection)
Expand Down