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