Skip to content

Commit

Permalink
Add request_id option to client
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyboy committed Apr 17, 2018
1 parent 0eb29b2 commit a6a0946
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/ably/rest/client.rb
Expand Up @@ -83,6 +83,10 @@ class Client
# if empty or nil then fallback host functionality is disabled
attr_reader :fallback_hosts

# Whethere the {Client} has to add a random identifier to the path of a request
# @return [Boolean]
attr_reader :add_request_ids

# Creates a {Ably::Rest::Client Rest Client} and configures the {Ably::Auth} object for the connection.
#
# @param [Hash,String] options an options Hash used to configure the client and the authentication, or String with an API key or Token ID
Expand Down Expand Up @@ -146,6 +150,7 @@ def initialize(options)
@custom_host = options.delete(:rest_host)
@custom_port = options.delete(:port)
@custom_tls_port = options.delete(:tls_port)
@add_request_ids = options.delete(:add_request_ids) == false ? false : true

if options[:fallback_hosts_use_default] && options[:fallback_jhosts]
raise ArgumentError, "fallback_hosts_use_default cannot be set to trye when fallback_jhosts is also provided"
Expand Down Expand Up @@ -434,6 +439,16 @@ def send_request(method, path, params, options)
max_retry_duration = http_defaults.fetch(:max_retry_duration)
requested_at = Time.now
retry_count = 0
if @add_request_ids
if method == :get
path_with_params = Addressable::URI.new
else
path_with_params = Addressable::URI.parse(path)
end
path_with_params.query_values = {request_id: SecureRandom.hex(20)}
query = path_with_params.query
path = "#{path}?#{query}"
end

begin
use_fallback = can_fallback_to_alternate_ably_host? && retry_count > 0
Expand Down
7 changes: 7 additions & 0 deletions spec/unit/rest/client_spec.rb
Expand Up @@ -50,4 +50,11 @@
end
end
end

context 'request_id generation' do
let(:client_options) { {key: 'appid.keyuid:keysecret', add_request_ids: true} }
it 'includes request_id in URL' do
expect(subject.add_request_ids).to eql(true)
end
end
end

0 comments on commit a6a0946

Please sign in to comment.