remote_controller

Does FastAPI provide some kind of way of converting a set of args and kwargs into inputs to requests? That is, converting them into the params and json of requests.post and requests.get etc.

I know FastAPI does some clever conversions here, and I’d like to replicate it. For instance, I know that if there is a Pydantic model in the arguments then it automatically means that you put it into json

I basically want a function prepare_requests_args(kwargs) that gives me two dictionaries corresponding to params and json respectively

def example_func(a, b: int, c="default", d: Optional[str]=None):
    pass

result = map_args_with_signature_types(example_func, [10, 20], {"d": [1, 2, 3]})
print(result)
{'a': (None, 10), 'b': (<class 'int'>, 20), 'c': (None, 'default'), 'd': (typing.Optional[str], [1, 2, 3])}
class FooModel(BaseModel):
    name: str
    value: int

def example_func(a, b: int, c: List[int], d: FooModel, e: Dict):
    pass

params, body = prepare_requests_args(
    example_func,
    args=[],
    kwargs={
        'a': 10,
        'b': 20,
        'c': [1, 2, 3],
        'd': FooModel(name="test", value=42),
        'e': {'key1': 'value1', 'key2': 'value2'}
    }
)

print("Params:", params)
print("Body:", body)
Params: {'a': 10, 'b': 20}
Body: {'c': [1, 2, 3], 'd': {'name': 'test', 'value': 42}, 'e': {'key1': 'value1', 'key2': 'value2'}}

RemoteController

Inherits from: Controller


Methods

init

__init__(self, url: str, api_key: Optional[str])

set_url

set_url(self, url: str)

init_subclass

__init_subclass__(
   cls,
   base_controller_cls: Type[Controller],
   prepend_method_group: bool,
   **kwargs
)

create_remote_controller

create_remote_controller(
   base_controller_cls: Type[Controller],
   url: str,
   api_key: Optional[str]
) -> RemoteController

# Check that the argument sets of RemoteController.__init__ and create_remote_controller match
argset1 = set(p.name for p in inspect.signature(RemoteController.__init__).parameters.values())
argset1.remove('self')
argset2 = set(p.name for p in inspect.signature(create_remote_controller).parameters.values())
argset2.remove('base_controller_cls')
assert argset1 == argset2
from ctrlstack import ctrl_cmd_method, ctrl_query_method, ctrl_method

class FooController(Controller):
    @ctrl_cmd_method
    def bar(self):
        pass
    
    @ctrl_query_method
    def baz(self, x: int) -> str:
        pass
    
    @ctrl_method(ControllerMethodType.QUERY, "q")
    def qux(self):
        pass

foo_remote_controller = create_remote_controller(FooController, url="http://localhost:8000")