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