Skip to content

Commit

Permalink
Add elapsed_seconds to request object (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirIgor authored Nov 15, 2022
1 parent 0192924 commit 8606be0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
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

0 comments on commit 8606be0

Please sign in to comment.