Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Building a report client app
0:00 While it's fun to play with the API over here,
0:01 let's go ahead and take it one step further and actually create a little application,
0:06 a little client app, that will do this. Now,
0:08 a lot of Web apps don't have stand alone clients,
0:12 but imagine you're creating an iPhone app or something along those lines.
0:15 We're gonna create a BIN folder and over in the BIN I'll create a report app.
0:20 Something like that. I'll just set that,
0:22 let's first run. Looks like the main is running.
0:24 That's good. And let's run our little report app,
0:27 okay. So when I just hit the button,
0:28 it's gonna run this. So what's this going to do?
0:31 Well, we've already seen how to consume these API's here,
0:34 right? This. This is what we did.
0:36 We wanted to consume it. We wrote that little bit of code
0:39 to asynchronously do this. Actually,
0:41 maybe we'll use requests just because it's simpler, we don't have to do the async event
0:45 loop. Here we go. So what we want to do is let's just write a
0:48 little bit of code, have a main method here,
0:51 and it's going to say "while",
0:53 let's say "choice" or, choice is fine.
0:57 "Choice equals input". Do We want to report something new?
1:09 Or do you wanna see them?
1:11 And we'll say "while choice", we're just gonna do something,
1:14 okay? We'll say "if choice dot lower,
1:16 dot strip is r" we're gonna do one thing.
1:25 Do another. Don't know what to do
1:37 with choice. And then we're just gonna ask this question again.
1:42 Great. So now what are we going to do with this?
1:44 Well, we're gonna define a report event method here,
1:50 and we're going to do a see events method.
1:55 The see event one is probably gonna be easiest.
1:57 All we got to do is go to our URL over here and do a get,
2:01 right? We'll say the URL is this.
2:04 Obviously you don't want local host in general,
2:06 but for this example, that'll be fine.
2:08 We'll say response equals requests. Get URL. Don't let it carry on
2:15 if something is wrong. And then we need the data back,
2:18 it comes back as JSON. So we'll just say response dot
2:21 JSON. And then if you look over here,
2:24 what we're getting back is a list of these things.
2:26 Let me copy it over just so we can have it to work with. We'll say
2:30 for, this should be a list here of reports, so r in data.
2:42 Alright, I'm just gonna do something super simple.
2:43 Like go to the location. Say
2:45 this city has something going on right now,
2:47 and we could do a lot more,
2:49 but let's just go with that.
2:51 And, you know, this might actually work.
2:53 Let's go ahead and run it.
2:54 It would work if we call it.
2:56 There we go. Let's run it.
2:58 Let's say I'm willing to see the reports.
3:00 There are no reports. Is that right?
3:03 No. There are reports right now.
3:05 So what are we doing? Let's print out the data we got back.
3:08 Print data. Oh, it would help if we call these,
3:14 wouldn't it? Okay, so this will be report
3:15 event. That will be see events.
3:18 Perfect. Let's see them. Oh,
3:20 yeah. Portland saw a little flooding.
3:22 Let's go back over here and add something else.
3:25 This will be our body that we're submitting.
3:28 Here it is. Frost on the roads.
3:32 Look out. If we just say I want to see reports
3:36 now, look, Portland has frost on the roads and they saw a little flooding.
3:39 How cool is that? The last thing to do is just do a simple little
3:43 version where we submit this back.
3:45 So this gets a little tiny,
3:47 bit more complicated. But you just copy it from over here like this.
3:51 We're gonna have the description. We'll say what's happening now and what city and
4:10 here We'll just put the description,
4:13 put the city, and I'm not gonna pass the state.
4:15 Alright, so we've got our data,
4:17 and instead of doing a get, well,
4:20 we're gonna need to do a post.
4:25 The URL is the same and what do we want?
4:30 We want the JSON to be data.
4:41 We can just say something has been happening here.
4:44 Actually, we can just say, gotta grab that back,
4:48 and then we come down and say,
4:50 result here, dot get. Let's just say we've got a new event with this
4:55 ID or something along those lines.
4:57 Okay, let's make this really big,
5:00 actually, let's run it outside like this. First,
5:03 let's see the report. Awesome.
5:05 Portland has frost and some flooding. Let's
5:08 report the weather. What's happening?
5:10 Sunny. Yes, it's sunny.
5:12 Where is that? That is in Vancouver.
5:15 Okay, that we reported some new event.
5:17 We got it back from the server.
5:18 How cool. Let's see that.
5:19 Yeah, Vancouver is sunny. Portland has frost on the roads,
5:22 and they saw a little flooding.
5:24 Let's report one more thing. It's misty in Seattle. And we can see the reports,
5:33 boom. Nailed it. So Seattle is misty.
5:36 Let's get out of here. Hit enter, we're done.
5:38 Okay, so that's a bit of a diversion.
5:40 But hopefully you saw some value in creating this.
5:42 Like look, it's all of 47 lines of code and it's pretty spaced out to
5:46 actually write an app that interacts with this cool FastAPI that we created.