Skip to content

Commit be0fc4d

Browse files
fix
1 parent 5909b9f commit be0fc4d

File tree

6 files changed

+76
-18
lines changed

6 files changed

+76
-18
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ jobs:
1616
- name: Run tests
1717
run: |
1818
pip install poetry
19-
poetry install --with dev --with api
19+
poetry install --with api --with dev
2020
poetry run pytest
2121

poetry.lock

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pre-commit = "^4.0.1"
3131

3232
[tool.poetry.group.api.dependencies]
3333
rb-api = { path = "src/rb-api", develop = true }
34-
jinja2 = "^3.1.4"
34+
3535

3636
[build-system]
3737
requires = ["poetry-core"]

src/rb-api/poetry.lock

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rb-api/pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ python = "^3.11"
1010
fastapi = "^0.115.4"
1111
uvicorn = "^0.32.0"
1212
jinja2 = "^3.1.4"
13-
13+
makefun = "^1.15.6"
14+
typer = "*"
1415

1516
rescuebox = { path = "../../", develop = true }
1617
rb-lib = { path = "../rb-lib", develop = true }
1718

19+
1820
[build-system]
1921
requires = ["poetry-core"]
2022
build-backend = "poetry.core.masonry.api"

src/rb-api/rb/api/routes/cli.py

+46-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from functools import wraps
2-
from typing import Callable
1+
import inspect
2+
from typing import Callable, Optional
33

4-
from fastapi import APIRouter, Depends, HTTPException
4+
import typer
5+
from fastapi import APIRouter, HTTPException
6+
from makefun import with_signature
57
from rb.api.models import CommandResult
68
from rb.lib.stdout import Capturing # type: ignore
79

@@ -10,10 +12,6 @@
1012
cli_router = APIRouter()
1113

1214

13-
def cli_flags(help: bool = False, stream: bool = False):
14-
return {"help": help, "stream": stream}
15-
16-
1715
def static_endpoint(callback: Callable, *args, **kwargs) -> CommandResult:
1816
with Capturing() as stdout:
1917
try:
@@ -27,18 +25,54 @@ def static_endpoint(callback: Callable, *args, **kwargs) -> CommandResult:
2725
return CommandResult(result=result, stdout=stdout, success=success, error=error)
2826

2927

30-
def command_callback(callback: Callable):
31-
@wraps(callback)
28+
def command_callback(command: typer.models.CommandInfo):
29+
# Get the original callback signature
30+
original_signature = inspect.signature(command.callback)
31+
32+
# Modify the signature to include `streaming` and `help` parameters
33+
new_params = list(original_signature.parameters.values())
34+
new_params.append(
35+
inspect.Parameter(
36+
"streaming",
37+
inspect.Parameter.KEYWORD_ONLY,
38+
default=False,
39+
annotation=Optional[bool],
40+
)
41+
)
42+
new_params.append(
43+
inspect.Parameter(
44+
"help",
45+
inspect.Parameter.KEYWORD_ONLY,
46+
default=False,
47+
annotation=Optional[bool],
48+
)
49+
)
50+
new_signature = original_signature.replace(parameters=new_params)
51+
52+
# Create a new function with the modified signature
53+
@with_signature(new_signature)
3254
def wrapper(*args, **kwargs):
33-
result = static_endpoint(callback, *args, **kwargs)
55+
# Extract additional parameters
56+
help = kwargs.pop("help", False)
57+
58+
if help:
59+
return CommandResult(
60+
result=command.callback.__doc__, stdout=[], success=True, error=None
61+
)
62+
63+
# Call the static endpoint with the wrapped callback and arguments
64+
result = static_endpoint(command.callback, *args, **kwargs)
3465
if not result.success:
35-
# Return the last 10 lines of stdout
66+
# Return the last 10 lines of stdout if there's an error
3667
raise HTTPException(
3768
status_code=400,
3869
detail={"error": result.error, "stdout": result.stdout[-10:]},
3970
)
4071
return result
4172

73+
wrapper.__name__ = command.callback.__name__
74+
wrapper.__doc__ = command.callback.__doc__
75+
4276
return wrapper
4377

4478

@@ -47,10 +81,9 @@ def wrapper(*args, **kwargs):
4781
for command in plugin.typer_instance.registered_commands:
4882
router.add_api_route(
4983
f"/{command.callback.__name__}/",
50-
endpoint=command_callback(command.callback),
84+
endpoint=command_callback(command),
5185
methods=["POST"],
5286
name=command.callback.__name__,
5387
response_model=CommandResult,
54-
dependencies=[Depends(cli_flags)],
5588
)
5689
cli_router.include_router(router, prefix=f"/{plugin.name}", tags=[plugin.name])

0 commit comments

Comments
 (0)