requestmodel 0.2.2__tar.gz → 0.3.1__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.2.2 → requestmodel-0.3.1}/PKG-INFO +25 -22
- {requestmodel-0.2.2 → requestmodel-0.3.1}/README.md +20 -17
- {requestmodel-0.2.2 → requestmodel-0.3.1}/pyproject.toml +6 -5
- requestmodel-0.3.1/setup.py +34 -0
- requestmodel-0.3.1/src/requestmodel/__init__.py +137 -0
- requestmodel-0.3.1/src/requestmodel/encoders.py +1 -0
- requestmodel-0.3.1/src/requestmodel/params.py +8 -0
- requestmodel-0.2.2/setup.py +0 -34
- requestmodel-0.2.2/src/requestmodel/__init__.py +0 -115
- {requestmodel-0.2.2 → requestmodel-0.3.1}/LICENSE +0 -0
- {requestmodel-0.2.2 → requestmodel-0.3.1}/src/requestmodel/py.typed +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: requestmodel
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: requestmodel
|
|
5
|
-
Home-page: https://github.com/foarsitter/
|
|
5
|
+
Home-page: https://github.com/foarsitter/requestmodel
|
|
6
6
|
License: MIT
|
|
7
7
|
Author: Jelmer Draaijer
|
|
8
8
|
Author-email: info@jelmert.nl
|
|
@@ -16,43 +16,46 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.11
|
|
17
17
|
Requires-Dist: fastapi
|
|
18
18
|
Requires-Dist: httpx
|
|
19
|
-
Project-URL: Changelog, https://github.com/foarsitter/
|
|
20
|
-
Project-URL: Documentation, https://
|
|
21
|
-
Project-URL: Repository, https://github.com/foarsitter/
|
|
19
|
+
Project-URL: Changelog, https://github.com/foarsitter/requestmodel/releases
|
|
20
|
+
Project-URL: Documentation, https://requestmodel.readthedocs.io
|
|
21
|
+
Project-URL: Repository, https://github.com/foarsitter/requestmodel
|
|
22
22
|
Description-Content-Type: text/markdown
|
|
23
23
|
|
|
24
|
-
#
|
|
24
|
+
# requestmodel
|
|
25
25
|
|
|
26
26
|
[][pypi status]
|
|
27
27
|
[][pypi status]
|
|
28
28
|
[][pypi status]
|
|
29
29
|
[][license]
|
|
30
30
|
|
|
31
|
-
[][read the docs]
|
|
32
|
+
[][tests]
|
|
33
|
+
[][codecov]
|
|
34
34
|
|
|
35
35
|
[][pre-commit]
|
|
36
36
|
[][black]
|
|
37
37
|
|
|
38
38
|
[pypi status]: https://pypi.org/project/requestmodel/
|
|
39
|
-
[read the docs]: https://
|
|
40
|
-
[tests]: https://github.com/foarsitter/
|
|
41
|
-
[codecov]: https://app.codecov.io/gh/foarsitter/
|
|
39
|
+
[read the docs]: https://requestmodel.readthedocs.io/
|
|
40
|
+
[tests]: https://github.com/foarsitter/requestmodel/actions?workflow=Tests
|
|
41
|
+
[codecov]: https://app.codecov.io/gh/foarsitter/requestmodel
|
|
42
42
|
[pre-commit]: https://github.com/pre-commit/pre-commit
|
|
43
43
|
[black]: https://github.com/psf/black
|
|
44
44
|
|
|
45
45
|
## Features
|
|
46
46
|
|
|
47
|
-
-
|
|
47
|
+
- Model your http requests as a pydantic model
|
|
48
|
+
- Annotate your request parameters with fastapi params
|
|
49
|
+
- Support voor sync & async requests
|
|
48
50
|
|
|
49
|
-
##
|
|
51
|
+
## Goals
|
|
50
52
|
|
|
51
|
-
-
|
|
53
|
+
- Create a generator for an OpenAPI spec
|
|
54
|
+
- Support all kinds of http requests
|
|
52
55
|
|
|
53
56
|
## Installation
|
|
54
57
|
|
|
55
|
-
You can install
|
|
58
|
+
You can install _requestmodel_ via [pip] from [PyPI]:
|
|
56
59
|
|
|
57
60
|
```console
|
|
58
61
|
$ pip install requestmodel
|
|
@@ -60,7 +63,7 @@ $ pip install requestmodel
|
|
|
60
63
|
|
|
61
64
|
## Usage
|
|
62
65
|
|
|
63
|
-
Please see the [
|
|
66
|
+
Please see the [implementation detail] for further instructions.
|
|
64
67
|
|
|
65
68
|
## Contributing
|
|
66
69
|
|
|
@@ -70,7 +73,7 @@ To learn more, see the [Contributor Guide].
|
|
|
70
73
|
## License
|
|
71
74
|
|
|
72
75
|
Distributed under the terms of the [MIT license][license],
|
|
73
|
-
|
|
76
|
+
_requestmodel_ is free and open source software.
|
|
74
77
|
|
|
75
78
|
## Issues
|
|
76
79
|
|
|
@@ -84,12 +87,12 @@ This project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter]
|
|
|
84
87
|
[@cjolowicz]: https://github.com/cjolowicz
|
|
85
88
|
[pypi]: https://pypi.org/
|
|
86
89
|
[hypermodern python cookiecutter]: https://github.com/cjolowicz/cookiecutter-hypermodern-python
|
|
87
|
-
[file an issue]: https://github.com/foarsitter/
|
|
90
|
+
[file an issue]: https://github.com/foarsitter/requestmodel/issues
|
|
88
91
|
[pip]: https://pip.pypa.io/
|
|
89
92
|
|
|
90
93
|
<!-- github-only -->
|
|
91
94
|
|
|
92
|
-
[license]: https://github.com/foarsitter/
|
|
93
|
-
[contributor guide]: https://github.com/foarsitter/
|
|
94
|
-
[
|
|
95
|
+
[license]: https://github.com/foarsitter/requestmodel/blob/main/LICENSE
|
|
96
|
+
[contributor guide]: https://github.com/foarsitter/requestmodel/blob/main/CONTRIBUTING.md
|
|
97
|
+
[implementation detail]: https://requestmodel.readthedocs.io/en/latest/usage.html
|
|
95
98
|
|
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# requestmodel
|
|
2
2
|
|
|
3
3
|
[][pypi status]
|
|
4
4
|
[][pypi status]
|
|
5
5
|
[][pypi status]
|
|
6
6
|
[][license]
|
|
7
7
|
|
|
8
|
-
[][read the docs]
|
|
9
|
+
[][tests]
|
|
10
|
+
[][codecov]
|
|
11
11
|
|
|
12
12
|
[][pre-commit]
|
|
13
13
|
[][black]
|
|
14
14
|
|
|
15
15
|
[pypi status]: https://pypi.org/project/requestmodel/
|
|
16
|
-
[read the docs]: https://
|
|
17
|
-
[tests]: https://github.com/foarsitter/
|
|
18
|
-
[codecov]: https://app.codecov.io/gh/foarsitter/
|
|
16
|
+
[read the docs]: https://requestmodel.readthedocs.io/
|
|
17
|
+
[tests]: https://github.com/foarsitter/requestmodel/actions?workflow=Tests
|
|
18
|
+
[codecov]: https://app.codecov.io/gh/foarsitter/requestmodel
|
|
19
19
|
[pre-commit]: https://github.com/pre-commit/pre-commit
|
|
20
20
|
[black]: https://github.com/psf/black
|
|
21
21
|
|
|
22
22
|
## Features
|
|
23
23
|
|
|
24
|
-
-
|
|
24
|
+
- Model your http requests as a pydantic model
|
|
25
|
+
- Annotate your request parameters with fastapi params
|
|
26
|
+
- Support voor sync & async requests
|
|
25
27
|
|
|
26
|
-
##
|
|
28
|
+
## Goals
|
|
27
29
|
|
|
28
|
-
-
|
|
30
|
+
- Create a generator for an OpenAPI spec
|
|
31
|
+
- Support all kinds of http requests
|
|
29
32
|
|
|
30
33
|
## Installation
|
|
31
34
|
|
|
32
|
-
You can install
|
|
35
|
+
You can install _requestmodel_ via [pip] from [PyPI]:
|
|
33
36
|
|
|
34
37
|
```console
|
|
35
38
|
$ pip install requestmodel
|
|
@@ -37,7 +40,7 @@ $ pip install requestmodel
|
|
|
37
40
|
|
|
38
41
|
## Usage
|
|
39
42
|
|
|
40
|
-
Please see the [
|
|
43
|
+
Please see the [implementation detail] for further instructions.
|
|
41
44
|
|
|
42
45
|
## Contributing
|
|
43
46
|
|
|
@@ -47,7 +50,7 @@ To learn more, see the [Contributor Guide].
|
|
|
47
50
|
## License
|
|
48
51
|
|
|
49
52
|
Distributed under the terms of the [MIT license][license],
|
|
50
|
-
|
|
53
|
+
_requestmodel_ is free and open source software.
|
|
51
54
|
|
|
52
55
|
## Issues
|
|
53
56
|
|
|
@@ -61,11 +64,11 @@ This project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter]
|
|
|
61
64
|
[@cjolowicz]: https://github.com/cjolowicz
|
|
62
65
|
[pypi]: https://pypi.org/
|
|
63
66
|
[hypermodern python cookiecutter]: https://github.com/cjolowicz/cookiecutter-hypermodern-python
|
|
64
|
-
[file an issue]: https://github.com/foarsitter/
|
|
67
|
+
[file an issue]: https://github.com/foarsitter/requestmodel/issues
|
|
65
68
|
[pip]: https://pip.pypa.io/
|
|
66
69
|
|
|
67
70
|
<!-- github-only -->
|
|
68
71
|
|
|
69
|
-
[license]: https://github.com/foarsitter/
|
|
70
|
-
[contributor guide]: https://github.com/foarsitter/
|
|
71
|
-
[
|
|
72
|
+
[license]: https://github.com/foarsitter/requestmodel/blob/main/LICENSE
|
|
73
|
+
[contributor guide]: https://github.com/foarsitter/requestmodel/blob/main/CONTRIBUTING.md
|
|
74
|
+
[implementation detail]: https://requestmodel.readthedocs.io/en/latest/usage.html
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "requestmodel"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.1"
|
|
4
4
|
description = "requestmodel"
|
|
5
5
|
authors = ["Jelmer Draaijer <info@jelmert.nl>"]
|
|
6
6
|
license = "MIT"
|
|
7
7
|
readme = "README.md"
|
|
8
|
-
homepage = "https://github.com/foarsitter/
|
|
9
|
-
repository = "https://github.com/foarsitter/
|
|
10
|
-
documentation = "https://
|
|
8
|
+
homepage = "https://github.com/foarsitter/requestmodel"
|
|
9
|
+
repository = "https://github.com/foarsitter/requestmodel"
|
|
10
|
+
documentation = "https://requestmodel.readthedocs.io"
|
|
11
11
|
classifiers = [
|
|
12
12
|
"Development Status :: 4 - Beta",
|
|
13
13
|
]
|
|
14
14
|
|
|
15
15
|
[tool.poetry.urls]
|
|
16
|
-
Changelog = "https://github.com/foarsitter/
|
|
16
|
+
Changelog = "https://github.com/foarsitter/requestmodel/releases"
|
|
17
17
|
|
|
18
18
|
[tool.poetry.dependencies]
|
|
19
19
|
python = ">=3.8"
|
|
@@ -46,6 +46,7 @@ myst-parser = {version = ">=0.16.1"}
|
|
|
46
46
|
|
|
47
47
|
[tool.poetry.group.dev.dependencies]
|
|
48
48
|
pytest-asyncio = "^0.21.1"
|
|
49
|
+
python-multipart = "^0.0.6"
|
|
49
50
|
|
|
50
51
|
[tool.coverage.paths]
|
|
51
52
|
source = ["src", "*/site-packages"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from setuptools import setup
|
|
3
|
+
|
|
4
|
+
package_dir = \
|
|
5
|
+
{'': 'src'}
|
|
6
|
+
|
|
7
|
+
packages = \
|
|
8
|
+
['requestmodel']
|
|
9
|
+
|
|
10
|
+
package_data = \
|
|
11
|
+
{'': ['*']}
|
|
12
|
+
|
|
13
|
+
install_requires = \
|
|
14
|
+
['fastapi', 'httpx']
|
|
15
|
+
|
|
16
|
+
setup_kwargs = {
|
|
17
|
+
'name': 'requestmodel',
|
|
18
|
+
'version': '0.3.1',
|
|
19
|
+
'description': 'requestmodel',
|
|
20
|
+
'long_description': "# requestmodel\n\n[][pypi status]\n[][pypi status]\n[][pypi status]\n[][license]\n\n[][read the docs]\n[][tests]\n[][codecov]\n\n[][pre-commit]\n[][black]\n\n[pypi status]: https://pypi.org/project/requestmodel/\n[read the docs]: https://requestmodel.readthedocs.io/\n[tests]: https://github.com/foarsitter/requestmodel/actions?workflow=Tests\n[codecov]: https://app.codecov.io/gh/foarsitter/requestmodel\n[pre-commit]: https://github.com/pre-commit/pre-commit\n[black]: https://github.com/psf/black\n\n## Features\n\n- Model your http requests as a pydantic model\n- Annotate your request parameters with fastapi params\n- Support voor sync & async requests\n\n## Goals\n\n- Create a generator for an OpenAPI spec\n- Support all kinds of http requests\n\n## Installation\n\nYou can install _requestmodel_ via [pip] from [PyPI]:\n\n```console\n$ pip install requestmodel\n```\n\n## Usage\n\nPlease see the [implementation detail] for further instructions.\n\n## Contributing\n\nContributions are very welcome.\nTo learn more, see the [Contributor Guide].\n\n## License\n\nDistributed under the terms of the [MIT license][license],\n_requestmodel_ is free and open source software.\n\n## Issues\n\nIf you encounter any problems,\nplease [file an issue] along with a detailed description.\n\n## Credits\n\nThis project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter] template.\n\n[@cjolowicz]: https://github.com/cjolowicz\n[pypi]: https://pypi.org/\n[hypermodern python cookiecutter]: https://github.com/cjolowicz/cookiecutter-hypermodern-python\n[file an issue]: https://github.com/foarsitter/requestmodel/issues\n[pip]: https://pip.pypa.io/\n\n<!-- github-only -->\n\n[license]: https://github.com/foarsitter/requestmodel/blob/main/LICENSE\n[contributor guide]: https://github.com/foarsitter/requestmodel/blob/main/CONTRIBUTING.md\n[implementation detail]: https://requestmodel.readthedocs.io/en/latest/usage.html\n",
|
|
21
|
+
'author': 'Jelmer Draaijer',
|
|
22
|
+
'author_email': 'info@jelmert.nl',
|
|
23
|
+
'maintainer': 'None',
|
|
24
|
+
'maintainer_email': 'None',
|
|
25
|
+
'url': 'https://github.com/foarsitter/requestmodel',
|
|
26
|
+
'package_dir': package_dir,
|
|
27
|
+
'packages': packages,
|
|
28
|
+
'package_data': package_data,
|
|
29
|
+
'install_requires': install_requires,
|
|
30
|
+
'python_requires': '>=3.8',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
setup(**setup_kwargs)
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from typing import ClassVar
|
|
3
|
+
from typing import Dict
|
|
4
|
+
from typing import Generic
|
|
5
|
+
from typing import Iterator
|
|
6
|
+
from typing import Optional
|
|
7
|
+
from typing import Type
|
|
8
|
+
from typing import TypeVar
|
|
9
|
+
|
|
10
|
+
from httpx import AsyncClient
|
|
11
|
+
from httpx import Client
|
|
12
|
+
from httpx import Request
|
|
13
|
+
from httpx._client import BaseClient
|
|
14
|
+
from pydantic import BaseModel
|
|
15
|
+
from pydantic import ConfigDict
|
|
16
|
+
from pydantic.fields import FieldInfo
|
|
17
|
+
from typing_extensions import get_type_hints
|
|
18
|
+
from typing_extensions import override
|
|
19
|
+
|
|
20
|
+
from requestmodel import params
|
|
21
|
+
|
|
22
|
+
from .encoders import jsonable_encoder
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
ResponseType = TypeVar("ResponseType", bound=BaseModel)
|
|
26
|
+
|
|
27
|
+
RequestArgs = Dict[Type[FieldInfo], Dict[str, Any]]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class RequestModel(BaseModel, Generic[ResponseType]):
|
|
31
|
+
"""Declarative way to define a model"""
|
|
32
|
+
|
|
33
|
+
model_config = ConfigDict(populate_by_name=True)
|
|
34
|
+
url: ClassVar[str]
|
|
35
|
+
method: ClassVar[str]
|
|
36
|
+
|
|
37
|
+
response_model: ClassVar[Type[ResponseType]] # type: ignore[misc]
|
|
38
|
+
|
|
39
|
+
body: Optional[ResponseType] = None
|
|
40
|
+
|
|
41
|
+
def get_annotated_type(self, variable_key: str, variable_type: Any) -> FieldInfo:
|
|
42
|
+
if hasattr(variable_type, "__metadata__"):
|
|
43
|
+
annotated_property = variable_type.__metadata__[0]
|
|
44
|
+
# when a key is present in the url we annotate it as a path parameter
|
|
45
|
+
elif f"{{{variable_key}}}" in self.url:
|
|
46
|
+
annotated_property = params.Path()
|
|
47
|
+
else:
|
|
48
|
+
annotated_property = params.Query()
|
|
49
|
+
return annotated_property
|
|
50
|
+
|
|
51
|
+
def as_request(self, client: BaseClient) -> Request:
|
|
52
|
+
"""Transform the properties of the object into a request"""
|
|
53
|
+
|
|
54
|
+
request_args: RequestArgs = {
|
|
55
|
+
params.Query: {},
|
|
56
|
+
params.Path: {},
|
|
57
|
+
params.Cookie: {},
|
|
58
|
+
params.Header: {},
|
|
59
|
+
params.File: {},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
self.request_args_for_values(request_args)
|
|
63
|
+
|
|
64
|
+
_params = request_args[params.Query]
|
|
65
|
+
headers = request_args[params.Header]
|
|
66
|
+
cookies = request_args[params.Cookie]
|
|
67
|
+
files = request_args[params.File]
|
|
68
|
+
|
|
69
|
+
body = jsonable_encoder(self.body) if self.body else None
|
|
70
|
+
|
|
71
|
+
headers["accept"] = "application/json"
|
|
72
|
+
|
|
73
|
+
r = Request(
|
|
74
|
+
method=self.method,
|
|
75
|
+
url=client._merge_url(self.url.format(**request_args[params.Path])),
|
|
76
|
+
params=_params,
|
|
77
|
+
headers=headers,
|
|
78
|
+
cookies=cookies,
|
|
79
|
+
files=files,
|
|
80
|
+
json=body,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return r
|
|
84
|
+
|
|
85
|
+
def request_args_for_values(self, request_args: RequestArgs) -> None:
|
|
86
|
+
# we exclude unset properties from the request
|
|
87
|
+
values = jsonable_encoder(self, exclude_unset=True)
|
|
88
|
+
|
|
89
|
+
skip_properties = ["url", "method", "response_model", "body"]
|
|
90
|
+
|
|
91
|
+
for k, v in get_type_hints(self.__class__, include_extras=True).items():
|
|
92
|
+
if k in skip_properties:
|
|
93
|
+
continue
|
|
94
|
+
|
|
95
|
+
annotated_property = self.get_annotated_type(k, v)
|
|
96
|
+
|
|
97
|
+
value = values.get(k, None)
|
|
98
|
+
|
|
99
|
+
# if we do not have a value but the property is set other than None
|
|
100
|
+
if not value and getattr(self, k, None) is not None:
|
|
101
|
+
value = jsonable_encoder(getattr(self, k))
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
isinstance(annotated_property, params.Header)
|
|
105
|
+
and annotated_property.convert_underscores
|
|
106
|
+
):
|
|
107
|
+
k = k.replace("_", "-")
|
|
108
|
+
|
|
109
|
+
if value is not None:
|
|
110
|
+
request_args[type(annotated_property)][k] = value
|
|
111
|
+
|
|
112
|
+
def send(self, client: Client) -> ResponseType:
|
|
113
|
+
"""Send the request synchronously"""
|
|
114
|
+
r = self.as_request(client)
|
|
115
|
+
response = client.send(r)
|
|
116
|
+
response.raise_for_status()
|
|
117
|
+
return self.response_model.model_validate(response.json())
|
|
118
|
+
|
|
119
|
+
async def asend(self, client: AsyncClient) -> ResponseType:
|
|
120
|
+
"""Send the request asynchronously"""
|
|
121
|
+
r = self.as_request(client)
|
|
122
|
+
response = await client.send(r)
|
|
123
|
+
return self.response_model.model_validate(response.json())
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class IteratorRequestModel(RequestModel[ResponseType]):
|
|
127
|
+
def next(self, response: ResponseType) -> bool: # pragma: no cover
|
|
128
|
+
raise NotImplementedError
|
|
129
|
+
|
|
130
|
+
@override
|
|
131
|
+
def send(self, client: Client) -> Iterator[ResponseType]: # type: ignore[override]
|
|
132
|
+
response = super().send(client)
|
|
133
|
+
yield response
|
|
134
|
+
|
|
135
|
+
while self.next(response):
|
|
136
|
+
response = super().send(client)
|
|
137
|
+
yield response
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from fastapi.encoders import jsonable_encoder as jsonable_encoder # noqa
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""In the future it may be possible to implement our own params,
|
|
2
|
+
but for now we use the ones from FastAPI"""
|
|
3
|
+
from fastapi.params import Body as Body # noqa
|
|
4
|
+
from fastapi.params import Cookie as Cookie # noqa
|
|
5
|
+
from fastapi.params import File as File # noqa
|
|
6
|
+
from fastapi.params import Header as Header # noqa
|
|
7
|
+
from fastapi.params import Path as Path # noqa
|
|
8
|
+
from fastapi.params import Query as Query # noqa
|
requestmodel-0.2.2/setup.py
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from setuptools import setup
|
|
3
|
-
|
|
4
|
-
package_dir = \
|
|
5
|
-
{'': 'src'}
|
|
6
|
-
|
|
7
|
-
packages = \
|
|
8
|
-
['requestmodel']
|
|
9
|
-
|
|
10
|
-
package_data = \
|
|
11
|
-
{'': ['*']}
|
|
12
|
-
|
|
13
|
-
install_requires = \
|
|
14
|
-
['fastapi', 'httpx']
|
|
15
|
-
|
|
16
|
-
setup_kwargs = {
|
|
17
|
-
'name': 'requestmodel',
|
|
18
|
-
'version': '0.2.2',
|
|
19
|
-
'description': 'requestmodel',
|
|
20
|
-
'long_description': "# Fastclient\n\n[][pypi status]\n[][pypi status]\n[][pypi status]\n[][license]\n\n[][read the docs]\n[][tests]\n[][codecov]\n\n[][pre-commit]\n[][black]\n\n[pypi status]: https://pypi.org/project/requestmodel/\n[read the docs]: https://fastclient.readthedocs.io/\n[tests]: https://github.com/foarsitter/fastclient/actions?workflow=Tests\n[codecov]: https://app.codecov.io/gh/foarsitter/fastclient\n[pre-commit]: https://github.com/pre-commit/pre-commit\n[black]: https://github.com/psf/black\n\n## Features\n\n- TODO\n\n## Requirements\n\n- TODO\n\n## Installation\n\nYou can install _Fastclient_ via [pip] from [PyPI]:\n\n```console\n$ pip install requestmodel\n```\n\n## Usage\n\nPlease see the [Command-line Reference] for details.\n\n## Contributing\n\nContributions are very welcome.\nTo learn more, see the [Contributor Guide].\n\n## License\n\nDistributed under the terms of the [MIT license][license],\n_Fastclient_ is free and open source software.\n\n## Issues\n\nIf you encounter any problems,\nplease [file an issue] along with a detailed description.\n\n## Credits\n\nThis project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter] template.\n\n[@cjolowicz]: https://github.com/cjolowicz\n[pypi]: https://pypi.org/\n[hypermodern python cookiecutter]: https://github.com/cjolowicz/cookiecutter-hypermodern-python\n[file an issue]: https://github.com/foarsitter/fastclient/issues\n[pip]: https://pip.pypa.io/\n\n<!-- github-only -->\n\n[license]: https://github.com/foarsitter/fastclient/blob/main/LICENSE\n[contributor guide]: https://github.com/foarsitter/fastclient/blob/main/CONTRIBUTING.md\n[command-line reference]: https://fastclient.readthedocs.io/en/latest/usage.html\n",
|
|
21
|
-
'author': 'Jelmer Draaijer',
|
|
22
|
-
'author_email': 'info@jelmert.nl',
|
|
23
|
-
'maintainer': 'None',
|
|
24
|
-
'maintainer_email': 'None',
|
|
25
|
-
'url': 'https://github.com/foarsitter/fastclient',
|
|
26
|
-
'package_dir': package_dir,
|
|
27
|
-
'packages': packages,
|
|
28
|
-
'package_data': package_data,
|
|
29
|
-
'install_requires': install_requires,
|
|
30
|
-
'python_requires': '>=3.8',
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
setup(**setup_kwargs)
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
from collections import defaultdict
|
|
2
|
-
from typing import Any
|
|
3
|
-
from typing import ClassVar
|
|
4
|
-
from typing import Dict
|
|
5
|
-
from typing import Generic
|
|
6
|
-
from typing import Optional
|
|
7
|
-
from typing import Type
|
|
8
|
-
from typing import TypeVar
|
|
9
|
-
|
|
10
|
-
import fastapi
|
|
11
|
-
import httpx
|
|
12
|
-
from fastapi import params
|
|
13
|
-
from fastapi.encoders import jsonable_encoder
|
|
14
|
-
from httpx import Request
|
|
15
|
-
from httpx._client import BaseClient
|
|
16
|
-
from pydantic import BaseModel
|
|
17
|
-
from pydantic import ConfigDict
|
|
18
|
-
from pydantic.fields import FieldInfo
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
ResponseType = TypeVar("ResponseType", bound=BaseModel)
|
|
22
|
-
|
|
23
|
-
RequestArgs = Dict[Type[FieldInfo], Dict[str, Any]]
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
class RequestModel(BaseModel, Generic[ResponseType]):
|
|
27
|
-
"""Declarative way to define a model"""
|
|
28
|
-
|
|
29
|
-
model_config = ConfigDict(populate_by_name=True)
|
|
30
|
-
url: ClassVar[str]
|
|
31
|
-
method: ClassVar[str]
|
|
32
|
-
|
|
33
|
-
response_model: ClassVar[Type[ResponseType]] # type: ignore[misc]
|
|
34
|
-
|
|
35
|
-
body: Optional[ResponseType] = None
|
|
36
|
-
|
|
37
|
-
def as_request(self, client: BaseClient) -> Request:
|
|
38
|
-
"""Transform the properties of the object into a request"""
|
|
39
|
-
|
|
40
|
-
request_args: RequestArgs = defaultdict(dict)
|
|
41
|
-
|
|
42
|
-
# we exclude unset properties from the request
|
|
43
|
-
values = self.model_dump(exclude_unset=True)
|
|
44
|
-
|
|
45
|
-
self.request_args_for_values(request_args, values)
|
|
46
|
-
|
|
47
|
-
_params = (
|
|
48
|
-
jsonable_encoder(request_args[params.Query], exclude_unset=True)
|
|
49
|
-
if request_args[params.Query]
|
|
50
|
-
else None
|
|
51
|
-
)
|
|
52
|
-
headers = (
|
|
53
|
-
jsonable_encoder(request_args[params.Header])
|
|
54
|
-
if request_args[params.Header]
|
|
55
|
-
else {}
|
|
56
|
-
)
|
|
57
|
-
cookies = (
|
|
58
|
-
jsonable_encoder(request_args[params.Cookie])
|
|
59
|
-
if request_args[params.Cookie]
|
|
60
|
-
else None
|
|
61
|
-
)
|
|
62
|
-
files = (
|
|
63
|
-
jsonable_encoder(request_args[params.File])
|
|
64
|
-
if request_args[params.File]
|
|
65
|
-
else None
|
|
66
|
-
)
|
|
67
|
-
body = jsonable_encoder(self.body) if self.body else None
|
|
68
|
-
|
|
69
|
-
headers["accept"] = "application/json"
|
|
70
|
-
|
|
71
|
-
r = Request(
|
|
72
|
-
method=self.method,
|
|
73
|
-
url=client._merge_url(self.url.format(**request_args[params.Path])),
|
|
74
|
-
params=_params,
|
|
75
|
-
headers=headers,
|
|
76
|
-
cookies=cookies,
|
|
77
|
-
files=files,
|
|
78
|
-
json=body,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
return r
|
|
82
|
-
|
|
83
|
-
def request_args_for_values(
|
|
84
|
-
self, request_args: RequestArgs, values: Dict[str, Any]
|
|
85
|
-
) -> None:
|
|
86
|
-
|
|
87
|
-
skip_properties = ["url", "method", "response_model", "body"]
|
|
88
|
-
|
|
89
|
-
for k, v in self.__annotations__.items():
|
|
90
|
-
|
|
91
|
-
if k in skip_properties:
|
|
92
|
-
continue
|
|
93
|
-
|
|
94
|
-
if hasattr(v, "__metadata__"):
|
|
95
|
-
annotated_property = v.__metadata__[0]
|
|
96
|
-
else:
|
|
97
|
-
annotated_property = fastapi.Query()
|
|
98
|
-
|
|
99
|
-
if k in values:
|
|
100
|
-
attr = values[k]
|
|
101
|
-
|
|
102
|
-
request_args[type(annotated_property)][k] = attr
|
|
103
|
-
|
|
104
|
-
def send(self, client: httpx.Client) -> ResponseType:
|
|
105
|
-
"""Send the request synchronously"""
|
|
106
|
-
r = self.as_request(client)
|
|
107
|
-
response = client.send(r)
|
|
108
|
-
response.raise_for_status()
|
|
109
|
-
return self.response_model.model_validate(response.json())
|
|
110
|
-
|
|
111
|
-
async def asend(self, client: httpx.AsyncClient) -> ResponseType:
|
|
112
|
-
"""Send the request asynchronously"""
|
|
113
|
-
r = self.as_request(client)
|
|
114
|
-
response = await client.send(r)
|
|
115
|
-
return self.response_model.model_validate(response.json())
|
|
File without changes
|
|
File without changes
|