#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Demo: Getting a individual post

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, before we move on from our read posts, let's actually do one more thing. Let's add another method here, that's going to correspond to this.
0:11 So, what we've done so far is we've just gotten the general info for all of them. Let's get the details for one in particular.
0:17 Theoretically, this one might return more information and more details, whereas, the get all of them might just have high level info.
0:24 This is going to be interesting because we have to pass the post id, so, what we're going to do down here is let's change this to "show a number"
0:32 so we can say "which number do you want to see". The way that, the easiest way to do that is to enumerate this and tell it to start at 1.
0:39 And change this to a little number here like this. So, that's going to print it out and then let's do a, so, we'll ask which number
0:52 they want to view and then we'll say selected_id. It's going to be, we're going to go to our posts, and we're going to use the selected index
1:01 but we're showing them 1 based in arrays but lists are 0 based would you like this dot get_id. Now, let's just print out selected_id really quick.
1:14 Just to prove that this little bit is working. Now, this is totally missing error handling at lots of levels but just to see that it works.
1:22 Just read them, which number do we want to view, I want to view 3, alright, so, that's cool. It pulls out that id and I'm sure you know
1:28 that that's correct, actually, you have no way of knowing but I'm pretty sure it's working. So, what we want to do is actually
1:33 go and use this to get some details. So, back here we go, and this is going to be similar to what we had before but slightly more interesting.
1:40 I'll say, entry my_id, and here we'll pass post_id. Now remember, the way this went, was up here, we had a slash curly id like this,
1:52 and if we want to call it, you don't want to call it id in Python, because id is a built-in and it overrides the name, so, you would just put
1:58 this like so, so, when we call this function this value, that is going to be replaced right there.
2:04 So, let's say get, so we'll get one particular detail. Now, instead of doing this, we'll say selected post,
2:13 entry by id and then we'll pass in the selected_id. Now, again, no error checking to make sure that this worked and so on but that's okay.
2:21 So, here we'll actually, I need to say response, then response.raise_for_status, we'll make this a little bit cleaner in a second
2:28 and then we'll have to come over here and say response.json because it comes back as JSON, until we do this, it doesn't become a thing
2:34 and then let's just print out the details about it, here. So, we get it back. Get the response back. Make sure it worked. We get the JSON
2:44 and then that turns it into an object and we're going to print out here. Let's just try this and make sure it works.
2:50 Let's look at the easy breezy Python HTTP clients. Look at that, it totally worked. We went and got it from the server,
2:58 here's it's id and it's easy breezy. Here's it when it was written. Here's it's body content and so on.
3:05 This is pretty cool, and so, this is really nice the way this is working; it turns out that this raise_for_status is a little bit annoying
3:12 that we have to do this each time. It would be better if we could tell this whole thing just like "hey, don't even
3:17 give me back anything if it didn't work". We'll do that next.

Talk Python's Mastodon Michael Kennedy's Mastodon