Skip to content

Commit

Permalink
Refine GetAccounts and Add two new API for API update in April 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed May 13, 2020
1 parent 9df3399 commit 4466bc4
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 52 deletions.
9 changes: 1 addition & 8 deletions example/restful/getaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@
from huobi.constant.test import *
from huobi.base.printobject import *
from huobi.model import Account

request_client = RequestClient(api_key=g_api_key, secret_key=g_secret_key)
account_balance_list = request_client.get_account_balance()
account_balance_list = request_client.get_accounts()
if account_balance_list and len(account_balance_list):
for account in account_balance_list:
print("======= ID", account.id, "=======")
print("Account Status", account.account_state)
print("Account Type", account.account_type)
print("Subtype", account.subtype)
if account.balances and len(account.balances):
for balance in account.balances:
print("\tBalance Currency", balance.currency)
print("\tBalance Type", balance.balance_type)
print("\tBalance", balance.balance)
print()
print()


20 changes: 20 additions & 0 deletions example/restful/getaccountbalance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from huobi import RequestClient
from huobi.constant.test import *
from huobi.base.printobject import *
from huobi.model import Account
request_client = RequestClient(api_key=g_api_key, secret_key=g_secret_key)
account_balance_list = request_client.get_account_balance()
if account_balance_list and len(account_balance_list):
for account in account_balance_list:
print("======= ID", account.id, "=======")
print("Account Status", account.account_state)
print("Account Type", account.account_type)
print("Subtype", account.subtype)
if account.balances and len(account.balances):
for balance in account.balances:
print("\tBalance Currency", balance.currency)
print("\tBalance Type", balance.balance_type)
print("\tBalance", balance.balance)
print()
print()

13 changes: 13 additions & 0 deletions example/restful/getsubuserdepositaddress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from huobi import RequestClient
from huobi.constant.test import *


def print_obj_list(list_obj):
if list_obj and len(list_obj):
for obj in list_obj:
obj.print_object()
print()

client = RequestClient(api_key=g_api_key, secret_key=g_secret_key)
list_obj = client.get_sub_user_deposit_address(sub_uid=g_sub_uid, currency="btc")
print_obj_list(list_obj)
14 changes: 14 additions & 0 deletions example/restful/getsubuserdeposithistory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from huobi import RequestClient
from huobi.constant.test import *


def print(deposit_history):
if deposit_history.data and len(deposit_history.data):
print(deposit_history.nextId)
for obj in deposit_history.data:
obj.print_object()
print()

client = RequestClient(api_key=g_api_key, secret_key=g_secret_key)
deposit_history = client.get_sub_user_deposit_history(sub_uid=g_sub_uid)
print(deposit_history)
32 changes: 32 additions & 0 deletions huobi/impl/accountinfomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
class AccountInfoMap:

user_map = dict()
account_id_type_map = dict()
account_type_id_map = dict()

def update_user_info(self, api_key, request_impl):
accounts = call_sync(request_impl.get_accounts())
user = User()
user.accounts = accounts
self.user_map[api_key] = user
if accounts and len(accounts):
self.account_id_type_map[api_key] = {}
self.account_type_id_map[api_key] = {}
for account_item in accounts:
self.account_id_type_map[api_key][account_item.id] = account_item.account_type
self.account_type_id_map[api_key][account_item.account_type] = account_item.id

def get_user(self, api_key):
if api_key is None or api_key == "":
Expand All @@ -29,5 +37,29 @@ def get_account_by_id(self, api_key, account_id):
api_key + ", account id: " + str(account_id))
return account

def get_all_accounts(self, api_key):
user = self.get_user(api_key)
return user.accounts

def get_account_type_by_id(self, api_key, account_id):
if api_key is None or api_key == "":
raise HuobiApiException(HuobiApiException.KEY_MISSING, "[User] Key is empty or null")
if api_key not in self.account_id_type_map:
raise HuobiApiException(HuobiApiException.RUNTIME_ERROR, "[User] Cannot found account_id by key: " + api_key)
return self.account_id_type_map.get(api_key, {}).get(account_id, None)

def get_account_id_by_type(self, api_key, account_type):
if api_key is None or api_key == "":
raise HuobiApiException(HuobiApiException.KEY_MISSING, "[User] Key is empty or null")
if api_key not in self.account_type_id_map:
raise HuobiApiException(HuobiApiException.RUNTIME_ERROR, "[User] Cannot found account_type by key: " + api_key)
return self.account_type_id_map.get(api_key, {}).get(account_type, None)

