Skip to content

Commit

Permalink
Allow options to be passed through to HTTPI's Request initializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Campbell committed Dec 4, 2015
1 parent f68aca9 commit 3b28053
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/betfair/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "betfair/api/rpc"
require "betfair/utils"
require "httpi"
require "active_support/core_ext/hash"

module Betfair
class Client
Expand All @@ -17,21 +18,22 @@ class Client
account: [:get_account_funds]
}

attr_accessor :settings, :persistent_headers
attr_accessor :settings, :request_opts, :persistent_headers

def initialize(headers = {}, opts = {}, api_type = :rest)
setting_opts = opts.extract!(:retries, :adapter)
@settings = DEFAULT_SETTINGS.merge(setting_opts)
@persistent_headers = headers
@request_opts = opts

def initialize(headers = {}, api_type = :rest, settings = {})
@settings = DEFAULT_SETTINGS.merge(settings)
@persistent_headers = {}
@persistent_headers.merge!(headers)
extend_api(api_type)
end

private

[:get, :post].each do |verb|
define_method(verb) do |*args|
request = configure_request(*args)
response = attempt((settings[:retries] || 1).times) do
response = attempt((settings[:retries]).times) do
HTTPI.request(verb, request, settings[:adapter])
end
response.body
Expand All @@ -40,14 +42,15 @@ def initialize(headers = {}, api_type = :rest, settings = {})

def configure_request(opts = {})
opts[:headers] = persistent_headers.merge(opts[:headers] || {})
request = HTTPI::Request.new(opts)

# It would be nice to have HTTPI do this itself but HTTPI::Request#mass_assign doesn't merge auth fields
unless opts[:cert_file_path].nil? or opts[:cert_key_file_path].nil?
request.auth.ssl.cert_key_file = opts[:cert_key_file_path]
request.auth.ssl.cert_file = opts[:cert_file_path]
HTTPI::Request.new(request_opts.merge(opts)).tap do |r|
# It would be nice to have HTTPI do this itself but
# HTTPI::Request#mass_assign doesn't merge auth fields
if opts[:cert_file_path] && opts[:cert_key_file_path]
r.auth.ssl.cert_key_file = opts[:cert_key_file_path]
r.auth.ssl.cert_file = opts[:cert_file_path]
end
end
request
end

def extend_api(type)
Expand Down

0 comments on commit 3b28053

Please sign in to comment.