# frozen_string_literal: true module Cryptum # This module is used to Interact with the APIs module API # Module specifically related to orders history retrieval. module Rest # Actually Make a REST API call public_class_method def self.call(opts = {}) env = opts[:env] option_choice = opts[:option_choice] order_type = opts[:order_type] event_notes = opts[:event_notes] api_endpoint = opts[:api_endpoint] base_increment = opts[:base_increment].to_f api_key = env[:api_key] api_secret = env[:api_secret] api_passphrase = env[:api_passphrase] api_endpoint = 'https://api.exchange.coinbase.com' api_endpoint = 'https://api-public.sandbox.exchange.coinbase.com' if env[:env] == :sandbox api_endpoint = opts[:api_endpoint] if opts[:api_endpoint] http_method = if opts[:http_method].nil? :GET else opts[:http_method].to_s.upcase.scrub.strip.chomp.to_sym end api_call = opts[:api_call].to_s.scrub params = opts[:params] http_body = opts[:http_body].to_s.scrub.strip.chomp max_conn_attempts = 30 conn_attempt = 0 begin conn_attempt += 1 if option_choice.proxy rest_client = RestClient rest_client.proxy = option_choice.proxy rest_client_request = rest_client::Request else rest_client_request = RestClient::Request end api_signature_response = Cryptum::API::Signature.generate( api_secret: api_secret, http_method: http_method, api_call: api_call, params: params, http_body: http_body ) api_signature = api_signature_response[:api_signature] api_timestamp = api_signature_response[:api_timestamp] case http_method when :GET headers = { content_type: 'application/json; charset=UTF-8', CB_ACCESS_TIMESTAMP: api_timestamp, CB_ACCESS_PASSPHRASE: api_passphrase, CB_ACCESS_KEY: api_key, CB_ACCESS_SIGN: api_signature } headers[:params] = params if params headers[:ORDER_TYPE] = order_type if order_type headers[:EVENT_NOTES] = event_notes if event_notes response = rest_client_request.execute( method: :GET, url: "#{api_endpoint}#{api_call}", headers: headers, verify_ssl: false ) when :DELETE headers = { content_type: 'application/json; charset=UTF-8', CB_ACCESS_TIMESTAMP: api_timestamp, CB_ACCESS_PASSPHRASE: api_passphrase, CB_ACCESS_KEY: api_key, CB_ACCESS_SIGN: api_signature } headers[:params] = params if params headers[:ORDER_TYPE] = order_type if order_type headers[:EVENT_NOTES] = event_notes if event_notes response = rest_client_request.execute( method: :DELETE, url: "#{api_endpoint}#{api_call}", headers: headers, verify_ssl: false ) when :POST headers = { content_type: 'application/json; charset=UTF-8', CB_ACCESS_TIMESTAMP: api_timestamp, CB_ACCESS_PASSPHRASE: api_passphrase, CB_ACCESS_KEY: api_key, CB_ACCESS_SIGN: api_signature } headers[:params] = params if params headers[:ORDER_TYPE] = order_type if order_type headers[:EVENT_NOTES] = event_notes if event_notes response = rest_client_request.execute( method: :POST, url: "#{api_endpoint}#{api_call}", headers: headers, payload: http_body, verify_ssl: false ) else msg = "Unsupported HTTP Method #{http_method}" Cryptum::Log.append(level: :debug, msg: msg, which_self: self) end resp = JSON.parse(response, symbolize_names: true) resp ||= [] rescue RestClient::Unauthorized => e Cryptum::Log.append(level: :debug, msg: e, which_self: self) if conn_attempt > max_conn_attempts Cryptum::Log.append( level: :error, msg: e, which_self: self ) end sleep 60 retry end rescue RestClient::ExceptionWithResponse => e debug = "URL: #{api_endpoint}#{api_call}" debug += "PARAMS: #{params.inspect}" debug += "HTTP POST BODY: #{http_body.inspect}" if http_body != '' debug += "#{e}\n#{e.response}\n\n\n" Cryptum::Log.append(level: :debug, msg: debug, which_self: self) insufficient_funds = '{"message":"Insufficient funds"}' size -= base_increment if e.response == insufficient_funds sleep 0.3 retry rescue RestClient::TooManyRequests => e debug = "URL: #{api_endpoint}#{api_call}" debug += "PARAMS: #{params.inspect}" debug += "HTTP POST BODY: #{http_body.inspect}" if http_body != '' debug += "#{e}\n#{e.response}\n\n\n" Cryptum::Log.append(level: :debug, msg: debug, which_self: self) sleep 1 retry rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self) end # Display Usage for this Module public_class_method def self.help puts "USAGE: rest_response = #{self}.call( env: 'required - Coinbase::Option::Environment.get Object' ) " end end end end