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