Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: Blog explorer: Deleting a post

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The last thing that we need for our blog explorer to be complete,
0:04 well, as a silly little command line crud app for API anyway,
0:09 is to be able to remove a post,
0:12 so we are going to implement delete a post and it kind of works like
0:15 it's going to get the post and ask you which one do you want to delete,
0:17 number of posts to delete and then it's going to say we're deleting that post
0:23 now the action that we actually have to do here is to implement that.
0:26 So the url according to our API if we go back, to our specification is
0:31 to issue a delete verb to api/blog/id,
0:36 we have the post right here, so that's cool,
0:39 and we're going to say it's going to be base_url + 'api/blog' + post_id
0:46 so that is the url, and we are going to a request.delete against it,
0:50 the url is going to be that, and I don't think we need to enter anything else,
0:56 of course, in a real system we might want to add some headers
0:58 or add authentication in various ways, but,
1:00 this simple example we are just going to do like so
1:03 and we want to check that the status code is successful.
1:06 Now, all the other status codes have been pretty straightforward,
1:09 it's not entirely clear what the best status code for delete is,
1:13 it could be 204 no content that worked, it could be 202 accepted,
1:19 so this service I believe is going to return a 202, if this is successful,
1:24 and if it's not then we'll have to say something went wrong.
1:28 So if we don't get a 202, we are going to assume that something went wrong
1:35 and we'll print this out, and if this accepted it, then we are all good, so deleted,
1:40 and we'll just put the title here,
1:46 deleted this thing, whatever, okay,
1:48 so let's go and run this, and let's go and just see what is here, we'll do a list,
1:52 we have easy breezy Python clients, this is success story,
1:58 we have this one right here, notice this sucker, this post is doomed from the start,
2:02 it has zero views, that seems like a perfect candidate to send a little delete to, right,
2:06 so let's issue our delete command, and it's going to show us that list again
2:10 and say alright, which one is doomed, number 3, number 3 is very doomed, alright.
2:14 And, if we move up a little here, we are going to come back and say
2:19 deleting this post is doomed, it's going to create the url straight to that post
2:22 using this id and it's going to issue a delete verb to that url
2:27 and then we'll just check the status code and see how it comes out, here we go.
2:31 Deleting this post doomed from the start, boom, deleted it, successfully,
2:35 and if we do a list, is it still in the list, no, it was doomed, it's gone.
2:40 Let's try to delete one more, just for the heck of it, let's try to delete number one.
2:44 Easy breezy Python http clients, what happens if we do this
2:49 - error deleting, 403, this post is read only.
2:54 So, it's really good that we have our status code checking error handling,
2:57 because, it's not guaranteed that we can delete everything that it's always going to work,
3:02 but if I add a post here that is going to go away,
3:06 I can go and delete this goodbye one right here,
3:11 deleted, goodbye, list, it is now gone.
3:14 Alright, so those are the crud operations, and how we do all of this work,
3:17 with requests and basic http restful services.