I'm developing a simple application with FastAPI.
I need a function to be called as endpoint for a certain route. Everything works just fine with the function's default parameters, but wheels come off the bus as soon as I try to override one of them.
Example. This works just fine:
async def my_function(request=Request, clientname='my_client'):
print(request.method)
print(clientname)
## DO OTHER STUFF...
return SOMETHING
private_router.add_route('/api/my/test/route', my_function, ['GET'])
This returns an error instead:
async def my_function(request=Request, clientname='my_client'):
print(request.method)
print(clientname)
## DO OTHER STUFF...
return SOMETHING
private_router.add_route('/api/my/test/route', my_function(clientname='my_other_client'), ['GET'])
The Error:
INFO: 127.0.0.1:60005 - "GET /api/my/test/route HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
...
...
TypeError: 'coroutine' object is not callable
The only difference is I'm trying to override the clientname
value in my_function
.
It is apparent that this isn't the right syntax but I looked everywhere and I'm just appalled that the documentation about the add_route
method is nowhere to be found.
Is anyone able to point me to the right way to do this supposedly simple thing?
Thanks!
One way is to make a partial application of the function using functools.partial
. As per functools.partial
's documentation:
functools.partial(func, /, *args, **keywords)
Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords. Roughly equivalent to:
def partial(func, /, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = {**keywords, **fkeywords} return func(*args, *fargs, **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc
The partial() is used for partial function application which "freezes" some portion of a function's arguments and/or keywords resulting in a new object with a simplified signature.
Here is the source for the add_route()
method, as well as the part in Route
class where Starlette checks if the endpoint_handler
that is passed to add_route()
is an instance of functools.partial
.
Note that the endpoint has to return an instance of Response
/JSONResponse
/etc., as returning a str
or dict
object (e.g., return client_name
), for instance, would throw TypeError: 'str' object is not callable
or TypeError: 'dict' object is not callable
, respectively. Please have a look at this answer for more details and examples on how to return JSON data using a custom Response
.
from fastapi import FastAPI, Request, APIRouter, Response
from functools import partial
async def my_endpoint(request: Request, client_name: str ='my_client'):
print(request.method)
return Response(client_name)
app = FastAPI()
router = APIRouter()
router.add_route('/', partial(my_endpoint, client_name='my_other_client'), ['GET'])
app.include_router(router)
As noted by @MatsLindh in the comments section, you could use a wrapper/helper function that returns an inner function, which is essentially the same as using functools.partial
in Option 1, as that is exactly how that function works under the hood (as shown in the quote block earlier). Hence, throught the wrapper function you could pass the parameters of your choice to the nested function.
from fastapi import FastAPI, Request, APIRouter, Response
def my_endpoint(client_name: str ='my_client'):
async def newfunc(request: Request):
print(request.method)
return Response(client_name)
return newfunc
app = FastAPI()
router = APIRouter()
router.add_route('/', my_endpoint(client_name='my_other_client'), ['GET'])
app.include_router(router)
I would also suggest having a look at this answer and this answer, which demonstrate how to use add_api_route()
instead of add_route()
, which might be a better alternative, if you faced issues when using FastAPI dependencies
.