def create_request(self, method, url, params, parse):
        builder = UrlParamsBuilder()
        if params and len(params):
            if method in [HttpMethod.GET, HttpMethod.GET_SIGN]:
                for key, value in params.items():
                    builder.put_url(key, value)
            elif method in [HttpMethod.POST, HttpMethod.POST_SIGN]:
                for key, value in params.items():
                    builder.put_post(key, value)
            else:
                raise HuobiApiException(HuobiApiException.EXEC_ERROR,
                                        "[error] undefined HTTP method")

        if method == HttpMethod.GET:
            request = self.__create_request_by_get(url, builder)
        elif method == HttpMethod.GET_SIGN:
            request = self.__create_request_by_get_with_signature(url, builder)
        elif method == HttpMethod.POST_SIGN:
            request = self.__create_request_by_post_with_signature(url, builder)
        elif method == HttpMethod.POST:
            request = self.__create_request_by_post_with_signature(url, builder)
        else:
            raise HuobiApiException(HuobiApiException.INPUT_ERROR, "[Input] " + method + "  is invalid http method")

        request.json_parser = parse

        return request
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")
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")
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")
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")
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)
示例#7
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)
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) + "]")
def check_time_in_force(time_in_force, order_type):
    if time_in_force is None:
        return

    if order_type in [OrderType.BUY_MARKET, OrderType.SELL_MARKET] \
            and time_in_force in [TimeInForceType.GTC, TimeInForceType.BOC, TimeInForceType.FOK]:
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR,
            "[Input] timeInForce not supported for market order")
示例#10
0
    def create_request_post_batch(self, method, url, params, parse):
        builder = UrlParamsBuilder()
        if params and len(params):
            if method in [HttpMethod.POST, HttpMethod.POST_SIGN]:
                if isinstance(params, list):
                    builder.post_list = params
            else:
                raise HuobiApiException(HuobiApiException.EXEC_ERROR,
                                        "[error] undefined HTTP method")

        request = self.__create_request_by_post_with_signature(url, builder)
        request.json_parser = parse

        return request
示例#11
0
def check_response(dict_data):
    status = dict_data.get("status", None)
    code = dict_data.get("code", None)
    success = dict_data.get("success", None)
    if status and len(status):
        if TypeCheck.is_basic(status):  # for normal case
            if status == "error":
                err_code = dict_data.get("err-code", 0)
                err_msg = dict_data.get("err-msg", "")
                raise HuobiApiException(
                    HuobiApiException.EXEC_ERROR,
                    "[Executing] " + str(err_code) + ": " + err_msg)
            elif status != "ok":
                raise HuobiApiException(
                    HuobiApiException.RUNTIME_ERROR,
                    "[Invoking] Response is not expected: " + status)
        elif TypeCheck.is_dict(
                status
        ):  # for https://status.huobigroup.com/api/v2/summary.json in example example/generic/get_system_status.py
            if dict_data.get("page") and dict_data.get("components"):
                pass
            else:
                raise HuobiApiException(
                    HuobiApiException.EXEC_ERROR,
                    "[Executing] System is in maintenances")
    elif code:
        code_int = int(code)
        if code_int != 200:
            err_code = dict_data.get("code", 0)
            err_msg = dict_data.get("message", "")
            raise HuobiApiException(
                HuobiApiException.EXEC_ERROR,
                "[Executing] " + str(err_code) + ": " + err_msg)
    elif success is not None:
        if bool(success) is False:
            err_code = etf_result_check(dict_data.get("code"))
            err_msg = dict_data.get("message", "")
            if err_code == "":
                raise HuobiApiException(HuobiApiException.EXEC_ERROR,
                                        "[Executing] " + err_msg)
            else:
                raise HuobiApiException(
                    HuobiApiException.EXEC_ERROR,
                    "[Executing] " + str(err_code) + ": " + err_msg)
    else:
        raise HuobiApiException(
            HuobiApiException.RUNTIME_ERROR,
            "[Invoking] Status cannot be found in response.")
示例#12
0
 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))
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)
def check_in_list(value, list_configed, name):
    if (value is not None) and (value not in list_configed):
        raise HuobiApiException(
            HuobiApiException.INPUT_ERROR, "[Input] " + name +
            " should be one in " + (",".join(list_configed)))
def check_should_none(value, name):
    if value is not None:
        raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                "[Input] " + name + " should be null")