Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Open Weather data info

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we've built a really cool API that echoes back what you send it. Not super useful, is it? So, of course,
0:07 what we want to do here is to pass this information along and actually call the place that has the real weather report, bundle it up and re-send back.
0:15 So, let's go and create another thing over here called "services". And here we'll put the "open weather service",
0:26 which is gonna be the few functions we need to interact with this open weather service. And we're gonna have a function called "get_report".
0:33 It's gonna be simple, let's have a city, let's go state, country, units like that, and units is gonna be a string.
0:41 This is going to be an optional string. Actually, this one we made required, didn't we? But we just give it a default.
0:52 And then this is going to be a string, and it's gonna return for now, let's say it just returns some arbitrary dictionary.
0:59 Now what? How do we call this thing? So let's actually go over to "" and you can see they're doing a ton of predictions and calls,
1:08 right? So apparently two billion forecasts a day. That's a lot of forecasts. Let's go to the API.
1:13 Remember you're gonna have to sign in and create an API key to do this. It's free, but you have to do it.
1:20 Let's just look while we're here, pricing is free. We get a free API, key 60 calls a minute,
1:25 a million calls a month. Sorry said that a day; it's a month, which is plenty for our little demo app.
1:30 So let's look at this. There's a couple of options. We make a request to all of these various places here. We can do just the city,
1:39 city and state, city state country. Alright, so that's what we're gonna need to do. And we'll just go down here say the URL is equal to this.
1:47 Let's put https at the front, and the query equals something that goes here. Let's put, we're gonna create a little string called "Q",
1:57 they we're gonna create another thing called "Key" for the API key. Those don't exist yet, so let's go over here and say "q=",
2:05 well, what's required? We're gonna have "city, country", like that. And where do we pass the units over? That's right. So we can pass the units
2:16 I think we pass them. Alright. And then the key is going to be "123" for the moment.
2:24 Alright. So let's just say this is the URL that we're going to need to call. And we'll print out URL,
2:30 and here we'll say "report equals open weather service dot get report" and pass the things on, location dot
2:38 city, location dot state, location dot country and units. We're gonna return that back. So let's give this a try and see what happens.
2:55 We go and click that. Well, we got nothing back, which is fine, but it says here is the URL we would go click on and close.
3:05 It's close. Portland, US. But we need to get our app ID. So what I'm gonna do is I'm gonna add my app ID,
3:14 but I'm not going to show it to you guys because you got to get your own. But I'll show you how we're gonna do that next.
3:18 So we're pretty close to calling this API endpoint and getting stuff back, but we gotta handle this shared secret, this secret in our project.

Talk Python's Mastodon Michael Kennedy's Mastodon