Transcript from the "requests Library" Lesson
>> Nina Zakharenko: Very quickly, let's use the request library to interact with APIs. So let's all together make a new file and we're gonna call it dog.ui
>> Nina Zakharenko: And it might be useful for you to have this page open from the class exercise. So to use the request library, the first thing we need to do is import requests, next we're gonna define the URL of an API that we want to interact with.
[00:00:41] If you go back to the class website and you look down here, where I define the API URL variable, you can just copy and paste this. It's the URL for Shibe Inu API, and just make sure that you get the quotes right.
>> Nina Zakharenko: Now, we want to get a response back, so we're gonna use the request library to call .get on our API URL.
>> Nina Zakharenko: And then if our response is successful, it should have a status code available on it. So we can print out that the status code.
>> Nina Zakharenko: Is, okay, and let's run this. Okay, status code 200. What do remember from the cat pictures? What does 200 mean? 200 okay, everything went great.
[00:01:48] Now, by default these days, most APIs return JSON, some of them optionally let you specify but I know that this API returns JSON. And oops, sorry and working with JSON, with requests is really easy, we can assign a variable response_json by just calling the response, remember, that's the variable here, the response from the server.
[00:02:23] And response.json will get the JSON from that response, convert it into a Python dictionary, so let's print that out.
>> Nina Zakharenko: Now if you did something wrong, if you're passing in some parameters that didn't make sense. Calling this API might result in any other type of HTTP status code, for example 404500, depends.
[00:03:00] Okay, now, one of the nice things about VS code, you should have a URL printed out here. You can command and click in the terminal to follow that URL and we just used an API to get this adorable picture of a doge. Doge's on demand, you can run this all day, every day and get new adorable dog pictures.
>> Nina Zakharenko: So the request library, I told you that Python was batteries included, sometimes those batteries are a little rusty, right? The standard library is more focused on feature richness, on backwards compatibility. Other external libraries have other purposes, so this library in particular requests focuses on making working with APIs as easy and as straightforward as possible.
>> Nina Zakharenko: There's one other thing I want to show you, remember, I talked about URL parameters that we can optionally add on some parameters to our API. Let's do that with our response, so by looking at the documentation I know that I can pass in a parameter of accounts.
[00:04:22] So how many adorable dog pictures do I want to look at? We declare our parameters as a dictionary, so I'm gonna say count of 10 and then we pass in those params to our response. So these two lines right here, lines five and six are the only things that I've changed.
[00:04:47] If I run my Python file in the terminal now, we'll see that I got ten responses back.