requestmodel 0.6.1__tar.gz → 0.8.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.
@@ -1,24 +1,26 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: requestmodel
3
- Version: 0.6.1
3
+ Version: 0.8.0
4
4
  Summary: requestmodel
5
- Home-page: https://github.com/foarsitter/requestmodel
6
5
  License: MIT
6
+ License-File: LICENSE
7
7
  Author: Jelmer Draaijer
8
8
  Author-email: info@jelmert.nl
9
- Requires-Python: >=3.8
9
+ Requires-Python: >=3.10
10
10
  Classifier: Development Status :: 4 - Beta
11
11
  Classifier: License :: OSI Approved :: MIT License
12
12
  Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.8
14
- Classifier: Programming Language :: Python :: 3.9
15
13
  Classifier: Programming Language :: Python :: 3.10
16
14
  Classifier: Programming Language :: Python :: 3.11
17
15
  Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
18
19
  Requires-Dist: httpx
19
20
  Requires-Dist: pydantic (>=2.0.0,<3.0.0)
20
21
  Project-URL: Changelog, https://github.com/foarsitter/requestmodel/releases
21
22
  Project-URL: Documentation, https://requestmodel.readthedocs.io
23
+ Project-URL: Homepage, https://github.com/foarsitter/requestmodel
22
24
  Project-URL: Repository, https://github.com/foarsitter/requestmodel
23
25
  Description-Content-Type: text/markdown
24
26
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "requestmodel"
3
- version = "0.6.1"
3
+ version = "0.8.0"
4
4
  description = "requestmodel"
5
5
  authors = ["Jelmer Draaijer <info@jelmert.nl>"]
6
6
  license = "MIT"
@@ -16,19 +16,19 @@ classifiers = [
16
16
  Changelog = "https://github.com/foarsitter/requestmodel/releases"
17
17
 
18
18
  [tool.poetry.dependencies]
19
- python = ">=3.8"
19
+ python = ">=3.10"
20
20
  httpx = "*"
21
21
  pydantic = "^2.0.0"
22
22
 
23
23
  [tool.poetry.group.requests.dependencies]
24
- requests = "^2.31.0"
24
+ requests = ">=2.31.0"
25
25
 
26
26
  [tool.poetry.group.dev.dependencies]
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"
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 = "^0.109.2"
54
+ fastapi = ">=0.109.2"
55
55
 
56
56
  [tool.coverage.paths]
57
57
  source = ["src", "*/site-packages"]
@@ -69,7 +69,6 @@ fail_under = 100
69
69
  [tool.isort]
70
70
  profile = "black"
71
71
  force_single_line = true
72
- lines_after_imports = 2
73
72
 
74
73
  [tool.mypy]
75
74
  plugins = [
@@ -1,5 +1,4 @@
1
1
  from .model import IteratorRequestModel
2
2
  from .model import RequestModel
3
3
 
4
-
5
4
  __all__ = ["RequestModel", "IteratorRequestModel"]
@@ -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.response_model.model_validate(self.response.json())
53
+ return self.adapt_type(self.response)
@@ -46,7 +46,6 @@ from pydantic_core import Url as Url
46
46
  from typing_extensions import get_args
47
47
  from typing_extensions import get_origin
48
48
 
49
-
50
49
  IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any]]
51
50
 
52
51
 
@@ -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
 
@@ -33,7 +40,7 @@ class BaseRequestModel(BaseModel, Generic[ResponseType]):
33
40
  url: ClassVar[str]
34
41
  method: ClassVar[str]
35
42
 
36
- response_model: ClassVar[Type[ResponseType]] # type: ignore[misc]
43
+ response_model: ClassVar[Type[ResponseType]]
37
44
 
38
45
  def get_path_param_names(self) -> Set[str]:
39
46
  return get_path_param_names(self.url)
@@ -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
- if isinstance(self.response_model, TypeAdapter):
102
- return self.response_model.validate_python(self.raw_response.json())
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
- if isinstance(self.response_model, TypeAdapter):
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"""
@@ -0,0 +1,35 @@
1
+ from typing import Any
2
+
3
+ from pydantic.fields import FieldInfo
4
+
5
+
6
+ class Param(FieldInfo): # type: ignore[misc]
7
+ pass
8
+
9
+
10
+ class Body(Param): # type: ignore[misc]
11
+ def __init__(self, embed: bool = False, **kwargs: Any) -> None:
12
+ super().__init__(**kwargs)
13
+ self.embed = embed
14
+
15
+
16
+ class Cookie(Param): # type: ignore[misc]
17
+ pass
18
+
19
+
20
+ class File(Param): # type: ignore[misc]
21
+ pass
22
+
23
+
24
+ class Header(Param): # type: ignore[misc]
25
+ def __init__(self, convert_underscores: bool = True, **kwargs: Any) -> None:
26
+ super().__init__(**kwargs)
27
+ self.convert_underscores = convert_underscores
28
+
29
+
30
+ class Path(Param): # type: ignore[misc]
31
+ pass
32
+
33
+
34
+ class Query(Param): # type: ignore[misc]
35
+ pass
@@ -1,11 +1,16 @@
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
-
10
- ResponseType = TypeVar("ResponseType", bound=BaseModel)
12
+ ResponseType = TypeVar(
13
+ "ResponseType",
14
+ bound=Union[BaseModel, TypeAdapter[List[BaseModel]], List[BaseModel]],
15
+ )
11
16
  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
 
@@ -1,37 +0,0 @@
1
- from pydantic.fields import FieldInfo
2
- from pydantic.fields import _FieldInfoInputs
3
- from typing_extensions import Unpack
4
-
5
-
6
- class Param(FieldInfo):
7
- pass
8
-
9
-
10
- class Body(Param):
11
- def __init__(self, embed: bool = False, **kwargs: Unpack[_FieldInfoInputs]) -> None:
12
- super().__init__(**kwargs)
13
- self.embed = embed
14
-
15
-
16
- class Cookie(Param):
17
- pass
18
-
19
-
20
- class File(Param):
21
- pass
22
-
23
-
24
- class Header(Param):
25
- def __init__(
26
- self, convert_underscores: bool = True, **kwargs: Unpack[_FieldInfoInputs]
27
- ) -> None:
28
- super().__init__(**kwargs)
29
- self.convert_underscores = convert_underscores
30
-
31
-
32
- class Path(Param):
33
- pass
34
-
35
-
36
- class Query(Param):
37
- pass
File without changes
File without changes