libentry 1.21.2__py3-none-any.whl → 1.21.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 CHANGED
@@ -10,10 +10,11 @@ __all__ = [
10
10
  "APIClient",
11
11
  ]
12
12
 
13
+ import asyncio
13
14
  from dataclasses import dataclass, field
14
15
  from time import sleep
15
16
  from typing import Any, AsyncIterable, Callable, Iterable, List, Literal, Mapping, Optional, Tuple, Union
16
- from urllib.parse import urljoin
17
+ from urllib.parse import urlencode, urljoin
17
18
 
18
19
  import httpx
19
20
  from urllib3 import PoolManager
@@ -216,6 +217,7 @@ class BaseClient:
216
217
  stream: bool,
217
218
  verify: bool,
218
219
  ) -> Union[bytes, Iterable[bytes]]:
220
+ headers = self.headers if headers is None else {**self.headers, **headers}
219
221
  response = self.URLLIB3_POOL[int(verify)].request(
220
222
  method=method,
221
223
  url=url,
@@ -304,6 +306,7 @@ class BaseClient:
304
306
  stream: bool,
305
307
  verify: bool,
306
308
  ):
309
+ headers = self.headers if headers is None else {**self.headers, **headers}
307
310
  if not stream:
308
311
  async with httpx.AsyncClient(headers=headers, verify=verify) as client:
