libentry 1.11.2__py3-none-any.whl → 1.11.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
libentry/api.py CHANGED
@@ -12,10 +12,9 @@ __all__ = [
12
12
 
13
13
  import os
14
14
  from dataclasses import dataclass, field
15
- from typing import Any, Callable, Iterable, List, Literal, Mapping, Optional, Tuple, Union
15
+ from typing import Any, Callable, Iterable, List, Literal, Mapping, Optional, Tuple
16
16
 
17
17
  import requests
18
- from pydantic import BaseModel
19
18
 
20
19
  from libentry import json
21
20
 
@@ -129,23 +128,40 @@ def list_api_info(obj) -> List[Tuple[Callable, APIInfo]]:
129
128
  return api_list
130
129
 
131
130
 
131
+ def _load_json_or_str(text: str):
132
+ try:
133
+ return json.loads(text)
134
+ except ValueError:
135
+ return text
136
+
137
+
132
138
  class ServiceError(RuntimeError):
133
139
 
134
- def __init__(self, error_message=None, error_type=None, traceback=None):
135
- self.error_message = error_message + "\n"
136
- self.error_type = " " + error_type if error_type is not None else ""
137
- self.traceback = traceback
140
+ def __init__(self, text: str):
141
+ err = _load_json_or_str(text)
142
+ if isinstance(err, dict):
143
+ if "message" in err:
144
+ self.message = err.get("message")
145
+ self.error = err.get("error")
146
+ self.traceback = err.get("traceback")
147
+ else:
148
+ self.message = str(err)
149
+ self.error = ""
150
+ self.traceback = None
151
+ else:
152
+ self.message = err
153
+ self.error = ""
154
+ self.traceback = None
138
155
 
139
156
  def __str__(self):
140
- message = (
141
- f"{self.error_message}\n"
142
- f"This is caused by server side error{self.error_type}. "
143
- )
144
- traceback = (
145
- f"Below is the stacktrace:\n"
146
- f"{self.traceback.rstrip()}"
147
- ) if self.traceback is not None else ""
148
- return message + traceback
157
+ lines = []
158
+ if self.message:
159
+ lines += [self.message, "\n\n"]
160
+ if self.error:
161
+ lines += ["This is caused by server side error ", self.error, ".\n"]
162
+ if self.traceback:
163
+ lines += ["Below is the stacktrace:\n", self.traceback.rstrip()]
164
+ return "".join(lines)
149
165
 
150
166
 
151
167
  class APIClient:
@@ -178,22 +194,19 @@ class APIClient:
178
194
  response = requests.get(api_url, headers=self.headers, verify=self.verify, timeout=timeout)
179
195
 
180
196
  if response.status_code != 200:
181
- err = self._load_json(response.text)
182
- is_standard_error = isinstance(err, dict) and "message" in err
183
- message = err["message"] if is_standard_error else str(err)
184
- error_type = err.get("error")
185
- traceback = err.get("traceback")
186
- raise ServiceError(message, error_type, traceback)
197
+ text = response.text
198
+ response.close()
199
+ raise ServiceError(text)
187
200
 
188
201
  try:
189
- return self._load_json(response.text)
202
+ return _load_json_or_str(response.text)
190
203
  finally:
191
204
  response.close()
192
205
 
193
206
  def post(
194
207
  self,
195
208
  path: str,
196
- json_data: Mapping = None,
209
+ json_data: Optional[Mapping] = None,
197
210
  stream=False,
198
211
  timeout=60,
199
212
  chunk_delimiter: str = "\n\n",
@@ -212,12 +225,9 @@ class APIClient:
212
225
  timeout=timeout
213
226
  )
214
227
  if response.status_code != 200:
215
- err = self._load_json(response.text)
216
- is_standard_error = isinstance(err, dict) and "message" in err
217
- message = err["message"] if is_standard_error else str(err)
218
- error_type = err.get("error")
219
- traceback = err.get("traceback")
220
- raise ServiceError(message, error_type, traceback)
228
+ text = response.text
229
+ response.close()
230
+ raise ServiceError(text)
221
231
 
222
232
  if stream:
223
233
  if chunk_delimiter is None:
@@ -232,7 +242,7 @@ class APIClient:
232
242
  )
