1
1
from functools import wraps
2
2
from typing import Callable
3
3
4
- from fastapi import APIRouter , HTTPException
4
+ from fastapi import APIRouter , Depends , HTTPException
5
5
from rb .api .models import CommandResult
6
6
from rb .lib .stdout import Capturing # type: ignore
7
7
10
10
cli_router = APIRouter ()
11
11
12
12
13
- def safe_endpoint (callback : Callable , * args , ** kwargs ) -> CommandResult :
13
+ def cli_flags (help : bool = False , stream : bool = False ):
14
+ return {"help" : help , "stream" : stream }
15
+
16
+
17
+ def static_endpoint (callback : Callable , * args , ** kwargs ) -> CommandResult :
14
18
with Capturing () as stdout :
15
19
try :
16
20
result = callback (* args , ** kwargs )
@@ -26,7 +30,7 @@ def safe_endpoint(callback: Callable, *args, **kwargs) -> CommandResult:
26
30
def command_callback (callback : Callable ):
27
31
@wraps (callback )
28
32
def wrapper (* args , ** kwargs ):
29
- result = safe_endpoint (callback , * args , ** kwargs )
33
+ result = static_endpoint (callback , * args , ** kwargs )
30
34
if not result .success :
31
35
# Return the last 10 lines of stdout
32
36
raise HTTPException (
@@ -47,5 +51,6 @@ def wrapper(*args, **kwargs):
47
51
methods = ["POST" ],
48
52
name = command .callback .__name__ ,
49
53
response_model = CommandResult ,
54
+ dependencies = [Depends (cli_flags )],
50
55
)
51
56
cli_router .include_router (router , prefix = f"/{ plugin .name } " , tags = [plugin .name ])
0 commit comments