def get_all_accounts_without_check(self, api_key):
if api_key is None or api_key == "":
raise HuobiApiException(HuobiApiException.KEY_MISSING, "[User] Key is empty or null")

user = self.user_map.get(api_key, None)
return None if (user is None) else user.accounts

account_info_map = AccountInfoMap()
13 changes: 8 additions & 5 deletions huobi/impl/restapiinvoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from huobi.impl.utils.etfresult import etf_result_check
from huobi.impl.utils import *

session = requests.Session()

def check_response(json_wrapper):
if json_wrapper.contain_key("status"):
Expand All @@ -27,23 +28,24 @@ def check_response(json_wrapper):
elif json_wrapper.contain_key("code"):
code = json_wrapper.get_int("code")
if code != 200:
raise HuobiApiException(HuobiApiException.EXEC_ERROR, "[Executing] " + str(code))
err_msg = json_wrapper.get_string_or_default("message", "")
raise HuobiApiException(HuobiApiException.EXEC_ERROR, "[Executing] " + str(code) + ": " + err_msg)
else:
raise HuobiApiException(HuobiApiException.RUNTIME_ERROR, "[Invoking] Status cannot be found in response.")


def call_sync(request, is_checked=False):
if request.method == "GET":
# print("call_sync url : " , request.host + request.url)
response = requests.get(request.host + request.url, headers=request.header)
response = session.get(request.host + request.url, headers=request.header)
# print("receive data : " + response.text)
if is_checked is True:
return response.text
json_wrapper = parse_json_from_string(response.text)
check_response(json_wrapper)
return request.json_parser(json_wrapper)
elif request.method == "POST":
response = requests.post(request.host + request.url, data=json.dumps(request.post_body), headers=request.header)
response = session.post(request.host + request.url, data=json.dumps(request.post_body), headers=request.header)
# print("receive data : " + response.text)
json_wrapper = parse_json_from_string(response.text)
check_response(json_wrapper)
Expand All @@ -52,7 +54,8 @@ def call_sync(request, is_checked=False):
def call_sync_perforence_test(request, is_checked=False):
if request.method == "GET":
inner_start_time = time.time()
response = requests.get(request.host + request.url, headers=request.header)
# print("call_sync_perforence_test url : ", request.host + request.url)
response = session.get(request.host + request.url, headers=request.header)
#print("call_sync_perforence_test data :", response.text)
inner_end_time = time.time()
cost_manual = round(inner_end_time - inner_start_time, 6)
Expand All @@ -64,7 +67,7 @@ def call_sync_perforence_test(request, is_checked=False):
return request.json_parser(json_wrapper), req_cost, cost_manual
elif request.method == "POST":
inner_start_time = time.time()
response = requests.post(request.host + request.url, data=json.dumps(request.post_body), headers=request.header)
response = session.post(request.host + request.url, data=json.dumps(request.post_body), headers=request.header)
inner_end_time = time.time()
cost_manual = round(inner_end_time - inner_start_time, 6)
req_cost = response.elapsed.total_seconds()
Expand Down
56 changes: 56 additions & 0 deletions huobi/impl/restapirequestimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from huobi.impl.utils.timeservice import *
from huobi.model import *
from huobi.model.accountledger import AccountLedger
from huobi.model.deposithistory import DepositHistory, History
from huobi.model.feerate import FeeRate
from huobi.model.marketticker import MarketTicker

Expand Down Expand Up @@ -329,6 +330,41 @@ def parse(json_wrapper):
request.json_parser = parse
return request

def get_sub_user_deposit_history(self, sub_uid, currency=None, start_time=None, end_time=None, sort=None, limit=None, from_id=None):
check_should_not_none(sub_uid, "sub_uid")

builder = UrlParamsBuilder()
builder.put_url("subUid", sub_uid)
builder.put_url("currency", currency)
builder.put_url("startTime", start_time)
builder.put_url("endTime", end_time)
builder.put_url("sort", sort)
builder.put_url("limit", limit)
builder.put_url("fromId", from_id)
request = self.__create_request_by_get_with_signature("/v2/sub-user/query-deposit", builder)