309
312
  response = await client.request(
@@ -377,12 +380,25 @@ class BaseClient:
377
380
  err = e
378
381
  if callable(on_error):
379
382
  on_error(e)
380
- sleep(interval)
383
+ await asyncio.sleep(interval)
381
384
  raise err
382
385
 
383
386
 
384
387
  class APIClient(BaseClient):
385
388
 
389
+ @staticmethod
390
+ def _encode_params(data):
391
+ result = []
392
+ for k, v in data.items():
393
+ if v is not None:
394
+ result.append(
395
+ (
396
+ k.encode("utf-8") if isinstance(k, str) else k,
397
+ v.encode("utf-8") if isinstance(v, str) else v,
398
+ )
399
+ )
400
+ return urlencode(result, doseq=True)
401
+
386
402
  def request(
387
403
  self,
388
404
  method: Literal["GET", "POST"],
@@ -404,9 +420,14 @@ class APIClient(BaseClient):
404
420
  verify: Optional[bool] = None,
405
421
  ):
406
422
  full_url = urljoin(self.base_url, path)
407
- headers = {**self.headers}
408
- headers["Accept"] = headers["Accept"] + f"; stream={int(stream)}"
409
- body = json.dumps(json_data) if json_data is not None else None
423
+ if headers is None:
424
+ headers = {}
425
+ accept = headers["Accept"] if "Accept" in headers else self.headers["Accept"]
426
+ headers["Accept"] = accept + f"; stream={int(stream)}"
427
+ if "Content-Type" in headers and headers["Content-Type"] == "application/x-www-form-urlencoded":
428
+ body = self._encode_params(json_data)
429
+ else:
430
+ body = json.dumps(json_data) if json_data is not None else None
410
431
  content = super().request(
411
432
  method,
412
433
  full_url,
@@ -481,6 +502,7 @@ class APIClient(BaseClient):
481
502
  self,
482
503
  path: Optional[str] = None,
483
504
  *,
505
+ headers: Optional[Mapping[str, str]] = None,
484
506
  timeout: float = 15,
485
507
  num_trials: int = 5,
486
508
  interval: float = 1,
@@ -490,6 +512,7 @@ class APIClient(BaseClient):
490
512
  return self.request(
491
513
  "GET",
492
514
  path,
515
+ headers=headers,
493
516
  timeout=timeout,
494
517
  num_trials=num_trials,
495
518
  interval=interval,
@@ -502,6 +525,7 @@ class APIClient(BaseClient):
502
525
  path: Optional[str] = None,
503
526
  json_data: Optional[Mapping] = None,
504
527
  *,
528
+ headers: Optional[Mapping[str, str]] = None,
505
529
  timeout: float = 15,
506
530
  num_trials: int = 5,
507
531
  interval: float = 1,
@@ -518,6 +542,7 @@ class APIClient(BaseClient):
518
542
  "POST",
519
543
  path,
520
544
  json_data=json_data,
545
+ headers=headers,
521
546
  timeout=timeout,
522
547
  num_trials=num_trials,
523
548
  interval=interval,
@@ -552,9 +577,14 @@ class APIClient(BaseClient):
552
577
  verify: Optional[bool] = None,
553
578
  ):
554
579
  full_url = urljoin(self.base_url, path)
555
- headers = {**self.headers}
556
- headers["Accept"] = headers["Accept"] + f"; stream={int(stream)}"
557
- body = json.dumps(json_data) if json_data is not None else None
580
+ if headers is None:
581
+ headers = {}
582
+ accept = headers["Accept"] if "Accept" in headers else self.headers["Accept"]
583
+ headers["Accept"] = accept + f"; stream={int(stream)}"
584
+ if "Content-Type" in headers and headers["Content-Type"] == "application/x-www-form-urlencoded":
585
+ body = self._encode_params(json_data)
586
+ else:
587
+ body = json.dumps(json_data) if json_data is not None else None
558
588
  content = await super().request_async(
559
589
  method,
560
590
  full_url,
@@ -629,6 +659,7 @@ class APIClient(BaseClient):
629
659
  self,
630
660
  path: Optional[str] = None,
631
661
  *,
662
+ headers: Optional[Mapping[str, str]] = None,
632
663
  timeout: float = 15,
633
664
  num_trials: int = 5,
634
665
  interval: float = 1,
@@ -638,6 +669,7 @@ class APIClient(BaseClient):
638
669
  return await self.request_async(
639
670
  "GET",
640
671
  path,
672
+ headers=headers,
641
673
  timeout=timeout,
642
674
  num_trials=num_trials,
643
675
  interval=interval,
@@ -650,6 +682,7 @@ class APIClient(BaseClient):
650
682
  path: Optional[str] = None,
651
683
  json_data: Optional[Mapping] = None,
652
684
  *,
685
+ headers: Optional[Mapping[str, str]] = None,
653
686
  timeout: float = 15,
654
687
  num_trials: int = 5,
655
688
  interval: float = 1,
@@ -666,6 +699,7 @@ class APIClient(BaseClient):
666
699
  "POST",
667
700
  path,
668
701
  json_data=json_data,
702
+ headers=headers,
669
703
  timeout=timeout,
670
704
  num_trials=num_trials,
671
705
  interval=interval,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: libentry
3
- Version: 1.21.2
3
+ Version: 1.21.4
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=ko2YBIIx5H3dD0tedBkialzJGEDczFaP_PZmT1cIlak,148
2
- libentry/api.py,sha256=zlZF0IWyQu8GPEVAM0J6K8BOSgqI06p8JJVoCNaTCBo,22110
2
+ libentry/api.py,sha256=-8rvVdOOuJQ4vL4hYZcNMm8sUoSgv2r41QLNQ-Ny8vw,23607
3
3
  libentry/argparse.py,sha256=NxzXV-jBN51ReZsNs5aeyOfzwYQ5A5nJ95rWoa-FYCs,10415
4
4
  libentry/dataclasses.py,sha256=AQV2PuxplJCwGZ5HKX72U-z-POUhTdy3XtpEK9KNIGQ,4541
5
5
  libentry/executor.py,sha256=cTV0WxJi0nU1TP-cOwmeodN8DD6L1691M2HIQsJtGrU,6582
@@ -15,10 +15,10 @@ libentry/service/list.py,sha256=ElHWhTgShGOhaxMUEwVbMXos0NQKjHsODboiQ-3AMwE,1397
15
15
  libentry/service/running.py,sha256=FrPJoJX6wYxcHIysoatAxhW3LajCCm0Gx6l7__6sULQ,5105
16
16
  libentry/service/start.py,sha256=mZT7b9rVULvzy9GTZwxWnciCHgv9dbGN2JbxM60OMn4,1270
17
17
  libentry/service/stop.py,sha256=wOpwZgrEJ7QirntfvibGq-XsTC6b3ELhzRW2zezh-0s,1187
18
- libentry-1.21.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
19
- libentry-1.21.2.dist-info/METADATA,sha256=M2-N8UEjYrgrXC-85vZ2-ts0SeNjOTx9_Vjclgdu8Xg,813
20
- libentry-1.21.2.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
21
- libentry-1.21.2.dist-info/entry_points.txt,sha256=vgHmJZhM-kqM7U9S179UwDD3pM232tpzJ5NntncXi_8,62
22
- libentry-1.21.2.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
23
- libentry-1.21.2.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
24
- libentry-1.21.2.dist-info/RECORD,,
18
+ libentry-1.21.4.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
19
+ libentry-1.21.4.dist-info/METADATA,sha256=54r6arFvYSQnlyDjhXivn-HYRtM4cEt0kyPRz5sK5YY,813
20
+ libentry-1.21.4.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
21
+ libentry-1.21.4.dist-info/entry_points.txt,sha256=vgHmJZhM-kqM7U9S179UwDD3pM232tpzJ5NntncXi_8,62
22
+ libentry-1.21.4.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
23
+ libentry-1.21.4.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
24
+ libentry-1.21.4.dist-info/RECORD,,