Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Calling open weather map synchronously
0:00 One of the really cool and important things we can do in FastAPI is
0:03 work with it's async capabilities, and I want to make sure we focus right
0:07 in on that. So first we're gonna implement
0:09 this without async, and then we're gonna come around and add async capabilities to
0:13 it. So let's go over here,
0:15 and what we're gonna do is we're gonna use requests temporarily,
0:18 and we're gonna go back to something else,
0:20 httpX. So, let's suppose we've got our requests here and we want to do a
0:28 get against this. So have the response equals get and well,
0:33 right now we have the city and country,
0:35 but we're not taking into account the state,
0:38 are we? So we'll say "if state is not"
0:41 We'll just say "if state" we're gonna do like this. else,
0:50 we're just gonna do the required values which are country and city. We're gonna pass our
0:55 API key along. Our units are also required.
0:58 We do need to do more validation.
0:59 Like, for example, you can't put "Oregon",
1:02 You have to put "or", you can't put anything for units.
1:05 It has to be metric, standard, or imperial.
1:08 The country has to be a two digit code and so on.
1:10 We're gonna add that validation in a moment.
1:12 But let's just get this kind of sort of working.
1:14 So here we go over here and say
1:16 URL and we'll say "response dot",
1:17 First, we could say,
1:19 raise for status, whoops, raise for status like that,
1:23 so make sure nothing went wrong. Later,
1:24 We're gonna actually want to come back and put better error handling there.
1:27 But that's fine. And then we're going to get, hopefully if things went okay,
1:32 we're going to go over here and say we'd like to go to our response and convert some
1:36 kind of JSON response over to an actual Python dictionary.
1:40 And I'm gonna print out the data and then return the data,
1:43 which is not actually what we want.
1:44 You'll see, it's close, but has way more information than we need.
1:48 Alright, moment of truth.
1:50 Click on it here, see what we get.
1:52 Yes, look at that. This is what we got back for the weather in
1:56 Oregon, units Imperial. So here's our location we have currently it's raining with light rain
2:02 and the temperature is 56 degrees F.
2:05 Want to see that in Celsius?
2:06 Let's put in metric. Ah,
2:08 sweet! 13.51 degrees Celsius. Light wind. I'm guessing
2:12 this is meters, meters per second, kilometers an hour.
2:16 I don't know. We'd have to look in the docs to see what that is,
2:18 but check this out. So we've got the Portland US.
2:22 We wanted to change it over to Portland,
2:24 Tennessee. You could see it doesn't really show you over here,
2:27 but the temperature, get it to scroll back.
2:30 The locations have changed. The forecast is now clear.
2:33 It's a little bit warmer, right?
2:35 As you would expect being further south in the US.
2:37 How cool is that? So we've got our data working, we've got our API
2:41 call working. The other thing we want to do is like,
2:44 see all this extra stuff? I mean,
2:45 our goal is not to just pass along
2:47 this information. I mean partly it's mostly just to be an example that we can use.
2:51 But let's suppose that this right here is really all that we care about.
2:55 This part right here. Notice that's under main.
2:58 So we could go down here and say forecast with an E,
3:03 there, is going to be data of main, and then we'll return forecast.
3:08 Right? So let's run it one more time.
3:11 Check it out. Here we go.
3:13 Here it is in Tennessee, and if we don't put a state,
3:18 we don't have to put a country,
3:19 we can just go to Portland, how about that.
3:21 Perfect the default over at open
3:23 weather API is, the units are metric as well.
3:26 So here's what it's like in Portland,
3:28 Oregon, right now. We don't have the clouds and whatnot,
3:32 but, you know, just for our simple example,
3:34 let's suppose this is all that we want to return,
3:36 like the current temperature and the low and the high.
3:39 Pretty awesome, right? So now we've been able to return that from our get
3:43 report here, which goes right out the door over there.
3:46 There's more stuff that we could be doing,
3:48 like converting error messages to proper responses and so on,
3:51 which we're not doing yet, but we will.
3:54 So there's stuff to be happening over here that's gonna make this interesting and useful,
3:58 but for now, we're just passing it along. Pretty cool.
4:00 We got our open weather map API integrated.