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