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