requestmodel 0.6.1__tar.gz → 0.7.0__tar.gz
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.
- {requestmodel-0.6.1 → requestmodel-0.7.0}/PKG-INFO +1 -1
- {requestmodel-0.6.1 → requestmodel-0.7.0}/pyproject.toml +8 -8
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/adapters/requests.py +1 -1
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/model.py +19 -6
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/typing.py +7 -1
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/utils.py +0 -15
- {requestmodel-0.6.1 → requestmodel-0.7.0}/LICENSE +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/README.md +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/__init__.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/adapters/__init__.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/adapters/base.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/adapters/httpx.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/fastapi.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/params.py +0 -0
- {requestmodel-0.6.1 → requestmodel-0.7.0}/src/requestmodel/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "requestmodel"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.7.0"
|
|
4
4
|
description = "requestmodel"
|
|
5
5
|
authors = ["Jelmer Draaijer <info@jelmert.nl>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -21,14 +21,14 @@ httpx = "*"
|
|
|
21
21
|
pydantic = "^2.0.0"
|
|
22
22
|
|
|
23
23
|
[tool.poetry.group.requests.dependencies]
|
|
24
|
-
requests = "
|
|
24
|
+
requests = ">=2.31.0"
|
|
25
25
|
|
|
26
26
|
[tool.poetry.group.dev.dependencies]
|
|
27
|
-
pytest-asyncio = "
|
|
28
|
-
python-multipart = "
|
|
29
|
-
flask-wtf = "
|
|
30
|
-
flask-pytest = "
|
|
31
|
-
a2wsgi = "
|
|
27
|
+
pytest-asyncio = ">=0.21.1"
|
|
28
|
+
python-multipart = ">=0.0.7"
|
|
29
|
+
flask-wtf = ">=1.2.1"
|
|
30
|
+
flask-pytest = ">=0.0.5"
|
|
31
|
+
a2wsgi = ">=1.8.0"
|
|
32
32
|
Pygments = ">=2.10.0"
|
|
33
33
|
bandit = ">=1.7.4"
|
|
34
34
|
black = ">=21.10b0"
|
|
@@ -51,7 +51,7 @@ sphinx-autobuild = ">=2021.3.14"
|
|
|
51
51
|
typeguard = ">=2.13.3"
|
|
52
52
|
xdoctest = { extras = ["colors"], version = ">=0.15.10" }
|
|
53
53
|
myst-parser = { version = ">=0.16.1" }
|
|
54
|
-
fastapi = "
|
|
54
|
+
fastapi = ">=0.109.2"
|
|
55
55
|
|
|
56
56
|
[tool.coverage.paths]
|
|
57
57
|
source = ["src", "*/site-packages"]
|
|
@@ -50,4 +50,4 @@ class RequestsRequestModel(BaseRequestModel[ResponseType]):
|
|
|
50
50
|
r = self.as_request()
|
|
51
51
|
self.response = client.send(r.prepare())
|
|
52
52
|
self.handle_error(self.response)
|
|
53
|
-
return self.
|
|
53
|
+
return self.adapt_type(self.response)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
from typing import Any
|
|
1
2
|
from typing import ClassVar
|
|
2
3
|
from typing import Generic
|
|
3
4
|
from typing import Iterator
|
|
4
5
|
from typing import Optional
|
|
6
|
+
from typing import Protocol
|
|
5
7
|
from typing import Set
|
|
6
8
|
from typing import Type
|
|
7
9
|
|
|
@@ -13,6 +15,7 @@ from httpx._client import BaseClient
|
|
|
13
15
|
from pydantic import BaseModel
|
|
14
16
|
from pydantic import ConfigDict
|
|
15
17
|
from pydantic import TypeAdapter
|
|
18
|
+
from pydantic._internal._model_construction import ModelMetaclass
|
|
16
19
|
from typing_extensions import get_type_hints
|
|
17
20
|
from typing_extensions import override
|
|
18
21
|
|
|
@@ -26,6 +29,10 @@ from .utils import get_annotated_type
|
|
|
26
29
|
from .utils import unify_body
|
|
27
30
|
|
|
28
31
|
|
|
32
|
+
class JSONResponse(Protocol): # pragma: no cover
|
|
33
|
+
def json(self) -> Any: ... # noqa: E704
|
|
34
|
+
|
|
35
|
+
|
|
29
36
|
class BaseRequestModel(BaseModel, Generic[ResponseType]):
|
|
30
37
|
"""Declarative way to define a model"""
|
|
31
38
|
|
|
@@ -86,6 +93,15 @@ class BaseRequestModel(BaseModel, Generic[ResponseType]):
|
|
|
86
93
|
|
|
87
94
|
return request_args
|
|
88
95
|
|
|
96
|
+
def adapt_type(self, response: JSONResponse) -> ResponseType:
|
|
97
|
+
if isinstance(self.response_model, TypeAdapter):
|
|
98
|
+
return self.response_model.validate_python(response.json())
|
|
99
|
+
|
|
100
|
+
if isinstance(self.response_model, (BaseModel, ModelMetaclass)):
|
|
101
|
+
return self.response_model.model_validate(response.json())
|
|
102
|
+
|
|
103
|
+
raise ValueError("response_model must be a TypeAdapter or a BaseModel")
|
|
104
|
+
|
|
89
105
|
|
|
90
106
|
class RequestModel(BaseRequestModel[ResponseType]):
|
|
91
107
|
raw_response: Optional[Response] = None
|
|
@@ -98,18 +114,15 @@ class RequestModel(BaseRequestModel[ResponseType]):
|
|
|
98
114
|
r = self.as_request(client)
|
|
99
115
|
self.raw_response = client.send(r)
|
|
100
116
|
self.handle_error(self.raw_response)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return self.response_model.model_validate(self.raw_response.json())
|
|
117
|
+
|
|
118
|
+
return self.adapt_type(self.raw_response)
|
|
104
119
|
|
|
105
120
|
async def asend(self, client: AsyncClient) -> ResponseType:
|
|
106
121
|
"""Send the request asynchronously"""
|
|
107
122
|
r = self.as_request(client)
|
|
108
123
|
self.raw_response = await client.send(r)
|
|
109
124
|
self.handle_error(self.raw_response)
|
|
110
|
-
|
|
111
|
-
return self.response_model.validate_python(self.raw_response.json())
|
|
112
|
-
return self.response_model.model_validate(self.raw_response.json())
|
|
125
|
+
return self.adapt_type(self.raw_response)
|
|
113
126
|
|
|
114
127
|
def as_request(self, client: BaseClient) -> Request:
|
|
115
128
|
"""Transform the properties of the object into a request"""
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
from typing import Any
|
|
2
2
|
from typing import Dict
|
|
3
|
+
from typing import List
|
|
3
4
|
from typing import Type
|
|
4
5
|
from typing import TypeVar
|
|
6
|
+
from typing import Union
|
|
5
7
|
|
|
6
8
|
from pydantic import BaseModel
|
|
9
|
+
from pydantic import TypeAdapter
|
|
7
10
|
from pydantic.fields import FieldInfo
|
|
8
11
|
|
|
9
12
|
|
|
10
|
-
ResponseType = TypeVar(
|
|
13
|
+
ResponseType = TypeVar(
|
|
14
|
+
"ResponseType",
|
|
15
|
+
bound=Union[BaseModel, TypeAdapter[List[BaseModel]], List[BaseModel]],
|
|
16
|
+
)
|
|
11
17
|
RequestArgs = Dict[Type[FieldInfo], Dict[str, Any]]
|
|
@@ -42,21 +42,6 @@ def get_annotated_type(
|
|
|
42
42
|
else:
|
|
43
43
|
annotated_property = params.Query()
|
|
44
44
|
|
|
45
|
-
scalar_types = (params.Query, params.Path, params.Header, params.Cookie)
|
|
46
|
-
|
|
47
|
-
if isinstance(annotated_property, scalar_types) and is_complex:
|
|
48
|
-
# query params do accept lists
|
|
49
|
-
if not (isinstance(annotated_property, params.Query) and is_sequence):
|
|
50
|
-
annotated_name = annotated_property.__class__.__name__
|
|
51
|
-
|
|
52
|
-
# in 3.8 & 3.9 Dict does not have a __name__
|
|
53
|
-
if not hasattr(origin, "__name__"):
|
|
54
|
-
origin = get_origin(origin)
|
|
55
|
-
raise ValueError(
|
|
56
|
-
f"`{variable_key}` annotated as {annotated_name} "
|
|
57
|
-
f"can only be a scalar, not a `{origin.__name__}`"
|
|
58
|
-
)
|
|
59
|
-
|
|
60
45
|
return annotated_property
|
|
61
46
|
|
|
62
47
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|