Skip to content

Commit 0387fc4

Browse files
committed
fix:httpmanager
1 parent 6c41c1c commit 0387fc4

File tree

12 files changed

+99
-100
lines changed

12 files changed

+99
-100
lines changed

lib/cryptomarket/http_manager.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
require_relative 'exceptions'
1010
require_relative 'credentials_factory'
1111

12+
def post?(method)
13+
method.upcase == 'POST'
14+
end
15+
16+
def get?(method)
17+
method.upcase == 'GET'
18+
end
19+
20+
def put?(method)
21+
method.upcase == 'PUT'
22+
end
23+
24+
def patch?(method)
25+
method.upcase == 'PATCH'
26+
end
27+
1228
module Cryptomarket
1329
# Manager of http requests to the cryptomarket server
1430
class HttpManager
@@ -24,7 +40,7 @@ def initialize(api_key:, api_secret:, window: nil)
2440
def make_request(method:, endpoint:, params: nil, public: false)
2541
uri = URI(@@API_URL + @@API_VERSION + endpoint)
2642
payload = build_payload(params)
27-
headers = build_headers(method, endpoint, params, public)
43+
headers = build_headers(method, endpoint, payload, public)
2844
if ((method.upcase == 'GET') || (method.upcase == 'PUT')) && !payload.nil?
2945
uri.query = URI.encode_www_form payload
3046
payload = nil
@@ -48,14 +64,16 @@ def build_payload(params)
4864
return nil if params.nil?
4965

5066
payload = params.compact
51-
payload = Hash[params.sort_by { |key, _val| key.to_s }] if params.is_a?(Hash)
67+
payload = Hash[payload.sort_by { |key, _val| key.to_s }] if payload.is_a?(Hash)
5268
payload
5369
end
5470

5571
def do_request(method, uri, payload, headers)
56-
response = RestClient::Request.execute(
57-
method: method.downcase.to_sym, url: uri.to_s, payload: payload.to_json, headers: headers
58-
)
72+
args = { method: method.downcase.to_sym, url: uri.to_s, headers: headers }
73+
if post?(method) || patch?(method)
74+
args[:payload] = post?(method) ? payload.to_json : payload
75+
end
76+
response = RestClient::Request.execute(**args)
5977
handle_response(response)
6078
rescue RestClient::ExceptionWithResponse => e
6179
handle_response(e.response)

tests/checker_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ def gen_check_notification_hash_list_callback(check_fn, veredict_checker)
9898
def gen_check_notification_hash_callback(check_fn, veredict_checker)
9999
->(notification, _notification_type) { check_hash(notification, check_fn, veredict_checker) }
100100
end
101+
102+
def gen_check_notification_list_w_n_type_callback(check_fn, veredict_checker)
103+
->(notification, _notification_type) { check_list(notification, check_fn, veredict_checker) }
104+
end

tests/checks.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ def defined(a_hash, key)
1313
true
1414
end
1515

16-
# good_params checks all of the values in the fields list to be present in the dict, and if they are
17-
# present, check the defined() condition to be true. if any of the fields fails to be defined(), then
18-
# this def returns false
16+
def good_list(check_fn, val_list)
17+
val_list.each { |val| check_fn.call(val) }
18+
end
19+
1920
def good_params(a_hash, fields)
2021
return false if a_hash.nil?
2122

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

3031
module Check # rubocop:disable Style/Documentation
3132
def self.good_currency(currency)
32-
good = good_params(currency, %w[
33-
full_name payin_enabled payout_enabled transfer_enabled
34-
precision_transfer networks
35-
])
33+
good = good_params(currency,
34+
%w[full_name crypto payin_enabled payout_enabled transfer_enabled sign
35+
crypto_payment_id_name crypto_explorer precision_transfer delisted networks])
3636
return false unless good
3737

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

4242
def self.good_network(network)
43-
good_params(network,
44-
%w[
45-
network default payin_enabled payout_enabled precision_payout
46-
payout_fee payout_is_payment_id payin_payment_id payin_confirmations
47-
])
43+
good_params(network, %w[code network_name network protocol default is_ens_available payin_enabled payout_enabled
44+
precision_payout payout_is_payment_id payin_payment_id payin_confirmations
45+
is_multichain])
4846
end
4947

5048
def self.good_symbol(symbol)
5149
good_params(symbol,
52-
%w[
53-
type base_currency quote_currency status quantity_increment
54-
tick_size take_rate make_rate fee_currency
55-
])
50+
%w[type base_currency quote_currency status quantity_increment tick_size take_rate make_rate
51+
fee_currency])
5652
end
5753

5854
def self.good_ticker(ticker)

