Skip to content

Commit

Permalink
fix:httpmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Insmael committed Feb 26, 2024
1 parent 6c41c1c commit 0387fc4
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 100 deletions.
28 changes: 23 additions & 5 deletions lib/cryptomarket/http_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
require_relative 'exceptions'
require_relative 'credentials_factory'

def post?(method)
method.upcase == 'POST'
end

def get?(method)
method.upcase == 'GET'
end

def put?(method)
method.upcase == 'PUT'
end

def patch?(method)
method.upcase == 'PATCH'
end

module Cryptomarket
# Manager of http requests to the cryptomarket server
class HttpManager
Expand All @@ -24,7 +40,7 @@ def initialize(api_key:, api_secret:, window: nil)
def make_request(method:, endpoint:, params: nil, public: false)
uri = URI(@@API_URL + @@API_VERSION + endpoint)
payload = build_payload(params)
headers = build_headers(method, endpoint, params, public)
headers = build_headers(method, endpoint, payload, public)
if ((method.upcase == 'GET') || (method.upcase == 'PUT')) && !payload.nil?
uri.query = URI.encode_www_form payload
payload = nil
Expand All @@ -48,14 +64,16 @@ def build_payload(params)
return nil if params.nil?

payload = params.compact
payload = Hash[params.sort_by { |key, _val| key.to_s }] if params.is_a?(Hash)
payload = Hash[payload.sort_by { |key, _val| key.to_s }] if payload.is_a?(Hash)
payload
end

def do_request(method, uri, payload, headers)
response = RestClient::Request.execute(
method: method.downcase.to_sym, url: uri.to_s, payload: payload.to_json, headers: headers
)
args = { method: method.downcase.to_sym, url: uri.to_s, headers: headers }
if post?(method) || patch?(method)
args[:payload] = post?(method) ? payload.to_json : payload
end
response = RestClient::Request.execute(**args)
handle_response(response)
rescue RestClient::ExceptionWithResponse => e
handle_response(e.response)
Expand Down
4 changes: 4 additions & 0 deletions tests/checker_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ def gen_check_notification_hash_list_callback(check_fn, veredict_checker)
def gen_check_notification_hash_callback(check_fn, veredict_checker)
->(notification, _notification_type) { check_hash(notification, check_fn, veredict_checker) }
end

def gen_check_notification_list_w_n_type_callback(check_fn, veredict_checker)
->(notification, _notification_type) { check_list(notification, check_fn, veredict_checker) }
end
28 changes: 12 additions & 16 deletions tests/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def defined(a_hash, key)
true
end

# good_params checks all of the values in the fields list to be present in the dict, and if they are
# present, check the defined() condition to be true. if any of the fields fails to be defined(), then
# this def returns false
def good_list(check_fn, val_list)
val_list.each { |val| check_fn.call(val) }
end

def good_params(a_hash, fields)
return false if a_hash.nil?

Expand All @@ -29,30 +30,25 @@ def good_orderbook_level(level)

module Check # rubocop:disable Style/Documentation
def self.good_currency(currency)
good = good_params(currency, %w[
full_name payin_enabled payout_enabled transfer_enabled
precision_transfer networks
])
good = good_params(currency,
%w[full_name crypto payin_enabled payout_enabled transfer_enabled sign
crypto_payment_id_name crypto_explorer precision_transfer delisted networks])
return false unless good

currency['networks'].each { |level| return false unless good_network(level) }
true
end

def self.good_network(network)
good_params(network,
%w[
network default payin_enabled payout_enabled precision_payout
payout_fee payout_is_payment_id payin_payment_id payin_confirmations
])
good_params(network, %w[code network_name network protocol default is_ens_available payin_enabled payout_enabled
precision_payout payout_is_payment_id payin_payment_id payin_confirmations
is_multichain])
end

def self.good_symbol(symbol)
good_params(symbol,
%w[
type base_currency quote_currency status quantity_increment
tick_size take_rate make_rate fee_currency
])
%w[type base_currency quote_currency status quantity_increment tick_size take_rate make_rate
fee_currency])
end

