Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: Blog explorer: Updating a post
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Our blog explorer lets us list post and add new ones, now it's time to make them editable,
0:08
so I've added just a little bit of I guess you could call this user interface code, like some interaction with the user to ask them
0:14
show me the posts that you would like basically I am going to list them out and put numbers by them, you tell me which number it is you want to edit
0:22
and then I give them the option to enter a new title and I use a little conditional here to say if they don't enter anything
0:29
we'll just use the existing title, we do that for content and for view count. So, that is going to get us something that looks like this,
0:36
if we go over here and say update it's going to say alright, these are all the posts,
0:39
which one would you like to update, let's update our success story. So it says okay, we could just hit this,
0:44
alright I could but the view count this thing has gone crazy, it looks like this,
0:48
and of course, nothing happens because that is the part we are going to write right now.
0:52
Okay, but that is what this user interaction bit here is, so we have the data, I'll say post data again that we want to update
0:59
and I am going to put this into a dictionary just like before, now we don't have published, we are not asking them when it was published,
1:07
that part can't be changed, so I am going to say post.published and we'll just use the one we got back from the server, here
1:14
well, we got all of them back from the server and we picked one based on the number they chose, enter a number of post,
1:21
notice, there is no error handling, we are just assuming this is going to be an int, and anything you build,
1:27
you know, obviously, error handling is important, so here is the updated data, and maybe they entered nothing and we're just sending the title back,
1:33
but maybe they updated the content and we are going to send a new version of content, or whatever. So, what do we need to do to update it?
1:41
First of all, we're going to need the url, that's going to be base url plus, what we had to forward is api/blog but in our service, over here,
1:50
if we are going to do an update, we have to do a put request to /api/blog/the id of the post so let's make that happen,
1:59
so it's going to be like so, and then we have post.id, like that, okay, now remember, these are named tuples that are coming back from our get post
2:09
so we can access them just like .id and .publish, we are going to have to use dictionary, okay so we have this,
2:16
we want to say the response is going to be request.put and again we have url and data now this time I don't think we have a json
2:23
so we're going to have to be a little more careful, so we have url data=json.dump s so that is going to do the stringification of the post data,
2:35
and if we had any headers, we could set the headers as well here. Okay, we want to do this put, and then we'll say if we got to test for success,
2:42
status code what does success mean, for an update command. Well, it might be 200 if they are not very careful with their codes,
2:50
but it turns out that the most precise response from the server should be 204. So 204 means the server has successfully fulfilled the request
3:01
and that there is no additional data for you to get, if we've given it the new data,
3:05
we already know the id, we already have the server representation of the post,
3:08
if we really want it back we can ask for it, so if it's not equal to this, we are going to print as always maybe there is text,
3:15
if there was an error, who knows, but if there is not, we are just going to say print, so this is status does equal 204,
3:23
successfully updated and I'll just print out the title. Okay, I think that's going to do it, we probably want to consider the headers,
3:33
but let's go and just run with this, okay, so let's do a list and we still have our data,
3:38
remember, this is a stateful server, so it has our success story and it has, this is our attempt to create a first post, okay, now, the first attempt,
3:48
people love getting started stories so maybe it's been viewed a lot and maybe we want to change the title or the body just a little bit
3:54
so let's say we want to update a post, alright, which number do we want to edit? We want to edit this one which is 4, okay, this is our first attempt,
4:02
let's take the default title, say "I knew all along that this demo would work the first time"
4:11
of course I did, and like I said, that's been viewed 1001 times, I don't know if this demo worked, but the one that created that post,
4:19
that one I can be sure it did actually work the first time, I think this is going to work,
4:23
let's see, we've got a request.put data as stringified json and url to the particular post, I think it's going to work, let's give it a shot.
4:33
Successfully updated, our first post. How cool is that; now, if I do a list, we'll know this worked if it went from what it was before,
4:41
scroll, scroll, from 10 to a 1001, let's do a list and find out. Boom, 1001, alright, let's do that one more time, we'll pick number 4 again,
4:53
this first attempt was a winner, and we'll just take these defaults here
5:00
and update it again, let's do another list, see this first attempt was a winner, it took those values and of course,
5:07
we are doing a put back to the url specified by that id.