#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: A glimpse at an API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, before we go and write this, I want to give you a quick glimpse at how we define API's with Uplink. Just so you know, where we're going,
0:08
and what we're working with. So here is a, API, a structured API, granted, it only has one method we could
0:16
implement many others, That consumes the GitHub API. Now, there's a couple of interesting things here.
0:22
First of all, we have this function called list_repos. If you look at its implementation, it's empty. It's literally just a string, that says,
0:30
get the users public repository. This is the docstring so you get a little help about it. Technically you could just put the word, pass.
0:37
You don't actually have to write this function, you use the signature of the function to let Uplink hook into the API.
0:45
Notice there's an at get decorator, it has user /{user}/repos, and that {user}, anything that goes in the curly's, that actually becomes an argument.
0:55
So if we call this function, list_repos, and we say user equals 772, that's going to go into that URL,
1:02
and that's what the path annotation indicates here. There's also a sort_by, notice its a query. This is really cool, so the URL will actually be users,
1:11
slash, whatever you pass for users, slash repos, question mark, sort equals the value of sort_by. So, stars for example, something like that.
1:20
So, this way we'd basically declare or imitate our ways we're going to access the service, and it's almost entirely up to Uplink to make this happen.
1:31
Now, to use it is crazy simple. So we're going to come down here and we just create an, instance of this class, pass the URL.
1:37
We could hard code that in and we will in our example. Then you just call it, gitHub.list_repos, and you pass in say, octocat, that's the username,
1:45
and sort by its creted here, and you get the repos back, look at this. So, really, really nice way to create structured API's
1:54
that let you work with headers, body, query strings, the particular URL's and not actually have to juggle all those details.
2:03
So, that's what we're going to build for an API that we haven't even talked about yet. So, let's get to that.