def self.good_ticker(ticker)
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup
def test_not_authorized_exception
@client = Cryptomarket::Client.new api_key: 'not a key', api_secret: 'not a key'
begin
@client.get_spot_trading_balance
@client.get_spot_trading_balances
rescue Cryptomarket::APIException => e
assert_equal(e.code, 1_002)
end
Expand Down
32 changes: 16 additions & 16 deletions tests/rest/spot_trading.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test/unit'
require_relative '../../lib/cryptomarket/client'
require_relative '../../lib/cryptomarket/constants'
Expand All @@ -6,36 +8,32 @@

class TestRestTradingMethods < Test::Unit::TestCase
def setup
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
end

def test_get_spot_trading_balances
result = @client.get_spot_trading_balances
assert(good_list(->(val) do good_balance(val) end, result))
assert(good_list(->(val) { Check.good_balance(val) }, result))
end

def test_get_spot_trading_balance
result = @client.get_spot_trading_balance currency: 'USDT'
assert(good_balance(result))
assert(Check.good_balance(result))
end

def get_all_active_spot_orders
result = @client.getActiveOrders
result.each { |val| assert(good_order(val)) }
result.each { |val| assert(Check.good_order(val)) }
end

def test_spot_order_lifecycle
timestamp = Time.now.to_i.to_s
order = @client.create_spot_order(
symbol: 'EOSETH',
price: '10000',
quantity: '0.01',
side: 'sell',
client_order_id: timestamp
symbol: 'EOSETH', price: '10000', quantity: '0.01', side: 'sell', client_order_id: timestamp
)
assert(good_order(order))
assert(Check.good_order(order))
order = @client.get_active_spot_order client_order_id: timestamp
assert(good_order(order))
assert(Check.good_order(order))

new_client_order_id = Time.now.to_i.to_s + '1'
order = @client.replace_spot_order(
Expand All @@ -44,25 +42,25 @@ def test_spot_order_lifecycle
quantity: '0.02',
price: '999'
)
assert(good_order(order))
assert(Check.good_order(order))
order = @client.cancel_spot_order client_order_id: new_client_order_id
assert(good_order(order))
assert(Check.good_order(order))
assert(order['status'] == 'canceled')
end

def test_cancel_all_spot_orders
result = @client.cancel_all_spot_orders
assert(good_list(->(val) do good_order(val) end, result))
assert(good_list(->(val) { Check.good_order(val) }, result))
end

def test_get_all_trading_commission
result = @client.get_all_trading_commission
assert(good_list(->(val) do good_trading_commission(val) end, result))
assert(good_list(->(val) { Check.good_trading_commission(val) }, result))
end

def test_get_trading_commission
result = @client.get_trading_commission symbol: 'EOSETH'
assert(good_trading_commission(result))
assert(Check.good_trading_commission(result))
end

def test_create_order_list
Expand All @@ -85,5 +83,7 @@ def test_create_order_list
}
]
)
# TODO: check missing
end

end
8 changes: 5 additions & 3 deletions tests/rest/spot_trading_history.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# frozen_string_literal: true

require 'test/unit'
require_relative '../../lib/cryptomarket/client'
require_relative '../key_loader'
require_relative '../checks'

class TestRestTradingMethods < Test::Unit::TestCase
def setup
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
end

def test_get_spot_orders_history
result = @client.get_spot_orders_history limit: 12
result.each { |val| assert(good_order(val)) }
result.each { |val| assert(Check.good_order(val)) }
end

def test_get_spot_trades_history
result = @client.get_spot_trades_history symbol: 'EOSETH'
result.each { |val| assert(good_trade(val)) }
result.each { |val| assert(Check.good_trade(val)) }
end
end
8 changes: 8 additions & 0 deletions tests/rest/test_suite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'test/unit'
require_relative 'exceptions'
require_relative 'market_data'
require_relative 'spot_trading_history'
require_relative 'spot_trading'
require_relative 'wallet_management'
42 changes: 14 additions & 28 deletions tests/rest/wallet_management.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# frozen_string_literal: true

