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