#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Demo: Only success responses

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This is working pretty well, but this constant need to call raise_for_status, super annoying.
0:05 So if it came back with a 404, this would give us an error and say 404, you didn't get the right response value. So let's go fix that real quick.
0:14 Let's do that by adding another little helper thing, I'll call this uplink_helpers. Now this is not to do with Uplink, this is us.
0:21 Okay, so we're going to write an import uplink, and what we're going to do is we're going to define what's called a response handler.
0:28 So we'll say @uplink.responsehandler, and define a method called raise_for_status. You can call it whatever you want. And here, it takes the response.
0:38 So, if we put this somewhere in our service description, it will be called automatically, and we can just do this, response.raise_for_status.
0:48 And that's like calling, this is a response which comes from requests, this is it's way to check for errors,
0:56 and if it works then we'll just return this and carry on. Because it's one of these Uplink response handlers, it lets is do this little bit,
1:05 this little trick here, that's going to make everything wonderful. Let's go over here and put blog 2 right there.
1:14 So let's try to see if we can make it break. I want to read, fails, could not find this URL, and where did this happen?
1:22 It happened on this raise_for_status. And what we need to do, is we need to actually call that all over the place, if for some reason we don't,
1:33 it'll give us some weird value about the JSON not matching. Why, because it gave us a 404 not JSON. But we don't want to have to call this everywhere.
1:41 So let's get rid of these, any it yet, not yet. There would be more but we're going to skip them. So now what we can do, is we can take,
1:48 from our little helper, we come over here and say, @raise_for_status. Have to import that from our little helpers we wrote here.
2:02 Python is saying this should be listed in requirements. It's not technically required, because it's a dependency,
2:07 but yeah, sure, let's go ahead and list it. Now this means all of these functions will all of a sudden
2:12 start validating, so if I run this again we should not see that JSON error, in fact we should see, well it worked 'cause I changed it.
2:19 Let's put it back so it's broken one more time. See 404 client error, where did this happen? If you go, here,
2:32 it's just right when we try to access it, right? Even though we're not checking this raise_for_status thing that we added, it's really really nice.
2:38 We're going to need to do one more thing like this. When you apply these decorators to the whole class, it applies to every method.
2:44 If you apply them to a method, it applies only to that method. Whew, okay, so now we have our error handling in place.

Talk Python's Mastodon Michael Kennedy's Mastodon