require 'test/unit'
require_relative '../key_loader'
require_relative '../checks'
require_relative '../../lib/cryptomarket/client'

class TestRestTradingMethods < Test::Unit::TestCase
class TestRestTradingMethods < Test::Unit::TestCase # rubocop:disable Metrics/ClassLength,Style/Documentation
def setup
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
end

def test_get_wallet_balances
result = @client.get_wallet_balances
assert(good_list(
->(balance) do good_balance(balance) end,
result
))
assert(good_list(->(balance) { Check.good_balance(balance) }, result))
end

def test_get_wallet_balance
Expand All @@ -23,45 +22,32 @@ def test_get_wallet_balance

def test_get_deposit_crypto_addresses
result = @client.get_deposit_crypto_addresses
assert(good_list(
->(address) do good_address(address) end,
result
))
assert(good_list(->(address) { Check.good_address(address) }, result))
end

def test_get_deposit_crypto_address
result = @client.get_deposit_crypto_address currency: 'ADA'
assert(good_address(result))
assert(Check.good_address(result))
end

def test_create_deposit_crypto_address
result = @client.create_deposit_crypto_address currency: 'ADA'
assert(good_address(result))
assert(Check.good_address(result))
end

def test_get_last_10_deposit_crypto_addresses
result = @client.get_last_10_deposit_crypto_addresses currency: 'ADA'
assert(good_list(
->(address) do good_address(address) end,
result
))
assert(good_list(->(address) { Check.good_address(address) }, result))
end

def test_get_last_10_withdrawal_crypto_addresses
result = @client.get_last_10_withdrawal_crypto_addresses currency: 'CLP'
assert(good_list(
->(address) do good_address(address) end,
result
))
assert(good_list(->(address) { Check.good_address(address) }, result))
end

def test_withdraw_crypto
ada_address = @client.get_deposit_crypto_address(currency: 'ADA')['address']
transaction_id = @client.withdraw_crypto(
currency: 'ADA',
amount: '0.1',
address: ada_address
)
transaction_id = @client.withdraw_crypto(currency: 'ADA', amount: '0.1', address: ada_address)
assert(!transaction_id.empty?)
end

Expand Down Expand Up @@ -105,7 +91,7 @@ def test_crypto_address_belongs_to_current_account
assert(it_belongs)
end

def test_transfer_between_wallet_and_exchange
def test_transfer_between_wallet_and_exchange # rubocop:disable Metrics/MethodLength
result = @client.transfer_between_wallet_and_exchange(
currency: 'CRO',
amount: '0.1',
Expand All @@ -125,7 +111,7 @@ def test_transfer_between_wallet_and_exchange
def test_get_transaction_history
result = @client.get_transaction_history
assert(good_list(
->(transaction) do good_transaction(transaction) end,
->(transaction) do Check.good_transaction(transaction) end,
result
))
end
Expand All @@ -134,7 +120,7 @@ def test_get_transaction
transaction_list = @client.get_transaction_history
first_transaction_id = transaction_list[0]['native']['tx_id']
result = @client.get_transaction id: first_transaction_id
assert(good_transaction(result))
assert(Check.good_transaction(result))
end

def test_offchain_available
Expand Down
5 changes: 5 additions & 0 deletions tests/test_suite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require 'test/unit'
require_relative 'rest/test_suite'
require_relative 'websocket/test_suite'
4 changes: 2 additions & 2 deletions tests/websocket/client_lifetime.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'test/unit'
require_relative '../../lib/cryptomarket/websocket/market_data_client'
require_relative '../key_loader'
require_relative '../checks'
require_relative 'sequence_flow'
require_relative 'time_flow'

class TestWSClientLifetime < Test::Unit::TestCase
@@SECOND = 1
Expand Down
29 changes: 0 additions & 29 deletions tests/websocket/keep_alive.rb

This file was deleted.

Loading

0 comments on commit 0387fc4

Please sign in to comment.