tests/rest/exceptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup
1313
def test_not_authorized_exception
1414
@client = Cryptomarket::Client.new api_key: 'not a key', api_secret: 'not a key'
1515
begin
16-
@client.get_spot_trading_balance
16+
@client.get_spot_trading_balances
1717
rescue Cryptomarket::APIException => e
1818
assert_equal(e.code, 1_002)
1919
end

tests/rest/spot_trading.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'test/unit'
24
require_relative '../../lib/cryptomarket/client'
35
require_relative '../../lib/cryptomarket/constants'
@@ -6,36 +8,32 @@
68

79
class TestRestTradingMethods < Test::Unit::TestCase
810
def setup
9-
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
11+
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
1012
end
1113

1214
def test_get_spot_trading_balances
1315
result = @client.get_spot_trading_balances
14-
assert(good_list(->(val) do good_balance(val) end, result))
16+
assert(good_list(->(val) { Check.good_balance(val) }, result))
1517
end
1618

1719
def test_get_spot_trading_balance
1820
result = @client.get_spot_trading_balance currency: 'USDT'
19-
assert(good_balance(result))
21+
assert(Check.good_balance(result))
2022
end
2123

2224
def get_all_active_spot_orders
2325
result = @client.getActiveOrders
24-
result.each { |val| assert(good_order(val)) }
26+
result.each { |val| assert(Check.good_order(val)) }
2527
end
2628

2729
def test_spot_order_lifecycle
2830
timestamp = Time.now.to_i.to_s
2931
order = @client.create_spot_order(
30-
symbol: 'EOSETH',
31-
price: '10000',
32-
quantity: '0.01',
33-
side: 'sell',
34-
client_order_id: timestamp
32+
symbol: 'EOSETH', price: '10000', quantity: '0.01', side: 'sell', client_order_id: timestamp
3533
)
36-
assert(good_order(order))
34+
assert(Check.good_order(order))
3735
order = @client.get_active_spot_order client_order_id: timestamp
38-
assert(good_order(order))
36+
assert(Check.good_order(order))
3937

4038
new_client_order_id = Time.now.to_i.to_s + '1'
4139
order = @client.replace_spot_order(
@@ -44,25 +42,25 @@ def test_spot_order_lifecycle
4442
quantity: '0.02',
4543
price: '999'
4644
)
47-
assert(good_order(order))
45+
assert(Check.good_order(order))
4846
order = @client.cancel_spot_order client_order_id: new_client_order_id
49-
assert(good_order(order))
47+
assert(Check.good_order(order))
5048
assert(order['status'] == 'canceled')
5149
end
5250

5351
def test_cancel_all_spot_orders
5452
result = @client.cancel_all_spot_orders
55-
assert(good_list(->(val) do good_order(val) end, result))
53+
assert(good_list(->(val) { Check.good_order(val) }, result))
5654
end
5755

5856
def test_get_all_trading_commission
5957
result = @client.get_all_trading_commission
60-
assert(good_list(->(val) do good_trading_commission(val) end, result))
58+
assert(good_list(->(val) { Check.good_trading_commission(val) }, result))
6159
end
6260

6361
def test_get_trading_commission
6462
result = @client.get_trading_commission symbol: 'EOSETH'
65-
assert(good_trading_commission(result))
63+
assert(Check.good_trading_commission(result))
6664
end
6765

6866
def test_create_order_list
@@ -85,5 +83,7 @@ def test_create_order_list
8583
}
8684
]
8785
)
86+
# TODO: check missing
8887
end
88+
8989
end

tests/rest/spot_trading_history.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
# frozen_string_literal: true
2+
13
require 'test/unit'
24
require_relative '../../lib/cryptomarket/client'
35
require_relative '../key_loader'
46
require_relative '../checks'
57

68
class TestRestTradingMethods < Test::Unit::TestCase
79
def setup
8-
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
10+
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
911
end
1012

1113
def test_get_spot_orders_history
1214
result = @client.get_spot_orders_history limit: 12
13-
result.each { |val| assert(good_order(val)) }
15+
result.each { |val| assert(Check.good_order(val)) }
1416
end
1517

1618
def test_get_spot_trades_history
1719
result = @client.get_spot_trades_history symbol: 'EOSETH'
18-
result.each { |val| assert(good_trade(val)) }
20+
result.each { |val| assert(Check.good_trade(val)) }
1921
end
2022
end

tests/rest/test_suite.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
require 'test/unit'
4+
require_relative 'exceptions'
5+
require_relative 'market_data'
6+
require_relative 'spot_trading_history'
7+
require_relative 'spot_trading'
8+
require_relative 'wallet_management'

