diff --git a/lib/cryptomarket/http_manager.rb b/lib/cryptomarket/http_manager.rb index cabcd78..46a0bb3 100644 --- a/lib/cryptomarket/http_manager.rb +++ b/lib/cryptomarket/http_manager.rb @@ -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 @@ -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 @@ -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) diff --git a/tests/checker_generator.rb b/tests/checker_generator.rb index 1fd19df..bf277e3 100644 --- a/tests/checker_generator.rb +++ b/tests/checker_generator.rb @@ -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 diff --git a/tests/checks.rb b/tests/checks.rb index 0cf25f3..7c56d15 100644 --- a/tests/checks.rb +++ b/tests/checks.rb @@ -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? @@ -29,10 +30,9 @@ 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) } @@ -40,19 +40,15 @@ def self.good_currency(currency) 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) diff --git a/tests/rest/exceptions.rb b/tests/rest/exceptions.rb index d201fa5..84f3e72 100644 --- a/tests/rest/exceptions.rb +++ b/tests/rest/exceptions.rb @@ -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 diff --git a/tests/rest/spot_trading.rb b/tests/rest/spot_trading.rb index e8b9afd..e90cc07 100644 --- a/tests/rest/spot_trading.rb +++ b/tests/rest/spot_trading.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test/unit' require_relative '../../lib/cryptomarket/client' require_relative '../../lib/cryptomarket/constants' @@ -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( @@ -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 @@ -85,5 +83,7 @@ def test_create_order_list } ] ) + # TODO: check missing end + end diff --git a/tests/rest/spot_trading_history.rb b/tests/rest/spot_trading_history.rb index 8fee69d..9e0742b 100644 --- a/tests/rest/spot_trading_history.rb +++ b/tests/rest/spot_trading_history.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test/unit' require_relative '../../lib/cryptomarket/client' require_relative '../key_loader' @@ -5,16 +7,16 @@ 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 diff --git a/tests/rest/test_suite.rb b/tests/rest/test_suite.rb new file mode 100644 index 0000000..87a419b --- /dev/null +++ b/tests/rest/test_suite.rb @@ -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' diff --git a/tests/rest/wallet_management.rb b/tests/rest/wallet_management.rb index d728162..71e7d84 100644 --- a/tests/rest/wallet_management.rb +++ b/tests/rest/wallet_management.rb @@ -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 @@ -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 @@ -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', @@ -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 @@ -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 diff --git a/tests/test_suite.rb b/tests/test_suite.rb new file mode 100644 index 0000000..cd361fb --- /dev/null +++ b/tests/test_suite.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +require 'test/unit' +require_relative 'rest/test_suite' +require_relative 'websocket/test_suite' diff --git a/tests/websocket/client_lifetime.rb b/tests/websocket/client_lifetime.rb index b7765a2..f632251 100644 --- a/tests/websocket/client_lifetime.rb +++ b/tests/websocket/client_lifetime.rb @@ -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 diff --git a/tests/websocket/keep_alive.rb b/tests/websocket/keep_alive.rb deleted file mode 100644 index 13b8745..0000000 --- a/tests/websocket/keep_alive.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require 'test/unit' -require_relative '../../lib/cryptomarket/websocket/market_data_client' -require_relative '../key_loader' - -class TestWStrading < Test::Unit::TestCase - def setup - @wsclient = Cryptomarket::Websocket::MarketDataClient.new - @wsclient.connect - sleep(3) - end - - @@result_callback = proc { |error, result| - if !error.nil? - puts error - else - puts result - end - } - @@feed_callback = proc { |_feed| - puts "feed: #{Time.now}" - } - - def test_keep_socket_alive - @wsclient.subscribe_to_ticker callback: @@feed_callback, result_callback: @@result_callback, speed: '1s' - sleep(3 * 60 * 60) - end -end diff --git a/tests/websocket/test_suite.rb b/tests/websocket/test_suite.rb new file mode 100644 index 0000000..7eafc7b --- /dev/null +++ b/tests/websocket/test_suite.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'test/unit' +require_relative 'client_lifetime' +require_relative 'market_data_client' +require_relative 'trading_client_subscriptions' +require_relative 'trading_client' +require_relative 'wallet_client_subscriptions' +require_relative 'wallet_client'