Module: Cryptum::API::Rest

Defined in:
lib/cryptum/api/rest.rb

Overview

Module specifically related to orders history retrieval.

Class Method Summary collapse

Class Method Details

.call(opts = {}) ⇒ Object

Actually Make a REST API call



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cryptum/api/rest.rb', line 9

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

.helpObject

Display Usage for this Module



165
166
167
168
169
170
171
# File 'lib/cryptum/api/rest.rb', line 165

public_class_method def self.help
  puts "USAGE:
    rest_response = #{self}.call(
      env: 'required - Coinbase::Option::Environment.get Object'
    )
  "
end