libentry 1.11.2__py3-none-any.whl → 1.11.4__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 +44 -62
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/METADATA +1 -1
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/RECORD +7 -7
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/LICENSE +0 -0
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/WHEEL +0 -0
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/top_level.txt +0 -0
- {libentry-1.11.2.dist-info → libentry-1.11.4.dist-info}/zip-safe +0 -0
libentry/api.py
CHANGED
@@ -10,12 +10,11 @@ __all__ = [
|
|
10
10
|
"APIClient",
|
11
11
|
]
|
12
12
|
|
13
|
-
import os
|
14
13
|
from dataclasses import dataclass, field
|
15
|
-
from typing import Any, Callable, Iterable, List, Literal, Mapping, Optional, Tuple
|
14
|
+
from typing import Any, Callable, Iterable, List, Literal, Mapping, Optional, Tuple
|
15
|
+
from urllib.parse import urljoin
|
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,
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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:
|
@@ -174,33 +190,30 @@ class APIClient:
|
|
174
190
|
self.verify = verify
|
175
191
|
|
176
192
|
def get(self, path: str, timeout=60):
|
177
|
-
api_url =
|
193
|
+
api_url = urljoin(self.base_url, path)
|
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
|
-
|
182
|
-
|
183
|
-
|
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
|
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",
|
200
213
|
chunk_prefix: str = None,
|
201
214
|
chunk_suffix: str = None,
|
202
215
|
):
|
203
|
-
full_url =
|
216
|
+
full_url = urljoin(self.base_url, path)
|
204
217
|
|
205
218
|
data = json.dumps(json_data) if json_data is not None else None
|
206
219
|
response = requests.post(
|
@@ -212,12 +225,9 @@ class APIClient:
|
|
212
225
|
timeout=timeout
|
213
226
|
)
|
214
227
|
if response.status_code != 200:
|
215
|
-
|
216
|
-
|
217
|
-
|
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
|
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
|
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,5 +1,5 @@
|
|
1
1
|
libentry/__init__.py,sha256=rDBip9M1Xb1N4wMKE1ni_DldrQbkRjp8DxPkTp3K2qo,170
|
2
|
-
libentry/api.py,sha256=
|
2
|
+
libentry/api.py,sha256=2I52bVwJPxr_FTHCn4fCm08lGpNofcp9JGWz8JJJq4A,7934
|
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.
|
18
|
-
libentry-1.11.
|
19
|
-
libentry-1.11.
|
20
|
-
libentry-1.11.
|
21
|
-
libentry-1.11.
|
22
|
-
libentry-1.11.
|
17
|
+
libentry-1.11.4.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
18
|
+
libentry-1.11.4.dist-info/METADATA,sha256=bWMkqIhQ4ro2pebBCS7tRNdvXEiOwWy7g2mmVSB1098,500
|
19
|
+
libentry-1.11.4.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
|
20
|
+
libentry-1.11.4.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
|
21
|
+
libentry-1.11.4.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
22
|
+
libentry-1.11.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|