Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/ ghrequestor Public archive

A resilient, rate-limit aware GitHub API client.

License

Notifications You must be signed in to change notification settings

microsoft/ghrequestor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version License Downloads

GHRequestor

A simple, resilient GitHub API client that:

  • Is great for bulk fetching of resources from the GitHub API.
  • Retries on errors and various forms of rate-limiting.
  • Backs off as you reach your API limit.
  • Automatically fetches all pages of a multi-page resource.
  • Uses etags to conserve API tokens and be kind to GitHub.
  • Reports comprehensive data on number of pages fetched, retry attempts, and length of delays.

GHRequestor is a relatively low-level facility intended for traversing large graphs of GitHub resources. Its primary usecase is GHCrawler, an engine that walks subsections of GitHub collecting related resources. This not intended to replace great modules like octonode or github.

Usage

The API is modelled after the standard node request libarary and the underlying requestretry library. It works with promises or callbacks and allows you to create pre-initialized requestor instances (with options setup) for injection into subsystems etc.

Simple GET of a single page

const ghrequestor = require('ghrequestor');
ghrequestor.get('https://api.github.com/repos/Microsoft/ghrequestor').then(response => {
  console.log(response.body);
});

GET a resource that spans multiple pages and flatten the responses into a single array. Notice the result includes an activity property detailing the requests and responses that went into getting the final result. You can swap getAll with getAllResponses to get back the raw responses rather than flattened array of values.

const ghrequestor = require('ghrequestor');
ghrequestor.getAll('https://api.github.com/repos/Microsoft/ghrequestor/commits').then(result => {
  console.log(result.length);
  console.log(result.activity.length);
  console.log(result.activity[0].attempts);
});

GET with a set of etags and a content supplier that has the etagged resources. With this you can integrate an in-memory or persistent cache of GitHub resources and let ghrequestor optimize the fetching and your use of API tokens. It's nicer on the GitHub infrastructure as well. Notice that the etag value include the "s.

const ghrequestor = require('ghrequestor');
const url = <some url>;
ghrequestor.getAllResponses(url, { etags: ['"42"']}).then(responses => {
  const supplier = url => { return yourContentLookupCode(); };
  return ghrequestor.flattenResponses(responses, supplier).then(results => {
    console.log(results.length);
  });
});

Authentication

Authentication is handled the same as with Node's request when using optoins and headers. You can either pass the authorization header in with each call or create a request template that has the authorization header set.

ghrequestor.get(url, { authorization: 'token <my token here>' });

Or

const requestorTemplate = ghrequestor.defaults({ authorization: 'token <my token here>' });
requestorTemplate.get(url);

Logging

ghrequestor takes a winston-style logger as a logger option. Set that option on each call or in a template

const winston = require('winston');
const requestorTemplate = ghrequestor.defaults({ logger: winston });
requestorTemplate.get(url);

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.