#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Demo: Creating the client
0:00 So here's our skeleton of our app,
0:02 let's go ahead and start by defining the client
0:05 for the service we're going to use over in a separate
0:08 class file, so we'll call this blog_client,
0:11 something like that, actually, call it like this,
0:15 and then create the class as BlogClient.
0:18 You just entered Python class, we talked about this
0:20 in the text game, where we built our wizard
0:22 and dragon and stuff.
0:23 The way Uplink works is,
0:25 of course we're going to import uplink,
0:27 and then, we need to derive from this thing called
0:30 an uplink.Consumer.
0:32 Now this will tell Uplink it's going to work with
0:34 our particular methods.
0:35 However, we still need to do a few things.
0:38 We need to define the operations that we're going to see,
0:41 and then have those map to the service.
0:43 So let's go over here and define a method,
0:45 just all_posts.
0:47 It doesn't take any argument, it's just going to go up there
0:49 and download them, that's what we saw
0:52 when we clicked right here.
0:54 Now notice the whole URL is like so,
0:58 and then, for the various pieces, the part that changes
1:01 as you know, basically this little end part, okay,
1:05 so we can actually go and say this, we can say
1:08 define it dunder init, and we can set that at one place
1:11 so we can say super.__init__, so this is going to
1:14 pass this information off to the Uplink consumer,
1:17 notice there's the base_url, so we'll say base_url is,
1:21 alright so we don't have to say that anywhere.
1:23 Down here we can say what we're going to do,
1:25 this is actually going to be an HTTP GET,
1:30 let's just put the details really quick here,
1:31 so, the way that works is we put a little docstring
1:34 here that just says gets all posts, called blog_entries.
1:39 I know they're typically called posts, but the word,
1:42 let's just call all_entries.
1:44 We're going to use HTTP verbs, GET POST and so on,
1:47 and that's, you know,
1:48 nomenclature can be challenging there.
1:50 So we've got this, now this is not
1:52 going to do anything for us right,
1:54 it's just going to return nothing basically,
1:56 but what we can do down here is we can say
1:58 add uplink.get, and this tells it convert this
2:01 to some sort of method that's going to go to the server
2:04 and it's just going to be, let's take that part off there,
2:07 and say api/blog.
2:12 This is literally all we have to do.
2:14 So, we're ready to use this thing.
2:16 Let's come over here and just test it out on one
2:19 and then we'll look at some more interesting pieces,
2:21 so I'm not going to go and write a lot of code here
2:24 to make a print out, I'm just going to show you that it works.
2:26 So let's come over here and we'll say response equals,
2:29 actually we'll have to create our client say svc equals
2:32 blog_client like that, import at the top,
2:35 and then we'll say svc.all_entries, let's just say
2:39 what is this, and let's just say what value does it have?
2:43 Okay, so let's run this and see if any of it works.
2:47 What went wrong?
2:48 Well, I forgot one little detail, we need to call main.
2:55 Let's read our posts.
2:57 Oh my goodness, look at this,
3:00 we got a response from the server 200,
3:02 everything is A okay,
3:03 and it's actually a requests response,
3:07 now it turns out you can use
3:08 other underlying network providers other than requests,
3:11 but if you don't do anything, that's what you get,
3:13 so requests is pretty cool,
3:14 we already talked about that previously,
3:16 that's what we used before.
3:17 So, what we can do, if we come over here and we type
3:19 response., you get no, no help, nothing.
3:22 So let's go over here and quickly add a little
3:24 type annotation that says this returns this,
3:28 and import at the top.
3:30 If we do that, and now we come over here and
3:32 type response. we get lots of good stuff
3:36 so we can say raise_for_status,
3:38 just to make sure this worked.
3:40 If it doesn't work, that line's going to sort of crash,
3:43 and then we can just come down here and say post equals
3:49 and let's just do a quick print out for this one.
3:53 Let's show the view count, and these things have come
3:55 back here, when we say JSON converts to a dictionary,
3:58 well a list of dictionaries,
3:59 so it does do dictionary stuff, so be a view count,
4:02 you can get this from looking just at
4:04 the JSON that actually comes back,
4:06 and we're going to have title,
4:09 so let's run this one more time.
4:11 Let's read the posts.
4:13 Bam, how awesome is that.
4:15 So, we're off to a really, really good start,
4:18 let's go and implement the rest of the features.