api

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package api encapsulates convenience functionality for interacting with well known APIs and related authentication mechanisms

Index

Constants

View Source
const (
	// PerPageResultsDefault defines the default page size for search query responses
	PerPageResultsDefault = 100
)

Variables

View Source
var (
	// WellKnownGithubTokenVarNames lists known env var names for Github related tokens
	WellKnownGithubTokenVarNames = []string{"GITHUB_TOKEN", "GH_TOKEN", "NPM_TOKEN"}
)

Functions

func GetNewOpts

func GetNewOpts(opts *github.SearchOptions, resp *github.Response) *github.SearchOptions

GetNewOpts provides github.SearchOptions optionally based on existing search options and an API response.

This facilitates setting proper search options for handling paging, properly, and can be used in a do-while loop that encapsulates and accumulates API responses.

allRepos := []*github.Repository{}
for opts = GetNewOpts(opts, nil); opts != nil; opts = GetNewOpts(opts, resp) {
	res, resp, err = gh.Search.Repositories(ctx, query, opts)
	if err != nil {
		return nil, err
	}
	allRepos = append(allRepos, res.Repositories...)
}

When opts and resp are provided, GetNewOpts will return nil if there is no page left to query.

func NewGithubClient

func NewGithubClient(authenticated *http.Client, gccf ...GithubClientCreationFunc) (*github.Client, error)

NewGithubClient conveniently creates a client connection to the Github API based on an already authenticated http client connection

If the authenticated client is not provided, a new client will be created by calling NewOAuthClient

Providing a parameter for gccf would allow creating the github client with a customized function

func NewGithubClientDefault

func NewGithubClientDefault(authenticated *http.Client) (*github.Client, error)

NewGithubClientDefault provides a client connection to the Github API based on an already authenticated http client connection

func NewOAuthStaticTokenSource

func NewOAuthStaticTokenSource(token string) oauth2.TokenSource

NewOAuthStaticTokenSource conveniently creates a oauth2.StaticTokenSource based on the provided token, which must be non-empty

Types

type EnvVarTokenProvider

type EnvVarTokenProvider struct {
	EnvvarNames []string
}

EnvVarTokenProvider takes names of env vars, that could hold string tokens

func (*EnvVarTokenProvider) Parse

func (p *EnvVarTokenProvider) Parse() (string, error)

Parse retrieves string tokens from env vars

type GithubClientCreationFunc added in v1.4.2

type GithubClientCreationFunc func(*http.Client) (*github.Client, error)

GithubClientCreationFunc defines the structure of a function, which is capable for creating an oauth2 client

type HTTPClient

type HTTPClient interface{}

HTTPClient is used here instead of *http.Client Casting can be considered as safe

func NewOAuthClient

func NewOAuthClient(ctx context.Context, stp StringTokenProvider, oaccf ...OAuth2ClientCreationFunc) (HTTPClient, error)

NewOAuthClient conveniently creates an oAuth2 client out of the provided factory function referred by oaccf OAuth2ClientCreationFunc and by using a string token provided via stp StringTokenProvider

If the provided context is empty, the context will be initialised with context.Background

If stp is not provided, the routine defaults to trying to retrieve tokens from default environment variables determined by WellKnownGithubTokenVarNames

Also if oaccf is not provided, the default oAuth2 client creation mechanism is triggered via NewOAuthClientDefault

func NewOAuthClientDefault

func NewOAuthClientDefault(ctx context.Context, ts oauth2.TokenSource) (HTTPClient, error)

NewOAuthClientDefault creates a default oAuth2 client

type OAuth2ClientCreationFunc

type OAuth2ClientCreationFunc func(ctx context.Context, ts oauth2.TokenSource) (HTTPClient, error)

OAuth2ClientCreationFunc defines the structure of a function, which is capable for creating an oauth2 client

type Queryable

type Queryable interface {
	Query(context.Context, *github.Client) (interface{}, error)
}

Queryable is able to query Github API and returns a data structure

type StringToken

type StringToken string

StringToken is a string that behaves like a StringTokenProvider

func (StringToken) Parse

func (s StringToken) Parse() (string, error)

Parse returns the string representation of the underlying type

type StringTokenProvider

type StringTokenProvider TokenProvider[string]

StringTokenProvider will provide string tokens to be processed by the TokenProvider

type TokenProvider

type TokenProvider[P any] interface {
	Parse() (P, error)
}

TokenProvider will provide the tokens to be processed by the TokenProvider

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL