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.
0:02 So we have got "/api/weather"
0:06 If we look over the homepage here,
0:07 the way I'd like to do this is I would like to have it go
0:10 "/api/weather/city" and then optionally add in some other things with a query
0:16 string. So optionally add in the country, that's not clear.
0:20 there's like a Stuttgart, Germany and a Stuttgart, United States.
0:23 So which one do you want?
0:24 And when you're speaking about the United States,
0:26 you can pass in the state, like Portland,
0:29 Oregon vs Portland, Maine. So this is the general structure that I'm hoping for.
0:34 We'll also be able to pass over some units.
0:37 So let's go and see how we would do this here.
0:40 Well, first of all, for that part,
0:41 that's like Portland, like this, we can just put a variable right there and then
0:47 that variable is gonna go here as a string.
0:50 Okay, so I'll say for,
0:54 and if we go back and click that, now
0:56 we get some report for Portland in slash Portland. we're not do anything yet with state or
1:02 country or units. But that's easy.
1:05 So, this is gonna be a required part of URL.
1:09 It's "api/weather/some city name"
1:12 but the other optional stuff, optional from the outside, will be the state,
1:17 which is, right now I'll say
1:18 It's a string, country, which is a string, and the units,
1:23 which is a string. So here's the deal.
1:26 If somebody specifies no units, that's OK.
1:30 We're just going to give them the default.
1:32 So in order for us to express that we're going to say this is an optional
1:38 string. We have to give it a default value.
1:40 Let's say that is metric. So if you don't ask for anything,
1:43 you're getting metric. And for the country,
1:45 Similarly, we're gonna make it optional,
1:48 but we don't want to allow people to pass in
1:50 "None". We're just gonna have a default value.
1:52 So this is going to be "US" like that.
1:55 And for the state, this one,
1:56 we really do want this to be optional,
1:58 and it's gonna be specified to "none" as well.
2:01 So this is our basic structure,
2:03 and let's just do some little string like this. We're just gonna return City,
2:11 state, country and units just to make sure everything's working here.
2:15 There we go. Portland, Oregon,
2:16 US. Imperial units. But if we omit country, just Portland, Oregon. And guess what?
2:22 It's still US. That's just the way I'm gonna do it, just so you
2:25 can have it something work like that.
2:28 And then if you omit the state,
2:30 there's just gonna be no state.
2:32 We need a question mark there,
2:34 I suppose. Here we have no state,
2:37 the default US, and imperial. So we could either put something for the units, and
2:41 there's other, or we could just omit that as well.
2:44 We get Portland in the
2:46 US, metric, and so on.
2:49 So this is going to be our signature here.
2:52 There's still gonna be some other things that we need to validate.
2:54 But this is starting to look long and hard to read with all the types and
2:57 defaults and so on. So for now,
2:59 we're gonna write it like this.
3:02 Not too bad. It's a little, little bit busy,
3:04 but I think that's okay, right?
3:06 And if we come over here and we put in nothing,
3:10 it's just going to come up as a 404. If we say we want it to require a
3:14 country to be passed, and we omit
3:16 that, has to be in a different order.
3:18 That's okay. And we do this.
3:20 It says "No, no,
3:21 no. The country's required. You gotta specify".
3:23 So we have a lot of control there,
3:25 but I'm gonna put that back to having a default value but being required.