Consuming HTTP Services in Python Transcripts
Chapter: HTTP services with Python builtins
Lecture: Python 3: Blog explorer: deleting a post with urllib.request

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Alright, with our new found power in Python 3's urllib we can get rid of this, sorry you can't do this because it requires web features,
0:10 we can come over here and copy across the original request version and let's just fix it up so all we have to do here
0:18 is we are going to use our context manager, right away, we'll say urllib.request.urlopen this url, and the method,
0:27 again, we are going to need our request, sorry, because we got to set the method,
0:38 and we are going to set this to delete, now we are going to pass that over, we'll say as response and this can go, that was just older version,
0:46 of course this is get code, and read, and then, we're done, so let's just do an indent here,
1:00 alright, beautiful, let's run this and see that it's working, so let's see what's here, alright, let's try to delete something,
1:06 well, what post is not very popular, I could try to delete two, but that actually should come back with an error saying
1:13 that one is read only, let's give it a shot. No, 403, forbidden, okay, that didn't work our very well,
1:19 let's try a different one, let's try delete, well, actually, let's go and delete that Python 2 version, okay, delete,
1:26 and did it go away, it did, okay, excellent. Now, one thing to notice is we actually got an error, we got an error there when we tried to delete that
1:36 and so just checking the response code, that totally didn't work for us,
1:39 let's try this again, let's try to delete number 2, and see where the error is, the error is right here, it says no, no, this is not working,
1:50 what type of error was it, it is a urllib.error.http error, so let's go ahead and add that error handling in here,
1:57 we have our import here, let's do error as well, so even though we have our cool context manager not enough,
2:10 we got to do add an exception, and http error as he and then we'll print error, see if we can get out of this, get the message and get the code,
2:25 maybe switch the word there, huh? Okay, let's try to delete this, one more time, let's try to delete number one,
2:34 error, we cannot delete it, it's forbidden, so I don't know, this is kind if annoying, that it doesn't let us even look at the error code,
2:42 right, the exception is thrown right here, so maybe it's better to use a try finally to clean this up,
2:48 if it gets created, I don't know, it's a little bit funky but we should add the same error handling to update as well,
2:53 because we will run into the same type of errors. Alright, I think we have our blog explorer written in Python 3 builtins working great.
3:02 Now, as you'll see, you might think well that's not so different, I am just going to say for get request, well,
3:07 we are going to talk about authentication as well, and authentications adds some pretty silly jurations to the builtins,
3:15 whereas it's super simple inside of request. So, the benefits of requests are still going to build up in a crew
3:22 over the whole course or this course here, and this is just some of the funkiness like that
3:28 we have to do a try and then a context manager and so on and so on, and check codes here, check codes there, etc,
3:35 it's kind of just why does it need to be so complicated, right? That said, it's not too bad, if you want to use the builtins.

Talk Python's Mastodon Michael Kennedy's Mastodon