Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Calling the API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It might seem like we're a long way from being done with this application.
0:04 We've talked a lot about some honestly slightly complicated topics with virtual environments and dependencies and
0:10 manage them and all that kind of stuff. But we're actually extremely close to being done,
0:14 as you will see. So let's go over here to the "call_weather_api". I've have already written "import requests" at the top, so we can jump down here.
0:20 Remember, we've already got the location as a named tuple, and we're using it to put together the URL. So here we just said, we would call the URL.
0:29 Well, let's not say we would, let's do call it. So we're gonna get a response from the server. The way it works is you go to request you say,
0:36 "get the URL", done. We've now downloaded the data from the Internet. How insane is that? So let's just print out what we got.
0:43 If I say Portland like that, response 200. 200 in the Web is okay, success. So that's good. There's all kinds of things here. One of them is the text.
0:56 So if we print out the text from Portland, what do we see? Oh oh oh.. broken clouds. How about that?
1:03 Super cool. Now we could use the libraries that understand strings and convert them to Python dictionaries by parsing the JSON document,
1:13 but requests already knows how to do that. It has a function called JSON. So if we write that and we type "Portland", it looks the same, Right?
1:22 But remember, Pythons representation of dictionaries has single quotes. Our Web server's returning double quotes.
1:27 So what actually came back is just data and it just when you print it out it looks basically the same.
1:32 So check this out. We'll say "data equals response dot JSON". Now, Before we go through that step,
1:38 we got to make sure that we actually got the right values. So what we need to do is say if for some reason we got something other than
1:45 a 200 ok request and JSON response, we just want to say "sorry we couldn't get you any weather".
1:50 So the easiest way for us to do that is to ask if the "response dot status code", that's that number, is in a set of things. We could do a
2:01 bunch of if statements or we could just say, "Is it in 400?" which would mean bad request, 404 like if you asked for a city that doesn't exist,
2:08 500, the server crashed. We could return "None". In fact, we could say, basically, it's not 200, return an error,
2:18 right? So here we'll print out "error", and then let's just put the "response dot text". A lot of times when there's an error,
2:25 you actually get an additional message in the text, so this might help us debug what's going on.
2:30 Like, for example, if we go and ask for the city such and such, it says "error, what we got back is 404, city not found".
2:38 Okay, so once we make it this far, we think we're in good shape, and this might, I'll leave it like this, but you might just say "not equal to 200".
2:46 So the last thing I want to do is I actually want to convert this response into something that's easier for us to work with.
2:54 But we're already off to a pretty good start. What about Boston? Whoo hoo! Boston! Alright, we returned it. We haven't printed out anything yet,
3:03 but our final step is going to be to convert that into something meaningful we can show to the users.


Talk Python's Mastodon Michael Kennedy's Mastodon