diff --git a/README.md b/README.md index 676278b..8f5499d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/ezclient/request.rb b/lib/ezclient/request.rb index 5831eb6..1d0869b 100644 --- a/lib/ezclient/request.rb +++ b/lib/ezclient/request.rb @@ -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 @@ -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) @@ -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) diff --git a/spec/ezclient_spec.rb b/spec/ezclient_spec.rb index f40d2e0..4854bc5 100644 --- a/spec/ezclient_spec.rb +++ b/spec/ezclient_spec.rb @@ -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", @@ -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