def request_api(method, url_path, param): # 把请求参数进行签名,获得含有签名的完整请求URL url = api_signature(method, param, url_path) # 请求循环10次,尝试次数为10次 flag = True a = 0 while (flag): # 每次循环+1 到10次时退出死循环 a += 1 if a == request_retry_count: flag = False try: if method == 'GET': r = requests.get(url, timeout=request_timeout) if method == 'POST': r = requests.post(url, data=json.dumps(param), headers=headers, timeout=request_timeout) log_print.info('request api success, result: %s ' % r.text) # 判断是否发送成功,如不成功则抛出异常 request_dict = r.json() request_status = request_dict['status'] if request_status != 'ok': raise Exception('request huobi api error') # 消息成功发送,停止死循环 flag = False return request_dict except Exception as e: log_print.exception(e)
def get_api_user_info(): log_print.info('request_api: get_api_user_info(获取账号信息)') # 请求请求方式 method = 'GET' # 接口url_path url_path = '/v1/account/accounts' # 定义接口请求参数 param = {} # 请求接口 result_dict = request_api(method, url_path, param) #print(result_dict) # 返回结果 return result_dict
def post_api_order_cancel(order_id): log_print.info('request_api: post_api_order_cancel(撤销订单)') # 请求请求方式 method = 'POST' # 接口请求URL url_path = '/v1/order/orders/%s/submitcancel' % (order_id) param = { 'order-id': order_id, } # 请求接口 result_dict = request_api(method, url_path, param) #print(result_dict) # 返回结果 return result_dict
def get_api_order_deal_detail(order_id): log_print.info('request_api: get_api_order_deal_detail(获取订单成交明细)') # 请求请求方式 method = 'GET' # 接口请求URL url_path = '/v1/order/orders/%s/matchresults' % (order_id) # 定义请求参数 param = { 'order-id': order_id, } # 请求接口 result_dict = request_api(method, url_path, param) #print(result_dict) # 返回结果 return result_dict
def get_api_user_balance(): log_print.info('request_api: get_api_user_balance(获取账余额)') # 请求请求方式 method = 'GET' # 接口请求URL url_path = '/v1/account/accounts/%s/balance' % (huobi_sub_account_id) # 定义请求参数 param = { 'account-id': huobi_sub_account_id, } # 请求接口 result_dict = request_api(method, url_path, param) #print(result_dict) # 返回结果 return result_dict
def get_api_ticker(): # 循环10次,尝试次数为10次 flag = True a = 0 while (flag): # 每次循环+1 到10次时退出死循环 a += 1 if a == request_retry_count: flag = False try: url = 'https://api.huobi.pro/market/detail/merged?symbol=%s' % ( huobi_symbol) r = requests.get(url, timeout=request_timeout) log_print.info('get ticker success %s ' % r.text) # 判断是否发送成功,如不成功则抛出异常 request_dict = r.json() request_status = request_dict['status'] if request_status != 'ok': raise Exception('get huobi api error') # 消息成功发送,停止死循环 flag = False return request_dict except Exception as e: log_print.exception(e)
def post_api_order_place(deal_type, deal_amount): log_print.info('request_api: post_api_order_place(下单)') # 请求请求方式 method = 'POST' # 接口请求URL url_path = '/v1/order/orders/place' param = { 'account-id': huobi_sub_account_id, 'symbol': huobi_symbol, 'type': deal_type, 'amount': deal_amount, #'price' : 100.00, 'source': 'api', } # 请求接口 result_dict = request_api(method, url_path, param) #print(result_dict) # 返回结果 return result_dict