libentry 1.11.1__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,15 +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_type = error_type
136
- self.error_message = error_message
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
- return f"{self.error_message}\nCaused by server side error:\n{self.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)
141
165
 
142
166
 
143
167
  class APIClient:
@@ -170,21 +194,19 @@ class APIClient:
170
194
  response = requests.get(api_url, headers=self.headers, verify=self.verify, timeout=timeout)
171
195
 
172
196
  if response.status_code != 200:
173
- err = self._load_json(response.text)
174
- if isinstance(err, dict) and "error" in err and "message" in err and "traceback" in err:
175
- raise ServiceError(err["message"], err["error"], err["traceback"])
176
- else:
177
- raise ServiceError(str(err), "UnknownError", "")
197
+ text = response.text
198
+ response.close()
199
+ raise ServiceError(text)
178
200
 
179
201
  try:
180
- return self._load_json(response.text)
202
+ return _load_json_or_str(response.text)
181
203
  finally:
182
204
  response.close()
183
205
 
184
206
  def post(
185
207
  self,
186
208
  path: str,
187
- json_data: Mapping = None,
209
+ json_data: Optional[Mapping] = None,
188
210
  stream=False,
189
211
  timeout=60,
190
212
  chunk_delimiter: str = "\n\n",
@@ -203,11 +225,9 @@ class APIClient:
203
225
  timeout=timeout
204
226
  )
205
227
  if response.status_code != 200:
206
- err = self._load_json(response.text)
207
- if isinstance(err, dict) and "error" in err and "message" in err and "traceback" in err:
208
- raise ServiceError(err["message"], err["error"], err["traceback"])
209
- else:
210
- raise ServiceError(str(err), "UnknownError", "")
228
+ text = response.text
229
+ response.close()
230
+ raise ServiceError(text)
211
231
 
212
232
  if stream:
213
233
  if chunk_delimiter is None:
@@ -222,7 +242,7 @@ class APIClient:
222
242
  )
223
243
  else:
224
244
  try:
225
- return self._load_json(response.text)
245
+ return _load_json_or_str(response.text)
226
246
  finally:
227
247
  response.close()
228
248
 
@@ -250,34 +270,6 @@ class APIClient:
250
270
  else:
251
271
  continue
252
272
 
253
- yield self._load_json(chunk)
273
+ yield _load_json_or_str(chunk)
254
274
  finally:
255
275
  response.close()
256
-
257
- @staticmethod
258
- def _load_json(text: str):
259
- try:
260
- return json.loads(text)
261
- except ValueError:
262
- return text
263
-
264
- def __getattr__(self, item: str):
265
- return MethodProxy(self, item)
266
-
267
-
268
- class MethodProxy:
269
-
270
- def __init__(self, client: APIClient, url: str):
271
- self.client = client
272
- self.url = url
273
-
274
- def __call__(self, request: Optional[Union[Mapping, BaseModel]] = None, **kwargs):
275
- if request is None:
276
- request = kwargs
277
- elif isinstance(request, BaseModel):
278
- request = request.model_dump()
279
- for k, v in kwargs.items():
280
- if isinstance(v, BaseModel):
281
- v = v.model_dump()
282
- request[k] = v
283
- 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.1
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=vezmTs6wrLu-RVO1_c2RWR0Ag-PFeHMiJuFhA1SA-EQ,8463
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.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
18
- libentry-1.11.1.dist-info/METADATA,sha256=kFaJRuzu6e1juY-GTXozSE2Tfz-2SVERUz72tdH1X2Q,500
19
- libentry-1.11.1.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
20
- libentry-1.11.1.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
21
- libentry-1.11.1.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
22
- libentry-1.11.1.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,,