Module: OandaAPI::Client
- Includes:
- HTTParty
- Included in:
- TokenClient, UsernameClient
- Defined in:
- lib/oanda_api/client/client.rb,
lib/oanda_api/client/json_parser.rb,
lib/oanda_api/client/token_client.rb,
lib/oanda_api/client/namespace_proxy.rb,
lib/oanda_api/client/username_client.rb,
lib/oanda_api/client/resource_descriptor.rb
Overview
Provides everything needed for accessing the API.
- Uses persistant http connections.
- Uses
OpenSSL::SSL::VERIFY_PEER
to always validate SSL certificates. - Uses compression if enabled (see OandaAPI::Configuration#use_compression).
- Uses request rate limiting if enabled (see OandaAPI::Configuration#use_request_throttling).
Defined Under Namespace
Classes: JsonParser, NamespaceProxy, ResourceDescriptor, TokenClient, UsernameClient
Constant Summary collapse
- BASE_URI =
Resource URI templates
{ live: "https://api-fxtrade.oanda.com/", practice: "https://api-fxpractice.oanda.com/", sandbox: "http://api-sandbox.oanda.com/" }
Class Method Summary collapse
- .last_request_at ⇒ Object
- .last_request_at=(value) ⇒ Object
-
.last_throttled_at ⇒ Time?
The local time of the most recently throttled request.
-
.map_method_to_http_verb(method) ⇒ Symbol
Maps An API action to a corresponding http verb.
-
.throttle_request_rate ⇒ void
Limits the execution rate of consecutive requests.
Instance Method Summary collapse
-
#api_uri(resource_descriptor) ⇒ String
Returns an absolute URI for a resource request.
-
#execute_request(method, path, conditions = {}) ⇒ OandaAPI::ResourceBase, OandaAPI::ResourceCollection
Executes an http request.
-
#initialize(options = {}) ⇒ OandaAPI::Client
Common initializations.
-
#load_persistent_connection_adapter(options = {}) ⇒ void
Binds a persistent connection adapter.
-
#proc ⇒ String
Camelizes keys and transforms array values into comma-delimited strings.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ NamespaceProxy (private)
Enables method-chaining.
223 224 225 |
# File 'lib/oanda_api/client/client.rb', line 223 def method_missing(sym, *args) NamespaceProxy.new self, sym, args.first end |
Class Method Details
.last_request_at ⇒ Object
147 148 149 |
# File 'lib/oanda_api/client/client.rb', line 147 def self.last_request_at @throttle_mutex.synchronize { @last_request_at } end |
.last_request_at=(value) ⇒ Object
151 152 153 |
# File 'lib/oanda_api/client/client.rb', line 151 def self.last_request_at=(value) @throttle_mutex.synchronize { @last_request_at = value } end |
.last_throttled_at ⇒ Time?
The local time of the most recently throttled request.
181 182 183 |
# File 'lib/oanda_api/client/client.rb', line 181 def self.last_throttled_at @throttle_mutex.synchronize { @throttled_at } end |
.map_method_to_http_verb(method) ⇒ Symbol
Maps An API action to a corresponding http verb.
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/oanda_api/client/client.rb', line 134 def self.map_method_to_http_verb(method) case method when :create :post when :close :delete when :update :patch else method end end |
.throttle_request_rate ⇒ void
This method returns an undefined value.
Limits the execution rate of consecutive requests. Specified by OandaAPI::Configuration#max_requests_per_second. Only enforced if OandaAPI::Configuration#use_request_throttling? is enabled.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/oanda_api/client/client.rb', line 161 def self.throttle_request_rate last_request_time = last_request_at now = Time.now if last_request_time delta = now - last_request_time _throttle(delta, now) if delta < OandaAPI.configuration.min_request_interval && OandaAPI.configuration.use_request_throttling? end self.last_request_at = Time.now end |
Instance Method Details
#api_uri(resource_descriptor) ⇒ String
Returns an absolute URI for a resource request.
66 67 68 69 |
# File 'lib/oanda_api/client/client.rb', line 66 def api_uri(resource_descriptor) api_version = resource_descriptor.labs? ? OandaAPI.configuration.labs_api_version : OandaAPI.configuration.rest_api_version "#{BASE_URI[domain]}#{api_version}#{resource_descriptor.path}" end |
#execute_request(method, path, conditions = {}) ⇒ OandaAPI::ResourceBase, OandaAPI::ResourceCollection
Executes an http request.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/oanda_api/client/client.rb', line 107 def execute_request(method, path, conditions = {}) method = Client.map_method_to_http_verb method resource_descriptor = ResourceDescriptor.new path, method response = Http::Exceptions.wrap_and_check do params_key = [:post, :patch, :put].include?(method) ? :body : :query Client.throttle_request_rate Client.send method, api_uri(resource_descriptor), params_key => Utils.stringify_keys(conditions.merge(default_params)), :headers => OandaAPI.configuration.headers.merge(headers), :open_timeout => OandaAPI.configuration.open_timeout, :read_timeout => OandaAPI.configuration.read_timeout, :timeout => OandaAPI.configuration.open_timeout end handle_response response, resource_descriptor rescue Http::Exceptions::HttpException => e raise OandaAPI::RequestError, e. end |
#initialize(options = {}) ⇒ OandaAPI::Client
Common initializations
55 56 57 58 |
# File 'lib/oanda_api/client/client.rb', line 55 def initialize(={}) super() load_persistent_connection_adapter [:connection_adapter_options] || {} end |
#load_persistent_connection_adapter(options = {}) ⇒ void
This method returns an undefined value.
Binds a persistent connection adapter. See documentation for the persistent_httparty gem for configuration details.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/oanda_api/client/client.rb', line 76 def load_persistent_connection_adapter(={}) adapter_config = { name: "oanda_api", idle_timeout: 10, keep_alive: 30, warn_timeout: 2, pool_size: OandaAPI.configuration.connection_pool_size, verify_mode: OpenSSL::SSL::VERIFY_PEER }.merge Client.persistent_connection_adapter adapter_config end |
#proc ⇒ String
Camelizes keys and transforms array values into comma-delimited strings.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/oanda_api/client/client.rb', line 36 query_string_normalizer proc { |hash| Array(hash).sort_by { |key, _value| key.to_s }.map do |key, value| if value.nil? Utils.camelize(key.to_s) elsif value.respond_to?(:to_ary) serialized = URI.encode value.join(","), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") "#{Utils.camelize(key)}=#{serialized}" else HashConversions.to_params Utils.camelize(key) => value end end.flatten.join("&") } |