lib/oanda_api/client/client.rb in oanda_api-0.9.4 vs lib/oanda_api/client/client.rb in oanda_api-0.9.5

- old
+ new

@@ -21,15 +21,16 @@ # Use a custom JSON parser parser OandaAPI::Client::JsonParser # Resource URI templates BASE_URI = { - live: "https://api-fxtrade.oanda.com/[API_VERSION]", - practice: "https://api-fxpractice.oanda.com/[API_VERSION]", - sandbox: "http://api-sandbox.oanda.com/[API_VERSION]" + live: "https://api-fxtrade.oanda.com/", + practice: "https://api-fxpractice.oanda.com/", + sandbox: "http://api-sandbox.oanda.com/" } + # @private # Camelizes keys and transforms array values into comma-delimited strings. # # @return [String] a url encoded query string. query_string_normalizer proc { |hash| @@ -48,24 +49,25 @@ # Common initializations # @param [Hash] options Specifies overrides to default settings. # Overrides for the persistent connection adapter are specified # by including an :connection_adapter_options: {} hash. - # @return [OandaAPI::Client] + # @return [OandaAPI::Client] def initialize(options={}) super() load_persistent_connection_adapter options[:connection_adapter_options] || {} end # Returns an absolute URI for a resource request. # - # @param [String] path the path portion of the URI. + # @param [OandaAPI::Client::ResourceDescriptor] resource_descriptor metadata + # describing the requested resource. # # @return [String] a URI. - def api_uri(path) - uri = "#{BASE_URI[domain]}#{path}" - uri.sub "[API_VERSION]", OandaAPI.configuration.rest_api_version + 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 # Binds a persistent connection adapter. See documentation for the # persistent_httparty gem for configuration details. # @param [Hash] options Specifies overrides for the connection adapter. @@ -76,10 +78,11 @@ 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 options Client.persistent_connection_adapter adapter_config end @@ -100,23 +103,25 @@ # @return [OandaAPI::ResourceCollection] if the API request returns a # collection of resources. # # @raise [OandaAPI::RequestError] if the API return code is not 2xx. 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 - method = Client.map_method_to_http_verb(method) params_key = [:post, :patch, :put].include?(method) ? :body : :query Client.throttle_request_rate Client.send method, - api_uri(path), + 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 end - handle_response response, ResourceDescriptor.new(path, method) + handle_response response, resource_descriptor rescue Http::Exceptions::HttpException => e raise OandaAPI::RequestError, e.message end # @private @@ -156,10 +161,10 @@ def self.throttle_request_rate now = Time.now delta = now - (last_request_at || now) _throttle(delta, now) if delta < OandaAPI.configuration.min_request_interval && OandaAPI.configuration.use_request_throttling? - last_request_at = Time.now + self.last_request_at = Time.now end # @private # The local time of the most recently throttled request. #