Example #1
0
def check_symbol(symbol):
    if not isinstance(symbol, str):
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] symbol must be string")
    if re.match(reg_ex, symbol):
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] " + symbol + "  is invalid symbol")
Example #2
0
def check_response(json_wrapper):
    if json_wrapper.contain_key("status"):
        status = json_wrapper.get_string("status")
        if status == "error":
            err_code = json_wrapper.get_string("err_code")
            err_msg = json_wrapper.get_string("err_msg")
            raise HuobiApiException(HuobiApiException.EXEC_ERROR,
                                    "[Executing] " + err_code + ": " + err_msg)
        elif status != "ok":
            raise HuobiApiException(
                HuobiApiException.RUNTIME_ERROR,
                "[Invoking] Response is not expected: " + status)
    elif json_wrapper.contain_key("success"):
        success = json_wrapper.get_boolean("success")
        if success is False:
            err_code = etf_result_check(json_wrapper.get_int("code"))
            err_msg = json_wrapper.get_string("message")
            if err_code == "":
                raise HuobiApiException(HuobiApiException.EXEC_ERROR,
                                        "[Executing] " + err_msg)
            else:
                raise HuobiApiException(
                    HuobiApiException.EXEC_ERROR,
                    "[Executing] " + err_code + ": " + err_msg)
    else:
        raise HuobiApiException(
            HuobiApiException.RUNTIME_ERROR,
            "[Invoking] Status cannot be found in response.")
Example #3
0
def check_currency(currency):
    if not isinstance(currency, str):
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] currency must be string")
    if re.match(reg_ex, currency) is not None:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] " + currency + "  is invalid currency")
Example #4
0
 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)
Example #5
0
 def get_user(self, api_key):
     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.user_map:
         raise HuobiApiException(
             HuobiApiException.RUNTIME_ERROR,
             "[User] Cannot found user by key: " + api_key)
     return self.user_map[api_key]
Example #6
0
def check_list(list_value, min_value, max_value, name):
    if list_value is None:
        return
    if len(list_value) > max_value:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR, "[Input] " + name +
            " is out of bound, the max size is " + str(max_value))
    if len(list_value) < min_value:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR, "[Input] " + name +
            " should contain " + str(min_value) + " item(s) at least")
Example #7
0
def format_date(value, name):
    if value is None:
        return None
    if not isinstance(value, str):
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] " + name + " must be string")
    try:
        new_time = time.strptime(value, "%Y-%m-%d")
        return time.strftime("%Y-%m-%d", new_time)
    except:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] " + name + " is not invalid date format")
Example #8
0
def create_signature(api_key, secret_key, method, url, builder):
    if api_key is None or secret_key is None or api_key == "" or secret_key == "":
        raise HuobiApiException(HuobiApiException.KEY_MISSING,
                                "API key and secret key are required")

    timestamp = utc_now()
    builder.put_url("AccessKeyId", api_key)
    builder.put_url("SignatureVersion", "2")
    builder.put_url("SignatureMethod", "HmacSHA256")
    builder.put_url("Timestamp", timestamp)

    host = urllib.parse.urlparse(url).hostname
    path = urllib.parse.urlparse(url).path

    # 对参数进行排序:
    keys = sorted(builder.param_map.keys())
    # 加入&
    qs0 = '&'.join([
        '%s=%s' % (key, parse.quote(builder.param_map[key], safe=''))
        for key in keys
    ])
    # 请求方法,域名,路径,参数 后加入`\n`
    payload0 = '%s\n%s\n%s\n%s' % (method, host, path, qs0)
    dig = hmac.new(secret_key.encode('utf-8'),
                   msg=payload0.encode('utf-8'),
                   digestmod=hashlib.sha256).digest()
    # 进行base64编码
    s = base64.b64encode(dig).decode()
    builder.put_url("Signature", s)
Example #9
0
def check_symbol_list(symbols):
    if not isinstance(symbols, list):
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] symbols in subscription is not a list")
    for symbol in symbols:
        check_symbol(symbol)
Example #10
0
    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
Example #11
0
def check_range(value, min_value, max_value, name):
    if value is None:
        return
    if min_value > value or value > max_value:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] " + name + " is out of bound. " + str(value) +
            " is not in [" + str(min_value) + "," + str(max_value) + "]")
Example #12
0
 def get_account_by_id(self, api_key, account_id):
     user = self.get_user(api_key)
     account = user.get_account_by_id(account_id)
     if account is None:
         raise HuobiApiException(HuobiApiException.RUNTIME_ERROR,
                                 "[User] Cannot find the account, key: " +
                                 api_key + ", account id: " + str(account_id))
     return account
Example #13
0
    def get_account_by_id(self, account_id):
        """
        Get account by account id.

        :param account_id: The specified account id.
        :return: The account.
        """
        for account in self.accounts:
            if account.id == account_id:
                return account
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] No such account")
Example #14
0
    def get_account_by_type(self, account_type, subtype: 'str' = None):
        """
        Get account by account type.

        :param account_type: The specified account type.
        :param subtype: for margin trade
        :return: The account.
        """
        margin_account_type_list = [AccountType.MARGIN]
        if account_type in margin_account_type_list and subtype is None or len(
                subtype) == 0:
            raise HuobiApiException(
                HuobiApiException.INPUT_ERROR,
                "[Input] subtype for margin account error")
        for account in self.accounts:
            if account.account_type == account_type:
                if account_type in margin_account_type_list:
                    if account.subtype == subtype:
                        return account
                else:
                    return account
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] No such account")
 def on_error(self, error_message):
     if self.request.error_handler is not None:
         exception = HuobiApiException(HuobiApiException.SUBSCRIPTION_ERROR, error_message)
         self.request.error_handler(exception)
     self.logger.error("[Sub][" + str(self.id) + "] " + str(error_message))
Example #16
0
 def __check_mandatory_field(self, name):
     if name not in self.json_object:
         raise HuobiApiException(
             HuobiApiException.RUNTIME_ERROR,
             "[Json] Get json item field: " + name + " does not exist")
Example #17
0
def greater_or_equal(value, base, name):
    if value is not None and value < base:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] " + name + " should be greater than " + base)
Example #18
0
def check_should_none(value, name):
    if value is not None:
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] " + name + " should be null")