Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Building a report client app

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


Talk Python's Mastodon Michael Kennedy's Mastodon