天择加密量化开放框架下载
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.4 KiB

1 year ago
  1. # coding: utf-8
  2. """
  3. Gate API v4
  4. Welcome to Gate.io API APIv4 provides spot, margin and futures trading operations. There are public APIs to retrieve the real-time market statistics, and private APIs which needs authentication to trade on user's behalf. # noqa: E501
  5. Contact: support@mail.gate.io
  6. Generated by: https://openapi-generator.tech
  7. """
  8. import six
  9. class OpenApiException(Exception):
  10. """The base exception class for all OpenAPIExceptions"""
  11. class ApiTypeError(OpenApiException, TypeError):
  12. def __init__(self, msg, path_to_item=None, valid_classes=None, key_type=None):
  13. """Raises an exception for TypeErrors
  14. Args:
  15. msg (str): the exception message
  16. Keyword Args:
  17. path_to_item (list): a list of keys an indices to get to the
  18. current_item
  19. None if unset
  20. valid_classes (tuple): the primitive classes that current item
  21. should be an instance of
  22. None if unset
  23. key_type (bool): False if our value is a value in a dict
  24. True if it is a key in a dict
  25. False if our item is an item in a list
  26. None if unset
  27. """
  28. self.path_to_item = path_to_item
  29. self.valid_classes = valid_classes
  30. self.key_type = key_type
  31. full_msg = msg
  32. if path_to_item:
  33. full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
  34. super(ApiTypeError, self).__init__(full_msg)
  35. class ApiValueError(OpenApiException, ValueError):
  36. def __init__(self, msg, path_to_item=None):
  37. """
  38. Args:
  39. msg (str): the exception message
  40. Keyword Args:
  41. path_to_item (list) the path to the exception in the
  42. received_data dict. None if unset
  43. """
  44. self.path_to_item = path_to_item
  45. full_msg = msg
  46. if path_to_item:
  47. full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
  48. super(ApiValueError, self).__init__(full_msg)
  49. class ApiKeyError(OpenApiException, KeyError):
  50. def __init__(self, msg, path_to_item=None):
  51. """
  52. Args:
  53. msg (str): the exception message
  54. Keyword Args:
  55. path_to_item (None/list) the path to the exception in the
  56. received_data dict
  57. """
  58. self.path_to_item = path_to_item
  59. full_msg = msg
  60. if path_to_item:
  61. full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
  62. super(ApiKeyError, self).__init__(full_msg)
  63. class ApiException(OpenApiException):
  64. def __init__(self, status=None, reason=None, http_resp=None):
  65. if http_resp:
  66. self.status = http_resp.status
  67. self.reason = http_resp.reason
  68. self.body = http_resp.data
  69. self.headers = http_resp.getheaders()
  70. else:
  71. self.status = status
  72. self.reason = reason
  73. self.body = None
  74. self.headers = None
  75. def __str__(self):
  76. """Custom error messages for exception"""
  77. error_message = "({0})\n" "Reason: {1}\n".format(self.status, self.reason)
  78. if self.headers:
  79. error_message += "HTTP response headers: {0}\n".format(self.headers)
  80. if self.body:
  81. error_message += "HTTP response body: {0}\n".format(self.body)
  82. return error_message
  83. class GateApiException(ApiException):
  84. def __init__(self, label=None, message=None, detail=None, exp=None):
  85. """Init GateApiException from ApiException
  86. :param str label: error label parsed
  87. :param str message: error message parsed
  88. :param str detail: possible error message parsed
  89. :param ApiException exp: parent exception
  90. """
  91. self.label = label
  92. self.message = detail if detail else message
  93. self.status = exp.status
  94. self.reason = exp.reason
  95. self.body = exp.body
  96. self.headers = exp.headers
  97. def render_path(path_to_item):
  98. """Returns a string representation of a path"""
  99. result = ""
  100. for pth in path_to_item:
  101. if isinstance(pth, six.integer_types):
  102. result += "[{0}]".format(pth)
  103. else:
  104. result += "['{0}']".format(pth)
  105. return result