Python Fundamentals

App Solution 1: Fetching the Github API

Nina Zakharenko

Nina Zakharenko

Microsoft
Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "App Solution 1: Fetching the Github API" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to utilize the Github API to query for popular courses and sort them by the number of stars that they have. Nina begins live-coding the solution by showing how to begin using Github API.

Preview
Close

Transcript from the "App Solution 1: Fetching the Github API" Lesson

[00:00:05]
>> Nina Zakharenko: So you're gonna build a small app that uses the GitHub search API to query for popular repositories by a specific language. And sort those in descending order by the number of stars that they have. At this point, if you have not installed the requests library, you're gonna have to do that.

[00:00:31]
And just note that you may see pip being used directly, like pip install requests. But python -m pip is what's recommended by python. And that's gonna help you avoid any versioning errors. So I'm gonna make a new python file. I'm gonna call it day1.py. Remember, pip install, or python -m pip install requests, if you have not done so already.

[00:01:09]
I'll see some output, because I already have it installed, but,
>> Nina Zakharenko: Make sure that you see that as well. The first thing we'll need to do is import requests, so that we can use the requests library.
>> Nina Zakharenko: And we want to run our code in a main method.

[00:01:37]
So we're gonna say if __name__ is is equal to the string __main__, then we're gonna have a main method here.
>> Nina Zakharenko: For the time being, I just want to stub this method out. So I'm gonna put a Python construct called pass in here. And that just means do nothing, but if I save this, it's still valid code.

[00:02:07]
So we're gonna want to use the GitHub search API to get the repos with the most stars. I'm gonna make a new method called repos_with_most,
>> Nina Zakharenko: Excuse me, can't type today, repos_with_most_stars. And I'm going to get the URL for the GitHub search API.
>> Nina Zakharenko: And, [LAUGH] let me just grab that from the course exercise here.

[00:02:46]
In the class website, you'll find a link to the documentation for this API endpoint, if you want to learn more about it. Now just like before with the cute dogs, we're gonna say response is equal to requests.get. This time, we're passing in the GitHub API search URL. And we're gonna print the response.text.

[00:03:13]
>> Nina Zakharenko: Now in my main method, I'm gonna need to call this repos_with_most_stars method. So I'm just gonna paste that down here. And let's run our code and see what happens. Okay, we get a message here, a validation failure. And earlier in the chat, someone asked the type that might come back from the API.

[00:03:41]
Well, it'll be converted under the hood to the Python type that matches the JSON. So if the API returns a list of JSON objects, then in Python it will be a list of dictionaries. But here, response.text doesn't do any conversion. It just prints out the exact response that we got.

[00:04:01]
So we got a validation error.
>> Nina Zakharenko: And it's showing us the research, which is the search API. And it's saying that field q is missing. So we're gonna need to provide a query to get this to work.
>> Nina Zakharenko: Let's stub this out with some parameters.
>> Nina Zakharenko: We're gonna make a dictionary, and we're gonna make a new key, q.

[00:04:35]
And the value is gonna be stars, followed by colon, greater than 50,000.
>> Nina Zakharenko: These parameters are specific to the GitHub search API. They don't have anything to do with Python. So remember that each API that you work with is probably gonna work a little bit differently, and accept different values have different requirements.

[00:05:03]
Just like before, we're going to pass these parameters in using,
>> Nina Zakharenko: An argument called params. Let's try this again.
>> Nina Zakharenko: Okay, that spit out a bunch of stuff, right? Looks like it's working correctly. And now we can start refactoring. And actually doing something with the data that we got back.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now