233
243
  else:
234
244
  try:
235
- return self._load_json(response.text)
245
+ return _load_json_or_str(response.text)
236
246
  finally:
237
247
  response.close()
238
248
 
@@ -260,34 +270,6 @@ class APIClient:
260
270
  else:
261
271
  continue
262
272
 
263
- yield self._load_json(chunk)
273
+ yield _load_json_or_str(chunk)
264
274
  finally:
265
275
  response.close()
266
-
267
- @staticmethod
268
- def _load_json(text: str):
269
- try:
270
- return json.loads(text)
271
- except ValueError:
272
- return text
273
-
274
- def __getattr__(self, item: str):
275
- return MethodProxy(self, item)
276
-
277
-
278
- class MethodProxy:
279
-
280
- def __init__(self, client: APIClient, url: str):
281
- self.client = client
282
- self.url = url
283
-
284
- def __call__(self, request: Optional[Union[Mapping, BaseModel]] = None, **kwargs):
285
- if request is None:
286
- request = kwargs
287
- elif isinstance(request, BaseModel):
288
- request = request.model_dump()
289
- for k, v in kwargs.items():
290
- if isinstance(v, BaseModel):
291
- v = v.model_dump()
292
- request[k] = v
293
- return self.client.post(self.url, request)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: libentry
3
- Version: 1.11.2
3
+ Version: 1.11.3
4
4
  Summary: Entries for experimental utilities.
5
5
  Home-page: https://github.com/XoriieInpottn/libentry
6
6
  Author: xi
@@ -1,5 +1,5 @@
1
1
  libentry/__init__.py,sha256=rDBip9M1Xb1N4wMKE1ni_DldrQbkRjp8DxPkTp3K2qo,170
2
- libentry/api.py,sha256=W6pDdozmMnfnwVjsLoz5YvEGHEjYPEqpsndKlKBgpiA,8809
2
+ libentry/api.py,sha256=m0j9aGPD4ewJdeQkooUoNJeTbrAtqlpSscmy3gfOTYw,7921
3
3
  libentry/argparse.py,sha256=Bk11H4WRKxcjMlSd0mjWj1T4NWh0JW5eA7TX3C21IoE,10116
4
4
  libentry/dataclasses.py,sha256=AQV2PuxplJCwGZ5HKX72U-z-POUhTdy3XtpEK9KNIGQ,4541
5
5
  libentry/executor.py,sha256=cTV0WxJi0nU1TP-cOwmeodN8DD6L1691M2HIQsJtGrU,6582
@@ -14,9 +14,9 @@ libentry/service/list.py,sha256=ElHWhTgShGOhaxMUEwVbMXos0NQKjHsODboiQ-3AMwE,1397
14
14
  libentry/service/running.py,sha256=FrPJoJX6wYxcHIysoatAxhW3LajCCm0Gx6l7__6sULQ,5105
15
15
  libentry/service/start.py,sha256=mZT7b9rVULvzy9GTZwxWnciCHgv9dbGN2JbxM60OMn4,1270
16
16
  libentry/service/stop.py,sha256=wOpwZgrEJ7QirntfvibGq-XsTC6b3ELhzRW2zezh-0s,1187
17
- libentry-1.11.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
18
- libentry-1.11.2.dist-info/METADATA,sha256=6OCqmqrv6dBqxh8QlDBILoySEewiFZ-KVPP62Cf-blo,500
19
- libentry-1.11.2.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
20
- libentry-1.11.2.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
21
- libentry-1.11.2.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
22
- libentry-1.11.2.dist-info/RECORD,,
17
+ libentry-1.11.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
18
+ libentry-1.11.3.dist-info/METADATA,sha256=oNzYKmc3HwT6ZGKaT-EHVKzEGj99lYQJvXQmNVVJDkc,500
19
+ libentry-1.11.3.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
20
+ libentry-1.11.3.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
21
+ libentry-1.11.3.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
22
+ libentry-1.11.3.dist-info/RECORD,,