Some of the responses from the GitHub API rely on the answers being in its cache. If you make a request and there is no available cache result, it returns an HTTP code (202) to let you know that you need to check back later (not very much later, but shortly). This is pretty standard and quite a few APIS work this way.

In Apps Script, one way would be to sleep for a while, and try again until you get a 200 code (or some failure), which means the data is ready. A better way is to use exponential backoff, which will wait increasingly longer periods of time and finally give up.

Exponential backoff is usually used for dealing with rate limiting, and of course the GitHub API also has rate limiting, so it’s ideal to use it for both purposes. You’ll find a write up on my exponential backoff here Custom checking for exponential backoff, along with where to get it.

Custom checking with exponential backoff

One of the the options to exponential backoff is a lookahead function. Here’s a simple example to detect a rate limit error for an API that signals such an error in its JSON response (as opposed to via an HTTP code). The lookahead functions returns true if it recognizes the response as a rate limit error, and false if it doesn’t recognize the response

GitHub API

Some of the requests to the github API (for example commit information), returns a 202 while it is preparing a response and its up to you to keep trying till you get a 200 or some error. In my main scripts, I can’t be bothered to worry about this so I’ve built it into the library I use for GitHub access and can simply forget all about it.

The github API is a little more complex that this as there is pagination to deal with, along with reading information from the response headers, so for my bulk github/apps script implementation, Getting your apps scripts to Github, I use my library cGitJsonAPI which knows how to deal with all of that

1_4RfsIW57fdzWh7T38O9IfGdTRgYbOSyC5PvsOm3a4GU1sxllw8blEUl

along with cUrlResult which uses exponential backoff behind the scenes

1NtAiJulZM4DssyN0HcK2XXTnykN_Ir2ee2pXV-CT367nKbdbTvRX4pTM

How to get commit data

Of course – this is pretty easy with the libraries (I use goa for managing access tokens – see Github service for Goa examples), so here’s the entire code for getting commit information on a given repo.

Which gives me this

Adding lookahead function to my cGitJsonApi,meant just a small change to provide a lookahead function to be passed to exponential backoff in the method that receives data from the JSON API and unpaginates them, like this

Of course there’s a lot going on around this, but the key point is that you can use exponential backoff to take care of many API quirks like this one and get the complication out of your main code and into utilities like this one. This lookahead simply checks for a 202 code – if it is, exponential backoff knows how to retry, how long to wait and how many times to attempt. If it’s not a 202, then processing will continue normally.

The full updated method looks like this

and is called like this

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.