mm-std 0.3.28__py3-none-any.whl → 0.3.29__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.
mm_std/http/http_request.py
CHANGED
@@ -74,7 +74,7 @@ async def _request_with_http_or_none_proxy(
|
|
74
74
|
method, url, params=params, data=data, json=json, headers=headers, cookies=cookies, proxy=proxy, timeout=timeout
|
75
75
|
) as res:
|
76
76
|
return HttpResponse(
|
77
|
-
|
77
|
+
status_code=res.status,
|
78
78
|
error=None,
|
79
79
|
error_message=None,
|
80
80
|
body=(await res.read()).decode(),
|
@@ -102,7 +102,7 @@ async def _request_with_socks_proxy(
|
|
102
102
|
) as res,
|
103
103
|
):
|
104
104
|
return HttpResponse(
|
105
|
-
|
105
|
+
status_code=res.status,
|
106
106
|
error=None,
|
107
107
|
error_message=None,
|
108
108
|
body=(await res.read()).decode(),
|
mm_std/http/response.py
CHANGED
@@ -3,8 +3,9 @@ import json
|
|
3
3
|
from typing import Any
|
4
4
|
|
5
5
|
import pydash
|
6
|
-
from pydantic import
|
7
|
-
|
6
|
+
from pydantic import BaseModel
|
7
|
+
|
8
|
+
from mm_std.data_result import DataResult
|
8
9
|
|
9
10
|
|
10
11
|
@enum.unique
|
@@ -15,79 +16,32 @@ class HttpError(str, enum.Enum):
|
|
15
16
|
ERROR = "error"
|
16
17
|
|
17
18
|
|
18
|
-
class HttpResponse:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
body: str | None = None,
|
25
|
-
headers: dict[str, str] | None = None,
|
26
|
-
) -> None:
|
27
|
-
self.status = status
|
28
|
-
self.error = error
|
29
|
-
self.error_message = error_message
|
30
|
-
self.body = body
|
31
|
-
self.headers = headers
|
32
|
-
|
33
|
-
self._json_data: Any = None
|
34
|
-
self._json_parsed = False
|
35
|
-
self._json_parsed_error = False
|
19
|
+
class HttpResponse(BaseModel):
|
20
|
+
status_code: int | None = None
|
21
|
+
error: HttpError | None = None
|
22
|
+
error_message: str | None = None
|
23
|
+
body: str | None = None
|
24
|
+
headers: dict[str, str] | None = None
|
36
25
|
|
37
|
-
def
|
26
|
+
def parse_json_body(self, path: str | None = None, none_on_error: bool = False) -> Any: # noqa: ANN401
|
38
27
|
if self.body is None:
|
39
|
-
|
40
|
-
|
28
|
+
if none_on_error:
|
29
|
+
return None
|
30
|
+
raise ValueError("Body is None")
|
31
|
+
|
41
32
|
try:
|
42
|
-
|
43
|
-
|
44
|
-
self._json_parsed_error = False
|
33
|
+
res = json.loads(self.body)
|
34
|
+
return pydash.get(res, path, None) if path else res
|
45
35
|
except json.JSONDecodeError:
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def json(self, path: str | None = None) -> Any: # noqa: ANN401
|
50
|
-
if not self._json_parsed:
|
51
|
-
self._parse_json()
|
52
|
-
if path:
|
53
|
-
return pydash.get(self._json_data, path, None)
|
54
|
-
return self._json_data
|
55
|
-
|
56
|
-
def dict(self) -> dict[str, object]:
|
57
|
-
return {
|
58
|
-
"status": self.status,
|
59
|
-
"error": self.error,
|
60
|
-
"error_message": self.error_message,
|
61
|
-
"body": self.body,
|
62
|
-
"headers": self.headers,
|
63
|
-
}
|
64
|
-
|
65
|
-
def is_json_parse_error(self) -> bool:
|
66
|
-
if not self._json_parsed:
|
67
|
-
self._parse_json()
|
68
|
-
return self._json_parsed_error
|
36
|
+
if none_on_error:
|
37
|
+
return None
|
38
|
+
raise
|
69
39
|
|
70
|
-
def
|
71
|
-
return
|
40
|
+
def is_error(self) -> bool:
|
41
|
+
return self.error is not None or (self.status_code is not None and self.status_code >= 400)
|
72
42
|
|
73
|
-
|
74
|
-
|
75
|
-
return core_schema.no_info_after_validator_function(
|
76
|
-
cls._validate,
|
77
|
-
core_schema.any_schema(),
|
78
|
-
serialization=core_schema.plain_serializer_function_ser_schema(lambda x: x.dict()),
|
79
|
-
)
|
43
|
+
def to_data_result_err[T](self, error: str | None = None) -> DataResult[T]:
|
44
|
+
return DataResult(err=error or self.error or "error", data=self.model_dump())
|
80
45
|
|
81
|
-
|
82
|
-
|
83
|
-
if isinstance(v, cls):
|
84
|
-
return v
|
85
|
-
if isinstance(v, dict):
|
86
|
-
return cls(
|
87
|
-
status=v.get("status"),
|
88
|
-
error=HttpError(v["error"]) if v.get("error") else None,
|
89
|
-
error_message=v.get("error_message"),
|
90
|
-
body=v.get("body"),
|
91
|
-
headers=v.get("headers"),
|
92
|
-
)
|
93
|
-
raise TypeError(f"Cannot parse value as {cls.__name__}: {v}")
|
46
|
+
def to_data_result_ok[T](self, result: T) -> DataResult[T]:
|
47
|
+
return DataResult(ok=result, data=self.model_dump())
|
@@ -27,8 +27,8 @@ mm_std/concurrency/sync_decorators.py,sha256=syCQBOmN7qPO55yzgJB2rbkh10CVww376hm
|
|
27
27
|
mm_std/concurrency/sync_scheduler.py,sha256=j4tBL_cBI1spr0cZplTA7N2CoYsznuORMeRN8rpR6gY,2407
|
28
28
|
mm_std/concurrency/sync_task_runner.py,sha256=s5JPlLYLGQGHIxy4oDS-PN7O9gcy-yPZFoNm8RQwzcw,1780
|
29
29
|
mm_std/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
30
|
-
mm_std/http/http_request.py,sha256=
|
31
|
-
mm_std/http/response.py,sha256=
|
32
|
-
mm_std-0.3.
|
33
|
-
mm_std-0.3.
|
34
|
-
mm_std-0.3.
|
30
|
+
mm_std/http/http_request.py,sha256=h74_ZjACwdbeINjzhuEwNUpvnaHZvyGl7TExjlBwMGg,3704
|
31
|
+
mm_std/http/response.py,sha256=qEQYV_TkwttS9fVuQwNmyB9QtrwUsuLAcC7juVbyCAg,1387
|
32
|
+
mm_std-0.3.29.dist-info/METADATA,sha256=ZZrqphz0r5EALNmkV3ysuqLCt8h5X_MxD3MSVSkLzLM,415
|
33
|
+
mm_std-0.3.29.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
34
|
+
mm_std-0.3.29.dist-info/RECORD,,
|
File without changes
|