requestmodel 0.2.3__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.
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: requestmodel
3
- Version: 0.2.3
3
+ Version: 0.3.1
4
4
  Summary: requestmodel
5
- Home-page: https://github.com/foarsitter/fastclient
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/fastclient/releases
20
- Project-URL: Documentation, https://fastclient.readthedocs.io
21
- Project-URL: Repository, https://github.com/foarsitter/fastclient
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
- # Fastclient
24
+ # requestmodel
25
25
 
26
26
  [![PyPI](https://img.shields.io/pypi/v/requestmodel.svg)][pypi status]
27
27
  [![Status](https://img.shields.io/pypi/status/requestmodel.svg)][pypi status]
28
28
  [![Python Version](https://img.shields.io/pypi/pyversions/requestmodel)][pypi status]
29
29
  [![License](https://img.shields.io/pypi/l/requestmodel)][license]
30
30
 
31
- [![Read the documentation at https://fastclient.readthedocs.io/](https://img.shields.io/readthedocs/fastclient/latest.svg?label=Read%20the%20Docs)][read the docs]
32
- [![Tests](https://github.com/foarsitter/fastclient/workflows/Tests/badge.svg)][tests]
33
- [![Codecov](https://codecov.io/gh/foarsitter/fastclient/branch/main/graph/badge.svg)][codecov]
31
+ [![Read the documentation at https://requestmodel.readthedocs.io/](https://img.shields.io/readthedocs/requestmodel/latest.svg?label=Read%20the%20Docs)][read the docs]
32
+ [![Tests](https://github.com/foarsitter/requestmodel/workflows/Tests/badge.svg)][tests]
33
+ [![Codecov](https://codecov.io/gh/foarsitter/requestmodel/branch/main/graph/badge.svg)][codecov]
34
34
 
35
35
  [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)][pre-commit]
36
36
  [![Black](https://img.shields.io/badge/code%20style-black-000000.svg)][black]
37
37
 
38
38
  [pypi status]: https://pypi.org/project/requestmodel/
39
- [read the docs]: https://fastclient.readthedocs.io/
40
- [tests]: https://github.com/foarsitter/fastclient/actions?workflow=Tests
41
- [codecov]: https://app.codecov.io/gh/foarsitter/fastclient
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
- - TODO
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
- ## Requirements
51
+ ## Goals
50
52
 
51
- - TODO
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 _Fastclient_ via [pip] from [PyPI]:
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 [Command-line Reference] for details.
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
- _Fastclient_ is free and open source software.
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/fastclient/issues
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/fastclient/blob/main/LICENSE
93
- [contributor guide]: https://github.com/foarsitter/fastclient/blob/main/CONTRIBUTING.md
94
- [command-line reference]: https://fastclient.readthedocs.io/en/latest/usage.html
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
- # Fastclient
1
+ # requestmodel
2
2
 
3
3
  [![PyPI](https://img.shields.io/pypi/v/requestmodel.svg)][pypi status]
4
4
  [![Status](https://img.shields.io/pypi/status/requestmodel.svg)][pypi status]
5
5
  [![Python Version](https://img.shields.io/pypi/pyversions/requestmodel)][pypi status]
6
6
  [![License](https://img.shields.io/pypi/l/requestmodel)][license]
7
7
 
8
- [![Read the documentation at https://fastclient.readthedocs.io/](https://img.shields.io/readthedocs/fastclient/latest.svg?label=Read%20the%20Docs)][read the docs]
9
- [![Tests](https://github.com/foarsitter/fastclient/workflows/Tests/badge.svg)][tests]
10
- [![Codecov](https://codecov.io/gh/foarsitter/fastclient/branch/main/graph/badge.svg)][codecov]
8
+ [![Read the documentation at https://requestmodel.readthedocs.io/](https://img.shields.io/readthedocs/requestmodel/latest.svg?label=Read%20the%20Docs)][read the docs]
9
+ [![Tests](https://github.com/foarsitter/requestmodel/workflows/Tests/badge.svg)][tests]
10
+ [![Codecov](https://codecov.io/gh/foarsitter/requestmodel/branch/main/graph/badge.svg)][codecov]
11
11
 
12
12
  [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)][pre-commit]
13
13
  [![Black](https://img.shields.io/badge/code%20style-black-000000.svg)][black]
14
14
 
15
15
  [pypi status]: https://pypi.org/project/requestmodel/
16
- [read the docs]: https://fastclient.readthedocs.io/
17
- [tests]: https://github.com/foarsitter/fastclient/actions?workflow=Tests
18
- [codecov]: https://app.codecov.io/gh/foarsitter/fastclient
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
- - TODO
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
- ## Requirements
28
+ ## Goals
27
29
 
28
- - TODO
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 _Fastclient_ via [pip] from [PyPI]:
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 [Command-line Reference] for details.
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
- _Fastclient_ is free and open source software.
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/fastclient/issues
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/fastclient/blob/main/LICENSE
70
- [contributor guide]: https://github.com/foarsitter/fastclient/blob/main/CONTRIBUTING.md
71
- [command-line reference]: https://fastclient.readthedocs.io/en/latest/usage.html
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.2.3"
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/fastclient"
9
- repository = "https://github.com/foarsitter/fastclient"
10
- documentation = "https://fastclient.readthedocs.io"
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/fastclient/releases"
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](https://img.shields.io/pypi/v/requestmodel.svg)][pypi status]\n[![Status](https://img.shields.io/pypi/status/requestmodel.svg)][pypi status]\n[![Python Version](https://img.shields.io/pypi/pyversions/requestmodel)][pypi status]\n[![License](https://img.shields.io/pypi/l/requestmodel)][license]\n\n[![Read the documentation at https://requestmodel.readthedocs.io/](https://img.shields.io/readthedocs/requestmodel/latest.svg?label=Read%20the%20Docs)][read the docs]\n[![Tests](https://github.com/foarsitter/requestmodel/workflows/Tests/badge.svg)][tests]\n[![Codecov](https://codecov.io/gh/foarsitter/requestmodel/branch/main/graph/badge.svg)][codecov]\n\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)][pre-commit]\n[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)][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
@@ -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.3',
19
- 'description': 'requestmodel',
20
- 'long_description': "# Fastclient\n\n[![PyPI](https://img.shields.io/pypi/v/requestmodel.svg)][pypi status]\n[![Status](https://img.shields.io/pypi/status/requestmodel.svg)][pypi status]\n[![Python Version](https://img.shields.io/pypi/pyversions/requestmodel)][pypi status]\n[![License](https://img.shields.io/pypi/l/requestmodel)][license]\n\n[![Read the documentation at https://fastclient.readthedocs.io/](https://img.shields.io/readthedocs/fastclient/latest.svg?label=Read%20the%20Docs)][read the docs]\n[![Tests](https://github.com/foarsitter/fastclient/workflows/Tests/badge.svg)][tests]\n[![Codecov](https://codecov.io/gh/foarsitter/fastclient/branch/main/graph/badge.svg)][codecov]\n\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)][pre-commit]\n[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)][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,118 +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
- value = values.get(k, None)
100
-
101
- if not value and getattr(self, k, None) is not None:
102
- value = getattr(self, k)
103
-
104
- if value:
105
- request_args[type(annotated_property)][k] = value
106
-
107
- def send(self, client: httpx.Client) -> ResponseType:
108
- """Send the request synchronously"""
109
- r = self.as_request(client)
110
- response = client.send(r)
111
- response.raise_for_status()
112
- return self.response_model.model_validate(response.json())
113
-
114
- async def asend(self, client: httpx.AsyncClient) -> ResponseType:
115
- """Send the request asynchronously"""
116
- r = self.as_request(client)
117
- response = await client.send(r)
118
- return self.response_model.model_validate(response.json())
File without changes