tests/rest/wallet_management.rb

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
# frozen_string_literal: true
2+
13
require 'test/unit'
24
require_relative '../key_loader'
35
require_relative '../checks'
46
require_relative '../../lib/cryptomarket/client'
57

6-
class TestRestTradingMethods < Test::Unit::TestCase
8+
class TestRestTradingMethods < Test::Unit::TestCase # rubocop:disable Metrics/ClassLength,Style/Documentation
79
def setup
8-
@client = Cryptomarket::Client.new api_key: Keyloader.api_key, api_secret: Keyloader.api_secret
10+
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
911
end
1012

1113
def test_get_wallet_balances
1214
result = @client.get_wallet_balances
13-
assert(good_list(
14-
->(balance) do good_balance(balance) end,
15-
result
16-
))
15+
assert(good_list(->(balance) { Check.good_balance(balance) }, result))
1716
end
1817

1918
def test_get_wallet_balance
@@ -23,45 +22,32 @@ def test_get_wallet_balance
2322

2423
def test_get_deposit_crypto_addresses
2524
result = @client.get_deposit_crypto_addresses
26-
assert(good_list(
27-
->(address) do good_address(address) end,
28-
result
29-
))
25+
assert(good_list(->(address) { Check.good_address(address) }, result))
3026
end
3127

3228
def test_get_deposit_crypto_address
3329
result = @client.get_deposit_crypto_address currency: 'ADA'
34-
assert(good_address(result))
30+
assert(Check.good_address(result))
3531
end
3632

3733
def test_create_deposit_crypto_address
3834
result = @client.create_deposit_crypto_address currency: 'ADA'
39-
assert(good_address(result))
35+
assert(Check.good_address(result))
4036
end
4137

4238
def test_get_last_10_deposit_crypto_addresses
4339
result = @client.get_last_10_deposit_crypto_addresses currency: 'ADA'
44-
assert(good_list(
45-
->(address) do good_address(address) end,
46-
result
47-
))
40+
assert(good_list(->(address) { Check.good_address(address) }, result))
4841
end
4942

5043
def test_get_last_10_withdrawal_crypto_addresses
5144
result = @client.get_last_10_withdrawal_crypto_addresses currency: 'CLP'
52-
assert(good_list(
53-
->(address) do good_address(address) end,
54-
result
55-
))
45+
assert(good_list(->(address) { Check.good_address(address) }, result))
5646
end
5747

5848
def test_withdraw_crypto
5949
ada_address = @client.get_deposit_crypto_address(currency: 'ADA')['address']
60-
transaction_id = @client.withdraw_crypto(
61-
currency: 'ADA',
62-
amount: '0.1',
63-
address: ada_address
64-
)
50+
transaction_id = @client.withdraw_crypto(currency: 'ADA', amount: '0.1', address: ada_address)
6551
assert(!transaction_id.empty?)
6652
end
6753

@@ -105,7 +91,7 @@ def test_crypto_address_belongs_to_current_account
10591
assert(it_belongs)
10692
end
10793

108-
def test_transfer_between_wallet_and_exchange
94+
def test_transfer_between_wallet_and_exchange # rubocop:disable Metrics/MethodLength
10995
result = @client.transfer_between_wallet_and_exchange(
11096
currency: 'CRO',
11197
amount: '0.1',
@@ -125,7 +111,7 @@ def test_transfer_between_wallet_and_exchange
125111
def test_get_transaction_history
126112
result = @client.get_transaction_history
127113
assert(good_list(
128-
->(transaction) do good_transaction(transaction) end,
114+
->(transaction) do Check.good_transaction(transaction) end,
129115
result
130116
))
131117
end
@@ -134,7 +120,7 @@ def test_get_transaction
134120
transaction_list = @client.get_transaction_history
135121
first_transaction_id = transaction_list[0]['native']['tx_id']
136122
result = @client.get_transaction id: first_transaction_id
137-
assert(good_transaction(result))
123+
assert(Check.good_transaction(result))
138124
end
139125

140126
def test_offchain_available

tests/test_suite.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
require 'test/unit'
4+
require_relative 'rest/test_suite'
5+
require_relative 'websocket/test_suite'

tests/websocket/client_lifetime.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
# frozen_string_literal: true
2+
13
require 'test/unit'
24
require_relative '../../lib/cryptomarket/websocket/market_data_client'
35
require_relative '../key_loader'
46
require_relative '../checks'
5-
require_relative 'sequence_flow'
6-
require_relative 'time_flow'
77

88
class TestWSClientLifetime < Test::Unit::TestCase
99
@@SECOND = 1

0 commit comments

Comments
 (0)