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 elapsed_seconds to request object #15

Merged
merged 3 commits into from
Nov 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ request.verb # => "POST"
request.url # => "http://example.com"
request.body # => '{"a": 1}'
request.headers # => { "Content-Type" => "application/json; charset=UTF-8", ... }
request.elapsed_seconds # => 0.08117745001072763
```

## Response object
Expand Down
5 changes: 4 additions & 1 deletion lib/ezclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EzClient::Request
query
].freeze

attr_accessor :verb, :url, :options
attr_accessor :verb, :url, :options, :elapsed_seconds

def initialize(verb, url, options)
self.verb = verb.to_s.upcase
Expand Down Expand Up @@ -101,6 +101,7 @@ def http_client
end

def perform_request
perform_started_at = EzClient.get_time
with_retry do
# Use original client so that connection can be reused
res = client.perform(http_request, http_options)
Expand All @@ -110,6 +111,8 @@ def perform_request
client.perform(request, http_options)
end
end
ensure
self.elapsed_seconds = EzClient.get_time - perform_started_at
end

def with_retry(&block)
Expand Down
2 changes: 2 additions & 0 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def self.sign!(*); end
expect(request.verb).to eq("POST")
expect(request.url).to eq("http://example.com")
expect(request.body).to eq("a=1")
expect(request.elapsed_seconds).to be_a(Float)

expect(request.headers).to eq(
"Connection" => "close",
Expand Down Expand Up @@ -258,6 +259,7 @@ def self.sign!(*); end
let(:on_error) do
proc do |request, error, metadata|
expect(request.url).to eq("http://example.com")
expect(request.elapsed_seconds).to be_a(Float)
expect(error).to be_a(StandardError)
expect(metadata).to eq(:smth)
calls << nil
Expand Down