Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Adding a weather report via the API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well we've been able to get the existing reports, but in reality, we don't start with any. So let's go and add the ability to
0:07
add a report. So remember our http verbs. Here we're doing, give me the information about the reports. We want to create something. Typically,
0:15
that's a post. If you know exactly where it belongs, that might be a put. But that's not the case here.
0:20
And I'm gonna rename this to "post", and over here this will be "add a report",
0:26
something like that. So notice we might have this these ideas in terms of our
0:31
API names, but what we're actually doing is we're doing get against the report resource, that gets its all the reports.
0:37
We're doing a post against the report resource, and that creates one of them for us. In this case, what we're going to return is a report,
0:44
not a list of them, right? So down here, we're going to go down and do an add report. We need a description and, what else?
0:53
We need a location. How are we going to get that? What we did up here was we started out having a bunch of variables. Well we
1:00
said you know, we could actually bind a pydantic model here. And so, we can definitely do that as well. We could actually say there's a report.
1:08
I'm gonna give it a different name, sub, and that's a report, like this. And then down here, I could just do yeah,
1:20
I went description. Description, and then reports service, that's why it's not working. Reports submittal, and a location is going to be the location.
1:34
And that's gonna be fine, right? Come down here, see we'll get a description and a location,
1:38
and then we're going to return, this thing returns a report. It seems okay, except for, let's go look at this report again.
1:45
It also has this created date which we would need to set but would be potentially accepting it from the user, which is weird,
1:52
because maybe it's a different time zone. Maybe they're lying to us about when it was created.
1:56
We want the server to control what time it thinks this was created because it was created when it was created on the server.
2:02
But this other stuff is useful. So let's do this. Let's go down and duplicate this and say this is gonna
2:08
be a report submittal that has no created date. And this is now going to derive from the report. Submittal thing that is just as a created date.
2:17
Okay, so we've got this is what users submit. This is what we create and store in the database and return back to them.
2:23
Maybe it also has some kind of ID, right? It could have like an ID about it, but we're not gonna do that. Let's go ahead and do it, why not?
2:30
This is going to be a UUID. Import that. Let's just put it as a string. We'll create out of UUID. Okay, great.
2:38
So, then back here, that means we don't have to accept the full report, but we can just do this Report submittal, pass it over.
2:46
It has all the information we need. This is gonna upgrade it to report and send it back. And let's go and set the ID equals to the string of
2:56
UUID UUID 4. Alright, so that's a little bit of something there. It's a little messy. A real database would probably create that for us,
3:04
but nonetheless, we've got it created. We've got our little fake ID to show the idea that we've got more data
3:10
coming out of the database. Let's give this a shot. It's also gonna get a little interesting. We go over here and we request this.
3:17
We get no data because nothing's been submitted. How do I call the other one? How do I call this over here with the post from my browser?
3:27
Well, I could create a form and post it but it expects the body to be JSON right? JavaScript. No, no, no, no. There's better options.
3:34
We'll look at some tools in a minute, but right now, it's only easy for us to get to this with our browser this one a little more tricky.