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
0:01
just trying to talk to it and have it give us answers.
0:04
Give us the weather in a location,
0:07
maybe converting that over correctly and so on.
0:09
But eventually we said, Hey,
0:10
it'd be great if the applications could submit data to the API,
0:14
and actually make changes to our in-memory data,
0:16
which ultimately would be a real database.
0:18
So in order to do that,
0:20
we set up a couple of pydantic models.
0:22
We already had location, which derives from base model with a city, country, and state.
0:27
We were using this for our search,
0:29
and we said we're gonna also have the ability to submit a weather report which has
0:33
a location. Notice how we're leveraging here
0:37
the other pydantic model in a hierarchical way for location.
0:40
It also has a description and
0:43
pydantic can keep this all together.
0:44
And then for our API endpoint,
0:47
we just say it takes a report,
0:48
which is one of these types, a report submittal we called it,
0:52
and then it will automatically take that data that's submitted over, with the description and embedded
0:57
location, and convert that and validate that exactly like we expected.
1:01
The other thing to note is our http verb, we're no longer doing router.get,
1:06
we're doing router.post, that means were changing something.
1:10
Also what we ended up doing in our example,
1:12
I didn't make it to the slides
1:14
here, is on the router.post decorator
1:17
we said the status code is 201 by default.
1:20
So if things go well, don't say 200
1:21
ok, that's not normally the response from a post. 201 created so we
1:25
can have it automatically do that when everything works out.