#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,
0:01 but this constant need to call raise_for_status,
0:03 super annoying.
0:04 So if it came back with a 404, this would give us an error
0:07 and say 404, you didn't get the right response value.
0:10 So let's go fix that real quick.
0:13 Let's do that by adding another little helper thing,
0:15 I'll call this uplink_helpers.
0:18 Now this is not to do with Uplink, this is us.
0:20 Okay, so we're going to write an import uplink,
0:23 and what we're going to do is we're going to define
0:24 what's called a response handler.
0:27 So we'll say @uplink.responsehandler,
0:31 and define a method called raise_for_status.
0:34 You can call it whatever you want.
0:35 And here, it takes the response.
0:37 So, if we put this somewhere in our service description,
0:41 it will be called automatically,
0:43 and we can just do this, response.raise_for_status.
0:47 And that's like calling,
0:48 this is a response which comes from requests,
0:53 this is it's way to check for errors,
0:55 and if it works then we'll just return this and carry on.
0:59 Because it's one of these Uplink response handlers,
1:02 it lets is do this little bit,
1:04 this little trick here,
1:05 that's going to make everything wonderful.
1:10 Let's go over here and put blog 2 right there.
1:13 So let's try to see if we can make it break.
1:16 I want to read,
1:18 fails, could not find this URL,
1:20 and where did this happen?
1:21 It happened on this raise_for_status.
1:25 And what we need to do, is we need to actually call
1:28 that all over the place,
1:29 if for some reason we don't,
1:32 it'll give us some weird value about the JSON not matching.
1:36 Why, because it gave us a 404 not JSON.
1:38 But we don't want to have to call this everywhere.
1:40 So let's get rid of these,
1:42 any it yet, not yet.
1:44 There would be more but we're going to skip them.
1:46 So now what we can do, is we can take,
1:47 from our little helper,
1:49 we come over here and say,
1:52 @raise_for_status.
1:58 Have to import that from our little helpers we wrote here.
2:01 Python is saying this should be listed in requirements.
2:03 It's not technically required, because it's a dependency,
2:06 but yeah, sure, let's go ahead and list it.
2:08 Now this means all of these functions will all of a sudden
2:11 start validating, so if I run this again we should not see
2:14 that JSON error,
2:15 in fact we should see, well it worked 'cause I changed it.
2:18 Let's put it back so it's broken one more time.
2:23 See 404 client error, where did this happen?
2:27 If you go,
2:29 here,
2:31 it's just right when we try to access it, right?
2:33 Even though we're not checking this raise_for_status
2:35 thing that we added, it's really really nice.
2:37 We're going to need to do one more thing like this.
2:38 When you apply these decorators to the whole class,
2:41 it applies to every method.
2:43 If you apply them to a method,
2:44 it applies only to that method.
2:46 Whew, okay, so now we have our error handling in place.