Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Concept: Submitted a weather report
0:00 Let's talk about how we're able to submit a weather report and all the moving pieces
0:04 in our API. So we started out by using our existing location pydantic
0:09 model, where it already had the validation.
0:11 The city is required. The other stuff is either optional or optional with defaults.
0:15 And part of the thing that they're submitting, part of the report, is the location where
0:19 that happened, right? Also,
0:22 we need additional information along with that location.
0:25 What happened there? So instead of just taking a location and a description separately,
0:29 we could actually go through pydantic and say
0:32 we're going to create a report submittal class,
0:34 and it has a location, and pydantic will actually do that hierarchical binding by traversing
0:40 the thing that was submitted, finding its location,
0:42 then binding that to a location
0:43 object and so on. Beautiful.
0:45 This is not the final thing we wanted to save or send back.
0:48 But this is what we accepted from the user.
0:50 Remember, we had a reports class that derive from report submittal. Here,
0:54 we're using the location exactly to find above and pydantic binds that together.
0:59 And then finally, we just have our async report post, or we gave it
1:03 a name. Here it's just reports.
1:05 Could be add report, something like that.
1:06 It takes a reports submittal and it's not documented here,
1:10 but add Report returns a proper report
1:12 object. That's it. This is all we got to do,
1:15 and it is super clean. Pydantic handles so much of it.
1:19 Remember also, that it's not as easy to test the post API endpoints
1:24 as it is to test the get ones in your browser.
1:26 So you should look at something like Postman or some other tool that will let you
1:30 very carefully control what gets sent over to your API and test it.