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