def parse(json_wrapper):
deposit_history = DepositHistory()
deposit_history.nextId = json_wrapper.get_int_or_default("nextId", 0)
deposit_history.data = list()
list_array = json_wrapper.get_array("data")
for item in list_array.get_items():
history = History()
history.id = item.get_int("id")
history.currency = item.get_string("currency")
history.txHash = item.get_string("txHash")
history.amount = item.get_float("amount")
history.address = item.get_string("address")
history.addressTag = item.get_string("addressTag")
history.deposit_state = item.get_string("state")
history.created_timestamp = item.get_int("createTime")
history.updated_timestamp = item.get_int("updateTime")
deposit_history.data.append(history)
return deposit_history

request.json_parser = parse
return request

def get_balance(self, account):
path = "/v1/account/accounts/{}/balance"
path = path.format(account.id)
Expand Down Expand Up @@ -1322,6 +1358,26 @@ def parse(json_wrapper):
request.json_parser = parse
return request

def get_sub_user_deposit_address(self, sub_uid, currency):
builder = UrlParamsBuilder()
builder.put_url("subUid", sub_uid)
builder.put_url("currency", currency)
request = self.__create_request_by_get_with_signature("/v2/sub-user/deposit-address", builder)

def parse(json_wrapper):
ret_list = []
data_array = json_wrapper.get_array("data")
for address_data in data_array.get_items():
obj = ChainDepositAddress()
obj.currency = address_data.get_string("currency")
obj.address = address_data.get_string("address")
obj.addressTag = address_data.get_string("addressTag")
obj.chain = address_data.get_string("chain")
ret_list.append(obj)
return ret_list

request.json_parser = parse
return request

def get_account_withdraw_quota(self, currency):
check_should_not_none(currency, "currency")
Expand Down
18 changes: 17 additions & 1 deletion huobi/model/balance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from huobi.model.constant import *


Expand Down Expand Up @@ -29,4 +31,18 @@ def print_object(self, format_data=""):
from huobi.base.printobject import PrintBasic
PrintBasic.print_basic(self.currency, format_data + "Currency")
PrintBasic.print_basic(self.balance_type, format_data + "Balance Type")
PrintBasic.print_basic(self.balance, format_data + "Balance")
PrintBasic.print_basic(self.balance, format_data + "Balance")

@staticmethod
def parse_from_api_response(dict_data):
response_status = dict_data.get("status", None)
balance_data_list = dict_data.get("data", {}).get("list", {})
balance_list = []
if ((response_status == "ok") and (balance_data_list is not None) and len(balance_data_list)):
for item in balance_data_list:
balance = Balance()
balance.balance = item.get("balance")
balance.currency = item.get("currency")
balance.balance_type = item.get("type")
balance_list.append(balance)
return balance_list
50 changes: 50 additions & 0 deletions huobi/model/deposithistory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from huobi.model.constant import *


class DepositHistory:
"""
The deposit history
:member
id: The transfer id.
currency: The crypto currency to deposit.
txHash: The on-chain transaction hash.
amount: The number of crypto asset transferred in its minimum unit.
address: The deposit source address.
addressTag: The user defined address tag.
deposit_state: The deposit state of this transfer.
created_timestamp: The UNIX formatted timestamp in UTC for the transfer creation.
updated_timestamp: The UNIX formatted timestamp in UTC for the transfer's latest update.
"""

def __init__(self):
self.data = list()
self.nextId = 0


class History:

def __init__(self):
self.id = 0
self.currency = ""
self.txHash = ""
self.chain = ""
self.amount = 0.0
self.address = ""
self.addressTag = ""
self.deposit_state = WithdrawState.INVALID
self.created_timestamp = 0
self.updated_timestamp = 0

def print_object(self, format_data=""):
from huobi.base.printobject import PrintBasic
PrintBasic.print_basic(self.id, format_data + "ID")
PrintBasic.print_basic(self.currency, format_data + "Currency")
PrintBasic.print_basic(self.chain, format_data + "Chain")
PrintBasic.print_basic(self.txHash, format_data + "Trade Hash")
PrintBasic.print_basic(self.amount, format_data + "Amount")
PrintBasic.print_basic(self.address, format_data + "Address")
PrintBasic.print_basic(self.addressTag, format_data + "Address Tag")
PrintBasic.print_basic(self.deposit_state, format_data + "Deposit State")
PrintBasic.print_basic(self.created_timestamp, format_data + "Create Time")
PrintBasic.print_basic(self.updated_timestamp, format_data + "Update Time")
Loading

0 comments on commit 4466bc4

Please sign in to comment.