1
- from functools import wraps
2
- from typing import Callable
1
+ import inspect
2
+ from typing import Callable , Optional
3
3
4
- from fastapi import APIRouter , Depends , HTTPException
4
+ import typer
5
+ from fastapi import APIRouter , HTTPException
6
+ from makefun import with_signature
5
7
from rb .api .models import CommandResult
6
8
from rb .lib .stdout import Capturing # type: ignore
7
9
10
12
cli_router = APIRouter ()
11
13
12
14
13
- def cli_flags (help : bool = False , stream : bool = False ):
14
- return {"help" : help , "stream" : stream }
15
-
16
-
17
15
def static_endpoint (callback : Callable , * args , ** kwargs ) -> CommandResult :
18
16
with Capturing () as stdout :
19
17
try :
@@ -27,18 +25,54 @@ def static_endpoint(callback: Callable, *args, **kwargs) -> CommandResult:
27
25
return CommandResult (result = result , stdout = stdout , success = success , error = error )
28
26
29
27
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 )
32
54
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 )
34
65
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
36
67
raise HTTPException (
37
68
status_code = 400 ,
38
69
detail = {"error" : result .error , "stdout" : result .stdout [- 10 :]},
39
70
)
40
71
return result
41
72
73
+ wrapper .__name__ = command .callback .__name__
74
+ wrapper .__doc__ = command .callback .__doc__
75
+
42
76
return wrapper
43
77
44
78
@@ -47,10 +81,9 @@ def wrapper(*args, **kwargs):
47
81
for command in plugin .typer_instance .registered_commands :
48
82
router .add_api_route (
49
83
f"/{ command .callback .__name__ } /" ,
50
- endpoint = command_callback (command . callback ),
84
+ endpoint = command_callback (command ),
51
85
methods = ["POST" ],
52
86
name = command .callback .__name__ ,
53
87
response_model = CommandResult ,
54
- dependencies = [Depends (cli_flags )],
55
88
)
56
89
cli_router .include_router (router , prefix = f"/{ plugin .name } " , tags = [plugin .name ])
0 commit comments