Modern APIs with FastAPI and Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Modifying data through the API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We spent much of the time with our API just trying to talk to it and have it give us answers. Give us the weather in a location,
0:08
maybe converting that over correctly and so on. But eventually we said, Hey, it'd be great if the applications could submit data to the API,
0:15
and actually make changes to our in-memory data, which ultimately would be a real database. So in order to do that,
0:21
we set up a couple of pydantic models. We already had location, which derives from base model with a city, country, and state.
0:28
We were using this for our search, and we said we're gonna also have the ability to submit a weather report which has
0:34
a location. Notice how we're leveraging here the other pydantic model in a hierarchical way for location. It also has a description and
0:44
pydantic can keep this all together. And then for our API endpoint, we just say it takes a report,
0:49
which is one of these types, a report submittal we called it,
0:53
and then it will automatically take that data that's submitted over, with the description and embedded
0:58
location, and convert that and validate that exactly like we expected. The other thing to note is our http verb, we're no longer doing router.get,
1:07
we're doing router.post, that means were changing something. Also what we ended up doing in our example, I didn't make it to the slides
1:15
here, is on the router.post decorator we said the status code is 201 by default. So if things go well, don't say 200
1:22
ok, that's not normally the response from a post. 201 created so we can have it automatically do that when everything works out.