Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Concept: Submitted a weather report
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's talk about how we're able to submit a weather report and all the moving pieces
0:05
in our API. So we started out by using our existing location pydantic model, where it already had the validation.
0:12
The city is required. The other stuff is either optional or optional with defaults.
0:16
And part of the thing that they're submitting, part of the report, is the location where that happened, right? Also,
0:23
we need additional information along with that location. What happened there? So instead of just taking a location and a description separately,
0:30
we could actually go through pydantic and say we're going to create a report submittal class,
0:35
and it has a location, and pydantic will actually do that hierarchical binding by traversing the thing that was submitted, finding its location,
0:43
then binding that to a location object and so on. Beautiful. This is not the final thing we wanted to save or send back.
0:49
But this is what we accepted from the user. Remember, we had a reports class that derive from report submittal. Here,
0:55
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
1:04
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,
1:11
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.
1:20
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.
1:27
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.