zgo.at/hubhub Index | Files

package hubhub

import "zgo.at/hubhub"

Package hubhub is a set of utility functions for working with the GitHub API.

It's not a "client library"; but just a few convenience functions that I found myself re-creating a bunch of times in different programs.


Package Files



var (
    User      string                     // GitHub username.
    Token     string                     // GitHub access token or password.
    API       = "https://api.github.com" // API base URL.
    DebugURL  = false                    // Show URLs as they're requested.
    DebugBody = false                    // Show body of requests.
    MaxWait   = 30 * time.Second         // Max time to wait on 202 Accepted responses.

Global configuration.

var ErrWait = errors.New("waited longer than MaxWait and still getting 202 Accepted")

ErrWait is used when we've waited longer than MaxWait for 202 Accepted.

func Paginate

func Paginate(scan interface{}, uri string, nPages int) error

Paginate an index request.

if nPages is higher than zero it will get exactly that number of pages in parallel. If it is zero it will get every page in serial until the last page.

The URL may contain query parameters, but the "page" parameter will be discarded.

TODO: Could be prettier.

func Request

func Request(scan interface{}, method, url string, body io.Reader) (*http.Response, error)

Request something from the GitHub API.

The response body is unmarshaled to scan unless the response code is 204 (No Content).

If the response code is 202 Accepted the HTTP request will be retried every two seconds until it returns a 200 OK. The ErrWait error will be returned if this takes longer than MaxWait.

A response code higher than 399 will return a NotOKError error, but won't affect the behaviour of this function.

The Body on the returned http.Response is closed.

This will use the global User and Token, which must be set.

type NotOKError

type NotOKError struct {
    Method, URL string
    Status      string
    StatusCode  int

NotOKError is used when the status code is not 200 OK.

func (NotOKError) Error

func (e NotOKError) Error() string
v0.0.0-20211017231734-35ca2b2ed259 (latest)
Oct 17, 2021
12 packages (graph)
Last checked
2 months ago

Tools for package owners.