Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: Blog explorer: Getting posts

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've seen that our blog explorer is really not very interesting if it can't get the post, so we've already seen how to get http json based data,
0:10 but let's go and do it just one more time so you become familiar with what the service expects you to send and receive
0:17 and just to start this project from scratch. So we are over here in Postman, you can see I am pointing at the public API,
0:23 we could do a little send here and see what comes back, so notice that we have a list of json objects which we will become very easily
0:30 Python dictionaries and that the title publish, content, id and view count precisely match
0:37 id, title, content, published and view count that are in our named tuple. This is going to save us some work, so let's go down here to get post
0:46 and it just says to do get post so let's return that, let's get rid of that, let's go over here and what we are going to do is
0:54 we are going to create the url that we got a colon, it's going to be base url plus api/blog, we'are just going to do a get request
1:00 of that that will give us what we saw in Postman, so we'll have our response=request.get(url)now,
1:06 maybe we should indicate in our header that we are expecting a response type of json,
1:11 but the service only returns that so we are going to roll with this for now. We'll say if response.status_code ! = 200,
1:20 we want to print, there is some kind of error, 404, or whatever, something to that effect, and now if we do, if we do have the post,
1:31 what we want to do is we want to return a list and we can use a nice little list comprehension here
1:37 because I've created the elements of this dictionary, well that is what we are getting from this service, there is no debating that,
1:45 but I've created this post thing up here this named tuple, think of it as a class without behaviors, just data, with exactly the same name here,
1:54 so that lets us write something like I am going to put a thing here for a minute and then we'll say for p in response.json, right
2:04 remember what we get back here is a list of these dictionaries, so p we could call it post is going to be one of those elements,
2:10 and what we want to do, we really want to return a post and I'd like to set title=post.get ('title'), and then id=post.get('id') etc,
2:23 but we can do it just like this, we can take that dictionary and unpack it as keyword values in the post
2:30 and because there should be if I did this right, a one to one mapping between keys and post and keys and the named tuple
2:36 it's expecting, we should get exactly what we are looking for, let's find out.
2:39 Alright, moment of truth, list, boom, look at that, this crazy number is the id,
2:45 this is the number of views and then this is the title; if you look at Postman, you see we actually get more data back, we also get the content,
2:54 now this is really short because I just didn't want to put a lot into it, but this is basically meant to be the body of the blog post,
3:00 where it's got the title and the publish text and view count and so on.
3:03 We are of course working to simplify data model here but I think it's working pretty well,
3:07 now look how cool that was, like nice little dictionary unpacking, this is great, so we got our data and we are reading it from the service,
3:17 let's go ahead and go back and tell the service hey I would actually like to be very explicit about that I am expecting you to send me json,
3:25 so you could do that over in postman here if we go to headers and we could add and accept here
3:33 and it says the value could be application json right there, application/json, so how do we do that in code, we just go over here and say headers,
3:41 and we make a dictionary and we say accept and the value is going to be application/json and we say headers = there is headers variable here.
3:51 so we can run this one more time, it should work the same, beautiful.


Talk Python's Mastodon Michael Kennedy's Mastodon