I'm sorry if this is a duplicate or I've missed how to do this. I have scoured the docs and github issues.
Here's a self-contained minimal, reproducible, example with my use case:
# URL request: localhost/api/weather/portland?state=OR&country=US&units=imperial
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
import uvicorn
class Location(BaseModel):
city: str
country: Optional[str] = 'US'
state: Optional[str] = None
units: Optional[str] = 'metric'
app = FastAPI()
# What we have is:
@app.get('/api/weather/{city}')
async def weather(city: str,
country: Optional[str] = None,
state: Optional[str] = None,
units: str = 'metric'):
# work with city, country, etc.
return Location(city=city, country=country, state=state, units=units)
# What I want:
# @app.get('/api/weather_binding/{city}')
# async def weather(loc: Location):
# work with loc.city, loc.country, etc.
# Where the binding inputs to the Pydantic model are basically:
# data = {
# **request.query_params,
# **request.headers,
# **dict(await request.form()), # only if the form dependencies are installed
# **request.path_params
# }
# return loc
uvicorn.run(app)
/api/weather/portland?state=OR&country=US&units=imperial.{
"city": "portland",
"country": null,
"state": "OR",
"units": "metric"
}
Basically, I can do this already with this step:
data = {
**request.query_params,
**request.headers,
**dict(await request.form()),
**request.path_params
}
loc = Location(**data)
But that's a drag and having the framework do this, even with an opt in flag in the route (like bind_all=True) would be fantastic.
I've consider just using the lines of code above more or less on method, but that doesn't seem to fit with the clean automatic nature of the platform.
None
I think you are looking for Depends here.
from fastapi import Depends
@app.get("/api/weather_binding/{city}")
async def weather(loc: Location = Depends()):
return loc
Open your browser go to http://127.0.0.1:8000/api/weather_binding/New%20Mexico?country=US&state=oregon&units=metric
{
"city": "Nashville",
"country": "US",
"state": "Tennessee",
"units": "metric"
}
It really understands what you are trying to do. Normally GET requests can not contain a request body as OpenAPI specification points it out clearly in here. Without Depends, it assumes it is a schema and expects a request body. But with Depends it understands it is a GET operation it turns that into parameters. But if you use Depends in a POST operation you'll have a request body whether you have a Depends or not.
Thanks @ycd This is close but it doesn't consider header values or body values. However taking query params and route params is pretty close to what I had in mind. Definitely appreciate the pointer.
That said, I would never ever think dependency injection == reading values from query string. To me, dependency injection is all about I register something like a DB concrete type at app startup, the method says it takes one, I get the instance or type I registered. But that is entirely outside the bounds of the request, just the app config. I think of dependency injection much like pytest fixtures.
So, it's super confusing to me that these things coexist in the same feature / help location. :) Thank you for helping me on it.
It's also possible to do something like this:
from fastapi.testclient import TestClient
from fastapi import FastAPI, Depends, Form
from pydantic import BaseModel
app = FastAPI()
def form_body(cls):
cls.__signature__ = cls.__signature__.replace(
parameters=[
arg.replace(default=Form(...))
for arg in cls.__signature__.parameters.values()
]
)
return cls
@form_body
class Item(BaseModel):
name: str
another: str
@app.post('/test', response_model=Item)
def endpoint(item: Item = Depends()):
return item
tc = TestClient(app)
r = tc.post('/test', data={'name': 'name', 'another': 'another'})
assert r.status_code == 200, r
print(r.json())
Great, thank you @Mause Depends() wasn't obvious but it does pretty much achieve what I was hoping for. :)
Most helpful comment
It's also possible to do something like this: