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