Elffuss BraidHub

kikocis/continue public

3297e898700c · 6.4 KiBRawPermalink
1# coding: utf-8
2
3"""
4 Continue Hub IDE API
5
6 API for Continue IDE to fetch assistants and other related information. These endpoints are primarily used by the Continue IDE extensions for VS Code and JetBrains.
7
8 The version of the OpenAPI document: 1.0.0
9 Generated by OpenAPI Generator (https://openapi-generator.tech)
10
11 Do not edit the class manually.
12""" # noqa: E501
13
14from typing import Any, Optional
15from typing_extensions import Self
16
17class OpenApiException(Exception):
18 """The base exception class for all OpenAPIExceptions"""
19
20
21class ApiTypeError(OpenApiException, TypeError):
22 def __init__(self, msg, path_to_item=None, valid_classes=None,
23 key_type=None) -> None:
24 """ Raises an exception for TypeErrors
25
26 Args:
27 msg (str): the exception message
28
29 Keyword Args:
30 path_to_item (list): a list of keys an indices to get to the
31 current_item
32 None if unset
33 valid_classes (tuple): the primitive classes that current item
34 should be an instance of
35 None if unset
36 key_type (bool): False if our value is a value in a dict
37 True if it is a key in a dict
38 False if our item is an item in a list
39 None if unset
40 """
41 self.path_to_item = path_to_item
42 self.valid_classes = valid_classes
43 self.key_type = key_type
44 full_msg = msg
45 if path_to_item:
46 full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
47 super(ApiTypeError, self).__init__(full_msg)
48
49
50class ApiValueError(OpenApiException, ValueError):
51 def __init__(self, msg, path_to_item=None) -> None:
52 """
53 Args:
54 msg (str): the exception message
55
56 Keyword Args:
57 path_to_item (list) the path to the exception in the
58 received_data dict. None if unset
59 """
60
61 self.path_to_item = path_to_item
62 full_msg = msg
63 if path_to_item:
64 full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
65 super(ApiValueError, self).__init__(full_msg)
66
67
68class ApiAttributeError(OpenApiException, AttributeError):
69 def __init__(self, msg, path_to_item=None) -> None:
70 """
71 Raised when an attribute reference or assignment fails.
72
73 Args:
74 msg (str): the exception message
75
76 Keyword Args:
77 path_to_item (None/list) the path to the exception in the
78 received_data dict
79 """
80 self.path_to_item = path_to_item
81 full_msg = msg
82 if path_to_item:
83 full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
84 super(ApiAttributeError, self).__init__(full_msg)
85
86
87class ApiKeyError(OpenApiException, KeyError):
88 def __init__(self, msg, path_to_item=None) -> None:
89 """
90 Args:
91 msg (str): the exception message
92
93 Keyword Args:
94 path_to_item (None/list) the path to the exception in the
95 received_data dict
96 """
97 self.path_to_item = path_to_item
98 full_msg = msg
99 if path_to_item:
100 full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
101 super(ApiKeyError, self).__init__(full_msg)
102
103
104class ApiException(OpenApiException):
105
106 def __init__(
107 self,
108 status=None,
109 reason=None,
110 http_resp=None,
111 *,
112 body: Optional[str] = None,
113 data: Optional[Any] = None,
114 ) -> None:
115 self.status = status
116 self.reason = reason
117 self.body = body
118 self.data = data
119 self.headers = None
120
121 if http_resp:
122 if self.status is None:
123 self.status = http_resp.status
124 if self.reason is None:
125 self.reason = http_resp.reason
126 if self.body is None:
127 try:
128 self.body = http_resp.data.decode('utf-8')
129 except Exception:
130 pass
131 self.headers = http_resp.getheaders()
132
133 @classmethod
134 def from_response(
135 cls,
136 *,
137 http_resp,
138 body: Optional[str],
139 data: Optional[Any],
140 ) -> Self:
141 if http_resp.status == 400:
142 raise BadRequestException(http_resp=http_resp, body=body, data=data)
143
144 if http_resp.status == 401:
145 raise UnauthorizedException(http_resp=http_resp, body=body, data=data)
146
147 if http_resp.status == 403:
148 raise ForbiddenException(http_resp=http_resp, body=body, data=data)
149
150 if http_resp.status == 404:
151 raise NotFoundException(http_resp=http_resp, body=body, data=data)
152
153 # Added new conditions for 409 and 422
154 if http_resp.status == 409:
155 raise ConflictException(http_resp=http_resp, body=body, data=data)
156
157 if http_resp.status == 422:
158 raise UnprocessableEntityException(http_resp=http_resp, body=body, data=data)
159
160 if 500 <= http_resp.status <= 599:
161 raise ServiceException(http_resp=http_resp, body=body, data=data)
162 raise ApiException(http_resp=http_resp, body=body, data=data)
163
164 def __str__(self):
165 """Custom error messages for exception"""
166 error_message = "({0})\n"\
167 "Reason: {1}\n".format(self.status, self.reason)
168 if self.headers:
169 error_message += "HTTP response headers: {0}\n".format(
170 self.headers)
171
172 if self.data or self.body:
173 error_message += "HTTP response body: {0}\n".format(self.data or self.body)
174
175 return error_message
176
177
178class BadRequestException(ApiException):
179 pass
180
181
182class NotFoundException(ApiException):
183 pass
184
185
186class UnauthorizedException(ApiException):
187 pass
188
189
190class ForbiddenException(ApiException):
191 pass
192
193
194class ServiceException(ApiException):
195 pass
196
197
198class ConflictException(ApiException):
199 """Exception for HTTP 409 Conflict."""
200 pass
201
202
203class UnprocessableEntityException(ApiException):
204 """Exception for HTTP 422 Unprocessable Entity."""
205 pass
206
207
208def render_path(path_to_item):
209 """Returns a string representation of a path"""
210 result = ""
211 for pth in path_to_item:
212 if isinstance(pth, int):
213 result += "[{0}]".format(pth)
214 else:
215 result += "['{0}']".format(pth)
216 return result