#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Concepts: uplink
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review what we built. We defined a client to interact with our HTTP service by creating a class and having it derive from uplink.Consumer.
0:12
So we created blog_client and its consumer so uplink can do its magic. The next thing that we needed to do was pass it the base url.
0:21
Here's one way to do that. We could tell this class, it's always going to work with that API, so you don't have to pass this base url every time.
0:29
It's really nice to have this if you're doing testing against your own APIs. You could, say if you're in debug mode,
0:36
then you say, like a local host version, otherwise use production or staging, whatever, right? So this is really nice, it sort of sets it up.
0:43
And then anytime we want to have an API point called, we just write a method, decorate it with one of
0:50
the HTTP verbs, here we're using uplink.get/api/blog/post_id, and that post_id is one of the arguments. Now of course what we get back
1:00
is a request version of a response. And notice, there's no real implementation, but here's a nice chance to add a doc string
1:08
and add a little documentation. So this is what I would think of as a raw API method. Down here, we saw when we want to pass the body
1:16
sometimes that's a little cumbersome, doesn't really give the consumer of the client a lot of help on what to put, so we wrote this wrapper function.
1:24
It takes meaningful arguments, creates some default values for us, and then calls this hidden internal one that routes to our url/api/blog.
1:32
To create a new one, we just kind of pass on through. Really really nice, and for that last part to work
1:38
when we did uplink.body, we had to specify JSON, otherwise it's going to pass it as form encoded, which, the server, if it doesn't want that,
1:45
it's going to get upset. Here's what we got to do to built data servers, and now we can just use it as if it was
1:51
a standard class, and all these things flow over to the service, really really nice.