#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.
0:02
We defined a client to interact
0:05
with our HTTP service by creating a class
0:08
and having it derive from uplink.Consumer.
0:11
So we created blog_client and its consumer
0:13
so uplink can do its magic.
0:16
The next thing that we needed to do
0:17
was pass it the base url.
0:20
Here's one way to do that.
0:21
We could tell this class, it's always going to work
0:24
with that API, so you don't have
0:26
to pass this base url every time.
0:28
It's really nice to have this
0:29
if you're doing testing against your own APIs.
0:32
You could, say if you're in debug mode,
0:35
then you say, like a local host version,
0:37
otherwise use production or staging, whatever, right?
0:40
So this is really nice, it sort of sets it up.
0:42
And then anytime we want to have an API point called,
0:46
we just write a method, decorate it with one of
0:49
the HTTP verbs, here we're using uplink.get/api/blog/post_id,
0:56
and that post_id is one of the arguments.
0:58
Now of course what we get back
0:59
is a request version of a response.
1:02
And notice, there's no real implementation,
1:04
but here's a nice chance to add a doc string
1:07
and add a little documentation.
1:08
So this is what I would think of as a raw API method.
1:12
Down here, we saw when we want to pass the body
1:15
sometimes that's a little cumbersome, doesn't really
1:17
give the consumer of the client a lot of help
1:21
on what to put, so we wrote this wrapper function.
1:23
It takes meaningful arguments, creates
1:25
some default values for us, and then calls
1:27
this hidden internal one that routes to our url/api/blog.
1:31
To create a new one, we just kind of pass on through.
1:34
Really really nice, and for that last part to work
1:37
when we did uplink.body, we had to specify JSON,
1:40
otherwise it's going to pass it as form encoded,
1:42
which, the server, if it doesn't want that,
1:44
it's going to get upset.
1:46
Here's what we got to do to built data servers,
1:48
and now we can just use it as if it was
1:50
a standard class, and all these things flow over
1:53
to the service, really really nice.