Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Weather API signature
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's focus in on our API for a while here. So we have got "/api/weather" If we look over the homepage here,
0:08 the way I'd like to do this is I would like to have it go "/api/weather/city" and then optionally add in some other things with a query
0:17 string. So optionally add in the country, that's not clear. there's like a Stuttgart, Germany and a Stuttgart, United States. So which one do you want?
0:25 And when you're speaking about the United States, you can pass in the state, like Portland,
0:30 Oregon vs Portland, Maine. So this is the general structure that I'm hoping for. We'll also be able to pass over some units.
0:38 So let's go and see how we would do this here. Well, first of all, for that part,
0:42 that's like Portland, like this, we can just put a variable right there and then that variable is gonna go here as a string. Okay, so I'll say for,
0:55 and if we go back and click that, now we get some report for Portland in slash Portland. we're not do anything yet with state or
1:03 country or units. But that's easy. So, this is gonna be a required part of URL. It's "api/weather/some city name"
1:13 but the other optional stuff, optional from the outside, will be the state, which is, right now I'll say
1:19 It's a string, country, which is a string, and the units, which is a string. So here's the deal. If somebody specifies no units, that's OK.
1:31 We're just going to give them the default. So in order for us to express that we're going to say this is an optional
1:39 string. We have to give it a default value. Let's say that is metric. So if you don't ask for anything, you're getting metric. And for the country,
1:46 Similarly, we're gonna make it optional, but we don't want to allow people to pass in "None". We're just gonna have a default value.
1:53 So this is going to be "US" like that. And for the state, this one, we really do want this to be optional,
1:59 and it's gonna be specified to "none" as well. So this is our basic structure,
2:04 and let's just do some little string like this. We're just gonna return City, state, country and units just to make sure everything's working here.
2:16 There we go. Portland, Oregon, US. Imperial units. But if we omit country, just Portland, Oregon. And guess what?
2:23 It's still US. That's just the way I'm gonna do it, just so you can have it something work like that. And then if you omit the state,
2:31 there's just gonna be no state. We need a question mark there, I suppose. Here we have no state,
2:38 the default US, and imperial. So we could either put something for the units, and there's other, or we could just omit that as well.
2:45 We get Portland in the US, metric, and so on. So this is going to be our signature here.
2:53 There's still gonna be some other things that we need to validate. But this is starting to look long and hard to read with all the types and
2:58 defaults and so on. So for now, we're gonna write it like this. Not too bad. It's a little, little bit busy, but I think that's okay, right?
3:07 And if we come over here and we put in nothing, it's just going to come up as a 404. If we say we want it to require a
3:15 country to be passed, and we omit that, has to be in a different order. That's okay. And we do this. It says "No, no,
3:22 no. The country's required. You gotta specify". So we have a lot of control there,
3:26 but I'm gonna put that back to having a default value but being required.