RESTFly 2.0.3__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.
restfly/_utils.py ADDED
@@ -0,0 +1,105 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any, get_origin, get_type_hints, overload
4
+
5
+ from httpx import Response
6
+ from pydantic import BaseModel, TypeAdapter
7
+ from pydantic_xml import BaseXmlModel
8
+
9
+ from .types import Model, XMLModel
10
+
11
+ if TYPE_CHECKING:
12
+ from ._async import AsyncAPIClient
13
+ from ._sync import APIClient
14
+
15
+
16
+ @overload
17
+ def unmarshal(
18
+ response: Response,
19
+ *,
20
+ model: type[XMLModel],
21
+ client: APIClient | AsyncAPIClient,
22
+ json_model_kwargs: dict[str, Any] | None = ...,
23
+ xml_model_kwargs: dict[str, Any] | None = ...,
24
+ ) -> XMLModel: ...
25
+
26
+
27
+ @overload
28
+ def unmarshal(
29
+ response: Response,
30
+ *,
31
+ model: type[Model],
32
+ client: APIClient | AsyncAPIClient,
33
+ json_model_kwargs: dict[str, Any] | None = ...,
34
+ xml_model_kwargs: dict[str, Any] | None = ...,
35
+ ) -> Model: ...
36
+
37
+
38
+ @overload
39
+ def unmarshal(
40
+ response: Response,
41
+ *,
42
+ model: type[list[Model]],
43
+ client: APIClient | AsyncAPIClient,
44
+ json_model_kwargs: dict[str, Any] | None = ...,
45
+ xml_model_kwargs: dict[str, Any] | None = ...,
46
+ ) -> list[Model]: ...
47
+
48
+
49
+ def unmarshal(
50
+ response: Response,
51
+ *,
52
+ model: type[Model | XMLModel | list[Model]],
53
+ client: APIClient | AsyncAPIClient,
54
+ json_model_kwargs: dict[str, Any] | None = None,
55
+ xml_model_kwargs: dict[str, Any] | None = None,
56
+ ) -> XMLModel | Model | list[Model]:
57
+ # initialize the mutables.
58
+ ctx = {"restfly_client": client}
59
+ json_model_kwargs = {} if json_model_kwargs is None else json_model_kwargs
60
+ json_model_kwargs["context"] = json_model_kwargs.get("context", {}) | ctx
61
+
62
+ xml_model_kwargs = {} if xml_model_kwargs is None else xml_model_kwargs
63
+ xml_model_kwargs["context"] = xml_model_kwargs.get("context", {}) | ctx
64
+
65
+ # If the model has an origin or list, then we will need to wrap it in a type
66
+ # adapter and return the model that way. This allows us to handle things like
67
+ # lists just like how FastAPI allows you to wrap models in list definitions.
68
+ #
69
+ # NOTE: We could likely expand the type adapter to support more use cases, but
70
+ # are purposefully keeping this constrained here.
71
+ if get_origin(model) is list:
72
+ return TypeAdapter(model).validate_json(response.content, **json_model_kwargs)
73
+
74
+ # As Pydantic-XML base-classes the Pydantic BaseModel, we will first check to see
75
+ # if the model passed to us is a Pydantic-XML model. If it is, then unmarshal the
76
+ # XML data.
77
+ elif isinstance(model, type) and issubclass(model, BaseXmlModel):
78
+ return model.from_xml(response.content, **xml_model_kwargs) # ty: ignore[invalid-return-type]
79
+
80
+ # If the model is a Pydantic class, then unmarshal with validate_json.
81
+ elif isinstance(model, type) and issubclass(model, BaseModel):
82
+ return model.model_validate_json(response.content, **json_model_kwargs)
83
+
84
+ # As None of the valid types were discovered, raise a type error and inform the
85
+ # caller that we don't know how to process what was passed.
86
+ raise TypeError(f"model {model} is not a valid type.")
87
+
88
+
89
+ def assign_annotations(obj: Any, base_type: type[Any]) -> None:
90
+ """
91
+ Assigns public attributes that have an annotation of the base_type.
92
+ """
93
+ annotations = get_type_hints(obj.__class__)
94
+ # For each annotation, we want to check to see if the attribute name is prefixed
95
+ # with an underscore (which would denote that it is private) and to check to see
96
+ # if it is of the base type. If both are true, then we will instantiate the
97
+ # annotated public attribute with the annotated type, passing the Any as the
98
+ # only attribute.
99
+ for attr, obj_type in annotations.items():
100
+ if (
101
+ attr[0] != "_"
102
+ and isinstance(obj_type, type)
103
+ and issubclass(obj_type, base_type)
104
+ ):
105
+ setattr(obj, attr, obj_type(obj))
restfly/_version.py ADDED
@@ -0,0 +1 @@
1
+ version = "2.0.3"
restfly/types.py ADDED
@@ -0,0 +1,77 @@
1
+ from typing import Literal, TypeVar
2
+
3
+ from httpx import (
4
+ AsyncBaseTransport,
5
+ AsyncClient,
6
+ BaseTransport,
7
+ Client,
8
+ Request,
9
+ Response,
10
+ codes,
11
+ )
12
+ from httpx import __version__ as HTTPX_VERSION
13
+ from httpx._client import USE_CLIENT_DEFAULT, BaseClient, EventHook, UseClientDefault
14
+ from httpx._config import (
15
+ DEFAULT_LIMITS,
16
+ DEFAULT_MAX_REDIRECTS,
17
+ DEFAULT_TIMEOUT_CONFIG,
18
+ Limits,
19
+ )
20
+ from httpx._types import (
21
+ AuthTypes,
22
+ CertTypes,
23
+ CookieTypes,
24
+ HeaderTypes,
25
+ ProxyTypes,
26
+ RequestContent,
27
+ RequestData,
28
+ RequestExtensions,
29
+ RequestFiles,
30
+ TimeoutTypes,
31
+ )
32
+ from httpx._types import (
33
+ QueryParamTypes as _QueryParamTypes,
34
+ )
35
+ from httpx._urls import URL
36
+ from pydantic import BaseModel
37
+ from pydantic_xml import BaseXmlModel
38
+
39
+ Model = TypeVar("Model", bound=BaseModel)
40
+ XMLModel = TypeVar("XMLModel", bound=BaseXmlModel)
41
+
42
+ HTTPMethods = Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]
43
+ QueryParamTypes = BaseModel | _QueryParamTypes
44
+
45
+ __all__ = [
46
+ "DEFAULT_LIMITS",
47
+ "DEFAULT_MAX_REDIRECTS",
48
+ "DEFAULT_TIMEOUT_CONFIG",
49
+ "HTTPX_VERSION",
50
+ "URL",
51
+ "USE_CLIENT_DEFAULT",
52
+ "AsyncBaseTransport",
53
+ "AsyncClient",
54
+ "AuthTypes",
55
+ "BaseClient",
56
+ "BaseTransport",
57
+ "CertTypes",
58
+ "Client",
59
+ "CookieTypes",
60
+ "EventHook",
61
+ "HTTPMethods",
62
+ "HeaderTypes",
63
+ "Limits",
64
+ "Model",
65
+ "ProxyTypes",
66
+ "QueryParamTypes",
67
+ "Request",
68
+ "RequestContent",
69
+ "RequestData",
70
+ "RequestExtensions",
71
+ "RequestFiles",
72
+ "Response",
73
+ "TimeoutTypes",
74
+ "UseClientDefault",
75
+ "XMLModel",
76
+ "codes",
77
+ ]
@@ -0,0 +1,48 @@
1
+ Metadata-Version: 2.4
2
+ Name: RESTFly
3
+ Version: 2.0.3
4
+ Summary: Write your own API Client libraries quickly.
5
+ Requires-Python: >=3.12
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: httpx>=0.28.1
9
+ Requires-Dist: pydantic>=2.12.5
10
+ Requires-Dist: pydantic-xml>=2.19.0
11
+ Dynamic: license-file
12
+
13
+ ![RESTFly Logo](docs/_static/logo-light-hero.svg)
14
+
15
+ # What is RESTFly?
16
+
17
+ Building API SDKs can be often be difficult. There is often a lot of boilerplate code
18
+ that is reused over and over again between classes, packages, and even libraries.
19
+ RESTFly is designed to try to remove as much of that boilerplate code as possible. The
20
+ library leverages HTTPX and Pydantic in the backend to support making interacting with
21
+ the API endpoints themselves simple, as well as supporting the ability to marshal and
22
+ unmarshal the data between python objects and XML or JSON.
23
+
24
+ Version 2 of RESTFly is a ground-up rewrite in order to support a more modern developer
25
+ experience when working with the library.
26
+
27
+ - Completely rewritten to use HTTPX instead of Requests
28
+ - Support for both synchronous and asynchronous clients.
29
+ - Significantly better typing support, which should make both AI Agents and devs happy.
30
+ - Support for coercion of data to/from Pydantic and Pydantic-XML models.
31
+ - Improved endpoint grafting (no more `@property` methods for each endpoint).
32
+ - Batteries-included error handling and retry logic.
33
+ - Designed to be heavily modified based on your particular needs.
34
+
35
+ # Why RESTFly?
36
+
37
+ After building several SDKs over the years, I wanted something that could abstract away
38
+ a lot of the boilerplate code that I kept having to write over and over. RESTFly
39
+ version 1 ended up being nearly a direct port of what I had built with pyTenable back
40
+ in 2017 and expanded since then. Over time, pyTenable was refactored to itself leverage
41
+ RESTFly, and several other SDKs started taking advantage of RESTFly as well. For
42
+ version 2, I wanted to incorporate a lot more of the modern Python approaches and also
43
+ incorporate a lot of the idioms that I've grown used to with libraries like
44
+ FastAPI, Pydantic, AsyncIO, etc. I also didn't want to have to keep diving into the
45
+ documentation every time I needed something when more and more libraries were supporting
46
+ type hints to aide developers within their SDKs.
47
+
48
+ To learn more, visit the documentation at [restfly.rtfd.io](https://restfly.rtfd.io).
@@ -0,0 +1,15 @@
1
+ restfly/__init__.py,sha256=C84BGVDyvHB3-JBDjtheizb_zs_dZg437gAxW6TST-c,588
2
+ restfly/_async.py,sha256=KLxRAp3wwLI5VXeYY2t68_kt3_uqiDW19mj6F_1aWR8,61256
3
+ restfly/_base.py,sha256=YmIWIcpbLbmRnT-6I7QhF5sOvDU5tQx20tHrpCmXHuM,12626
4
+ restfly/_errors.py,sha256=8wWxiNjASP0i1zH8yupUXg7XIwh6xIEj6457oH4yhak,7606
5
+ restfly/_iterator.py,sha256=3yZ9HexJJRj4aQ5QxQzxqkcIG4zAioqSJDIFkz3loD4,10129
6
+ restfly/_models.py,sha256=qZyhcKwJ0mQy9aLNmQjHWrh3w2IYvYf_--SWeSY8iNM,3438
7
+ restfly/_sync.py,sha256=4asNJMwErxp4gQviwX_8Jwk2fIIVyljdTF4H_SfPPxc,60566
8
+ restfly/_utils.py,sha256=XJKLPgxYQcXuSghUuuRV-JSSeQOapPUCSXwLfP74gJA,3848
9
+ restfly/_version.py,sha256=uHXas6DjpuSTmJj9L-uXefo-GY-AWJGDwacerbP74GA,18
10
+ restfly/types.py,sha256=_B7NdLdUQQlunTDB_53IykVahfALe0-VqcKPsGSUH34,1633
11
+ restfly-2.0.3.dist-info/licenses/LICENSE,sha256=2tyomAIQpyOWHDDpSsSgmzyMnsuN4Qk9MJoFB34Pw9A,1054
12
+ restfly-2.0.3.dist-info/METADATA,sha256=PZGz5VwG24YKkEgMXg9N7aaR5zoJHBX3naQvZNumg8o,2385
13
+ restfly-2.0.3.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
14
+ restfly-2.0.3.dist-info/top_level.txt,sha256=byCbdwNZswCk-e3spQMh0uddEDIUhffKRI334dMQW1M,8
15
+ restfly-2.0.3.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,19 @@
1
+ Copyright 2024 Steven McGrath
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ restfly