#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,
0:01 I want to give you a quick glimpse at
0:03 how we define API's with Uplink.
0:06 Just so you know, where we're going,
0:07 and what we're working with.
0:09 So here is a, API, a structured API,
0:13 granted, it only has one method we could
0:15 implement many others, That consumes the GitHub API.
0:19 Now, there's a couple of interesting things here.
0:21 First of all, we have this function called list_repos.
0:24 If you look at its implementation, it's empty.
0:27 It's literally just a string, that says,
0:29 get the users public repository.
0:31 This is the docstring so you get a little help about it.
0:34 Technically you could just put the word, pass.
0:36 You don't actually have to write this function,
0:37 you use the signature of the function
0:40 to let Uplink hook into the API.
0:44 Notice there's an at get decorator,
0:45 it has user /{user}/repos,
0:49 and that {user}, anything that goes in the curly's,
0:51 that actually becomes an argument.
0:54 So if we call this function, list_repos,
0:57 and we say user equals 772, that's going to go into that URL,
1:01 and that's what the path annotation indicates here.
1:03 There's also a sort_by, notice its a query.
1:06 This is really cool, so the URL will actually be users,
1:10 slash, whatever you pass for users, slash repos,
1:13 question mark, sort equals the value of sort_by.
1:16 So, stars for example, something like that.
1:19 So, this way we'd basically declare or imitate our ways
1:25 we're going to access the service, and it's almost
1:27 entirely up to Uplink to make this happen.
1:30 Now, to use it is crazy simple.
1:32 So we're going to come down here and we just create an,
1:34 instance of this class, pass the URL.
1:36 We could hard code that in and we will in our example.
1:39 Then you just call it, gitHub.list_repos,
1:42 and you pass in say, octocat, that's the username,
1:44 and sort by its creted here,
1:47 and you get the repos back, look at this.
1:49 So, really, really nice way to create structured API's
1:53 that let you work with headers, body, query strings,
1:57 the particular URL's and not actually
2:00 have to juggle all those details.
2:02 So, that's what we're going to build for an API
2:05 that we haven't even talked about yet.
2:06 So, let's get to that.