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


Talk Python's Mastodon Michael Kennedy's Mastodon