fastexec 0.1.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.
- fastexec-0.1.0/LICENSE +21 -0
- fastexec-0.1.0/PKG-INFO +19 -0
- fastexec-0.1.0/README.md +2 -0
- fastexec-0.1.0/fastexec/VERSION +1 -0
- fastexec-0.1.0/fastexec/__init__.py +148 -0
- fastexec-0.1.0/fastexec/version.py +11 -0
- fastexec-0.1.0/pyproject.toml +37 -0
fastexec-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AllenChou
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fastexec-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fastexec
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Execute function with FastAPI features.
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: AllenChou
|
|
7
|
+
Author-email: f1470891079@gmail.com
|
|
8
|
+
Requires-Python: >=3.12,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Provides-Extra: all
|
|
14
|
+
Requires-Dist: fastapi[standard]
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# fastexec
|
|
18
|
+
Execute function with FastAPI features.
|
|
19
|
+
|
fastexec-0.1.0/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import inspect
|
|
3
|
+
import json
|
|
4
|
+
import typing
|
|
5
|
+
from contextlib import AsyncExitStack
|
|
6
|
+
from urllib.parse import urlencode
|
|
7
|
+
|
|
8
|
+
import fastapi
|
|
9
|
+
import fastapi.dependencies.models
|
|
10
|
+
import fastapi.dependencies.utils
|
|
11
|
+
import fastapi.exceptions
|
|
12
|
+
import pydantic
|
|
13
|
+
import starlette.requests
|
|
14
|
+
from starlette.concurrency import run_in_threadpool
|
|
15
|
+
|
|
16
|
+
from fastexec.version import get_version
|
|
17
|
+
|
|
18
|
+
T = typing.TypeVar("T")
|
|
19
|
+
__version__ = get_version()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_coroutine_callable(callable_obj):
|
|
23
|
+
return inspect.iscoroutinefunction(callable_obj)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def call_any_function(func, **kwargs):
|
|
27
|
+
if is_coroutine_callable(func):
|
|
28
|
+
# Async function, just await it
|
|
29
|
+
return await func(**kwargs)
|
|
30
|
+
else:
|
|
31
|
+
# Sync function, run in threadpool
|
|
32
|
+
return await run_in_threadpool(func, **kwargs)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def dict_to_asgi_headers(
|
|
36
|
+
headers: typing.Mapping[typing.Text, typing.Text]
|
|
37
|
+
) -> typing.List[typing.Tuple[bytes, bytes]]:
|
|
38
|
+
return [
|
|
39
|
+
(k.lower().encode("latin1"), v.encode("latin1")) for k, v in headers.items()
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get_dependant(
|
|
44
|
+
*, path: typing.Text = "/", call: typing.Callable
|
|
45
|
+
) -> fastapi.dependencies.models.Dependant:
|
|
46
|
+
return fastapi.dependencies.utils.get_dependant(path=path, call=call)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
async def exec_with_dependant(
|
|
50
|
+
*,
|
|
51
|
+
dependant: fastapi.dependencies.models.Dependant,
|
|
52
|
+
query_params: typing.Optional[typing.Union[typing.Dict, pydantic.BaseModel]] = None,
|
|
53
|
+
headers: typing.Optional[
|
|
54
|
+
typing.Union[typing.Mapping[typing.Text, typing.Text], pydantic.BaseModel]
|
|
55
|
+
] = None,
|
|
56
|
+
body: typing.Optional[typing.Union[typing.Any, pydantic.BaseModel]] = None,
|
|
57
|
+
) -> typing.Any:
|
|
58
|
+
|
|
59
|
+
_query_params: typing.Dict = (
|
|
60
|
+
json.loads(query_params.model_dump_json())
|
|
61
|
+
if isinstance(query_params, pydantic.BaseModel)
|
|
62
|
+
else query_params
|
|
63
|
+
) or {}
|
|
64
|
+
_headers: typing.Mapping[typing.Text, typing.Text] = (
|
|
65
|
+
json.loads(headers.model_dump_json())
|
|
66
|
+
if isinstance(headers, pydantic.BaseModel)
|
|
67
|
+
else headers
|
|
68
|
+
) or {}
|
|
69
|
+
_body = (
|
|
70
|
+
json.loads(body.model_dump_json())
|
|
71
|
+
if isinstance(body, pydantic.BaseModel)
|
|
72
|
+
else body
|
|
73
|
+
) or None
|
|
74
|
+
_body_bytes = json.dumps(_body).encode("utf-8") if _body else b""
|
|
75
|
+
|
|
76
|
+
request = starlette.requests.Request(
|
|
77
|
+
scope={
|
|
78
|
+
"type": "http",
|
|
79
|
+
"method": "POST",
|
|
80
|
+
"path": "/fastexec",
|
|
81
|
+
"query_string": urlencode(_query_params).encode("utf-8"),
|
|
82
|
+
"headers": dict_to_asgi_headers(_headers),
|
|
83
|
+
"client": ("127.0.0.1", 8000),
|
|
84
|
+
"content-length": str(len(_body_bytes)).encode("utf-8"),
|
|
85
|
+
},
|
|
86
|
+
receive=lambda: asyncio.Future(),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Mock the receive function to return the body
|
|
90
|
+
async def mock_receive():
|
|
91
|
+
return {"type": "http.request", "body": _body_bytes, "more_body": False}
|
|
92
|
+
|
|
93
|
+
request._receive = mock_receive
|
|
94
|
+
|
|
95
|
+
async with AsyncExitStack() as stack:
|
|
96
|
+
solved = await fastapi.dependencies.utils.solve_dependencies(
|
|
97
|
+
request=request,
|
|
98
|
+
dependant=dependant,
|
|
99
|
+
body=_body if isinstance(_body, typing.Dict) else None,
|
|
100
|
+
async_exit_stack=stack, # Required
|
|
101
|
+
embed_body_fields=(
|
|
102
|
+
False if isinstance(_body, typing.Dict) else True
|
|
103
|
+
), # Required (usually False for non-JSON bodies)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# If there were no errors, get the final function’s return by calling the function
|
|
107
|
+
# with the solved dependency values:
|
|
108
|
+
if solved.errors:
|
|
109
|
+
raise fastapi.exceptions.HTTPException(
|
|
110
|
+
status_code=fastapi.status.HTTP_400_BAD_REQUEST, detail=str(solved.errors)
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
# For async functions:
|
|
114
|
+
final_result = await call_any_function(dependant.call, **solved.values)
|
|
115
|
+
return final_result
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class FastExec(typing.Generic[T]):
|
|
119
|
+
def __init__(
|
|
120
|
+
self,
|
|
121
|
+
call: typing.Union[
|
|
122
|
+
typing.Callable[..., T],
|
|
123
|
+
typing.Callable[..., typing.Awaitable[T]],
|
|
124
|
+
],
|
|
125
|
+
*args,
|
|
126
|
+
**kwargs,
|
|
127
|
+
):
|
|
128
|
+
self.dependant = get_dependant(call=call)
|
|
129
|
+
|
|
130
|
+
async def exec(
|
|
131
|
+
self,
|
|
132
|
+
*,
|
|
133
|
+
query_params: typing.Optional[
|
|
134
|
+
typing.Union[typing.Dict, pydantic.BaseModel]
|
|
135
|
+
] = None,
|
|
136
|
+
headers: typing.Optional[
|
|
137
|
+
typing.Union[typing.Mapping[typing.Text, typing.Text], pydantic.BaseModel]
|
|
138
|
+
] = None,
|
|
139
|
+
body: typing.Optional[typing.Union[typing.Any, pydantic.BaseModel]] = None,
|
|
140
|
+
**kwargs,
|
|
141
|
+
) -> T:
|
|
142
|
+
return await exec_with_dependant(
|
|
143
|
+
dependant=self.dependant,
|
|
144
|
+
query_params=query_params,
|
|
145
|
+
headers=headers,
|
|
146
|
+
body=body,
|
|
147
|
+
**kwargs,
|
|
148
|
+
)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import pathlib
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@functools.lru_cache(maxsize=1)
|
|
7
|
+
def get_version() -> typing.Text:
|
|
8
|
+
ver_txt_file = pathlib.Path(__file__).parent.joinpath("VERSION")
|
|
9
|
+
if not ver_txt_file.is_file():
|
|
10
|
+
raise FileNotFoundError(f"Version file not found: {ver_txt_file}")
|
|
11
|
+
return ver_txt_file.read_text().strip()
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
authors = ["AllenChou <f1470891079@gmail.com>"]
|
|
3
|
+
description = "Execute function with FastAPI features."
|
|
4
|
+
license = "MIT"
|
|
5
|
+
name = "fastexec"
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
fastapi = { extras = ["standard"], version = "*" }
|
|
11
|
+
python = ">=3.12,<4.0"
|
|
12
|
+
|
|
13
|
+
[tool.poetry.extras]
|
|
14
|
+
all = []
|
|
15
|
+
|
|
16
|
+
[tool.poetry.group.dev.dependencies]
|
|
17
|
+
black = { extras = ["jupyter"], version = "*" }
|
|
18
|
+
faker = "*"
|
|
19
|
+
isort = "*"
|
|
20
|
+
poetry-plugin-export = "^1.6.0"
|
|
21
|
+
pytest = "^8"
|
|
22
|
+
pytest-asyncio = "*"
|
|
23
|
+
pytest-cov = "^4"
|
|
24
|
+
pytest-xdist = "^3"
|
|
25
|
+
scipy = "*"
|
|
26
|
+
setuptools = ">=69"
|
|
27
|
+
|
|
28
|
+
[tool.isort]
|
|
29
|
+
profile = "black"
|
|
30
|
+
|
|
31
|
+
[tool.flake8]
|
|
32
|
+
ignore = ["E203", "E704", "W503"]
|
|
33
|
+
max-line-length = 88
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
build-backend = "poetry.core.masonry.api"
|
|
37
|
+
requires = ["poetry-core"]
|