Codecademy Practice

Github API

Challenge: We are going to create a script that can retrieve information about a specified GitHub user using the GitHub API.

HTTP Requests

What are HTTP Requests?

Every time the browser fetches data from a server (which could be a page, an image, a script, etc.) it does it using HTTP. HTTP is the H​yperText T​ransport P​rotocol. The server then sends back a response. An API is an easy way of fetching information from a remote service, in a way that’s easy for a computer to understand.

GitHub offers a simple API for viewing its current and historical server availability.

Availability means whether or not the GitHub website was accessible to users and accepting traffic. If your website is down, it is not available.

You can access an API in your web browser. Just type the following into the address bar:

https://www.githubstatus.com/api/v2/summary.json

If you are on a mac or a Linux/UNIX machine, you can access the API using curl:

curl https://www.githubstatus.com/api/v2/summary.json

Here is an example of the GET requests issued by this site. You can view any requests issued by a website by going to the Network tab in the developer tools of your browser.

Screenshot of this tutorial page showing the network tab in the developer tools of Firefox
The network tab shows information for every resource that was requested, including its HTTP status code, HTTP method, type, size, and time it spent in each request phase.

As part of the response, a request gives back a status code. You can use this to identify if the request was successful or not.

Some examples of status codes
Status code - message Description

200 - OK

Successful request

304 - Not modified

The page has not been modified since we last retrieved data

400 - Bad Request

The server did not understand the request

404 - Not Found

The server could not find the requested resource

HTTP Verbs

HTTP verbs are sent by the browser or client, and along with the URL used and data transmitted form part of the instruction to the API. There are several verbs, but in this tutorial we will be primarily using GET. GET is used to fetch information from an API. Another common verb is POST, which is used to create a new object on the remote service.

HTTP Headers

HTTP headers are metadata about the request that can be used by the server to obtain information about the request. You can see the headers of a resource if you click on it in the network tab of your browser’s developer tools.

Screenshot of the network tab in Firefox showing the headers of the html file
If you click on any resource in the network tab, you can see information about the request for that resource, for example the headers.

More information about HTTP: Follow the HTTP tutorial, in particular the methods, status codes and header fields.

To practice: The HTTP protocol, APIs, JSON, Loading API data into your application.

Step by step instructions:

We’ll build a small application that gives us back information about a GitHub user - we want to show their username, information and their picture.

GitHub offers an API where you can request information for a given username. The verb to use is GET, and the url is https://api.github.com/users/username. Try replacing username with your actual GitHub username. You can simply access this URL in your web browser by typing https://api.github.com/users/username into the address bar.

Again, to request the same URL in the terminal, you can use curl:

curl -XGET https://api.github.com/users/username

or, as GET is the default verb, just:

curl https://api.github.com/users/username

The response will look something like the JSON data below (this one is shortened):

{
  "login": "octocat",
  "id": 1,
  "avatar_url": "https://avatars.githubusercontent.com/u/9906?v=2",
  "gravatar_id": "",
  "html_url": "https://github.com/octocat",
  "type": "User",
  "name": "monalisa octocat",
  "company": "GitHub",
  "blog": "https://github.com/blog",
  "location": "San Francisco",
  "email": "octocat@github.com",
  "bio": "There once was...",
}

JSON data is what’s called key-value pairs, meaning that the name of the field is displayed immediately before the value. As you can see, the URL for the avatar (user’s icon) is in the "avatar_url" field, and is https://avatars.githubusercontent.com/u/9906?v=2.

Testing

When testing HTTP clients, you often need to simulate specific scenarios like returning a successful response, returning an error, or returning specific responses in a certain order. Because unit tests need to be predictable, easy to bootstrap, and fast, hitting an actual remote API is a test smell.

In order to test requests, we have to mock them, that is, intercept the request before it goes online and manually tell it what to return. That way we are in control.

We are going to use the external webmock library to mock requests. You can find it in its dedicated page on the rubygems site. Familiarize yourself with the gem and how you can use it to capture requests.

In the WebMock examples, they are not using rest-client to make the requests, they are using net-http. That is a native-Ruby library that does the same as rest-client, but it’s less intuitive to use than rest-client.

The docs show some testing examples, we can follow any of the examples for testing GET requests.

GET ting started (🥁)

Handling a successful request

If the request was successful, the status code will be 200. If we check that this code is 200, we know we can proceed to reading the data.

<username> is GitHub user #<userid>
Avatar: <avatarurl>
Link to profile: <link>

replace username, userid, avatarurl and link with the corresponding values.

Handling a failed request

We should always handle errors in requests. Check if the library you are using provides any custom errors that you can rescue.

Have a look again at the HTTP status codes. There is only one response code for a successfully request, anything else should be classed as a failure.

No such user!

Optional