Let's talk about how we're able to submit a weather report and all the moving pieces
in our API. So we started out by using our existing location pydantic
model, where it already had the validation.
The city is required. The other stuff is either optional or optional with defaults.
And part of the thing that they're submitting, part of the report, is the location where
that happened, right? Also,
we need additional information along with that location.
What happened there? So instead of just taking a location and a description separately,
we could actually go through pydantic and say
we're going to create a report submittal class,
and it has a location, and pydantic will actually do that hierarchical binding by traversing
the thing that was submitted, finding its location,
then binding that to a location
object and so on. Beautiful.
This is not the final thing we wanted to save or send back.
But this is what we accepted from the user.
Remember, we had a reports class that derive from report submittal. Here,
we're using the location exactly to find above and pydantic binds that together.
And then finally, we just have our async report post, or we gave it
a name. Here it's just reports.
Could be add report, something like that.
It takes a reports submittal and it's not documented here,
but add Report returns a proper report
object. That's it. This is all we got to do,
and it is super clean. Pydantic handles so much of it.
Remember also, that it's not as easy to test the post API endpoints
as it is to test the get ones in your browser.
So you should look at something like Postman or some other tool that will let you
very carefully control what gets sent over to your API and test it.