Class: Binance::SDK::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/binance/sdk/request.rb

Constant Summary collapse

SECURITY_TYPES =
[:none, :trade, :user_data, :user_stream, :market_data, :margin].freeze

Class Method Summary collapse

Class Method Details

.send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none, tld: Configuration.tld, api_key: nil, api_secret_key: nil, testnet: false, futures: false) ⇒ Object

Raises:



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
# File 'lib/binance/sdk/request.rb', line 9

def send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none, tld: Configuration.tld, api_key: nil, api_secret_key: nil, testnet: false, futures: false)
  Configuration.validate_tld!(tld)

  if Configuration.testnet?
    self.base_uri 'https://testnet.binancefuture.com'
  else
    if Configuration.futures?
      self.base_uri 'https://fapi.binance.com'
    else
      self.base_uri 'https://api.binance.com'
    end
  end

  raise Error.new(message: "Invalid security type #{security_type}") unless SECURITY_TYPES.include?(security_type)
  all_headers = default_headers(api_key_type: api_key_type, security_type: security_type, api_key: api_key)
  params.delete_if { |k, v| v.nil? }

  if %w(trade user_data).include?(security_type&.to_s)
    signature = signed_request_signature(params: params, api_secret_key: api_secret_key)
    params.merge!(signature: signature)
  end

  case method
  when :get
    response = get(path, headers: all_headers, query: params)
  when :post
    response = post(path, query: params, headers: all_headers)
  when :put
    response = put(path, query: params, headers: all_headers)
  when :delete
    response = delete(path, query: params, headers: all_headers)
  else
    raise Error.new(message: "invalid http method used: #{method}")
  end

  process!(response: response || "{}")
end