crusch

package module
v0.0.0-...-4456295 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 11 Imported by: 0

README

tests GoDoc

Crusch is a lightweight libary which provides tools for Github Apps to communicate with Githubs V3 API, without too much unnecessary hassle.

This libary provides a simple client structure to make requests to Githubs API. Clients aid with adding and creating the required authorization headers, keeping track of when they might need to be renewed and other small helper methods.

If you are looking for something more complete then go-github is probably for you. go-github is a more complete libary with types, seperate methods and bindings for every request github offers, which for what I was working on, was too complicated and quite annoying to work with when all I wanted was something simple, hence crusch.

Usage

import "github.com/weavc/crusch"

basic installation example

var v []map[string]interface{}
authorizer, err := crusch
    .NewInstallationAuth(<ApplicationID int64>, <InstallationID int64>, <rsaKey *rsa.PrivateKey>)

res, err := crusch.Client.Get(
    authorizer, 
    "/repos/weavc/crusch/issues", 
    "assignee=weavc&state=open", 
    &v)

new client

httpClient := &http.Client{}

client := crusch.NewGithubClient("api.github.com", "https")
client.SetHTTPClient(httpClient)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// GithubClient is the default GithubClient, using standard API url and https
	GithubClient = NewGithubClient("api.github.com", "https")
)

Functions

func AttachAuthorizer

func AttachAuthorizer(authorizer Authorizer, httpClient *http.Client) error

AttachAuthorizer attaches a new http.Transport layer that adds authorization headers to the request this new layer wraps any existing transport layers this can be used in conjuction with go-github to provide authorization headers to requests

func RSAPrivateKeyFromPEMFile

func RSAPrivateKeyFromPEMFile(keyfile string) (*rsa.PrivateKey, error)

RSAPrivateKeyFromPEMFile produces a *rsa.PrivateKey from a .pem file The .pem file is provided by Github to authorize your application against their API

Types

type ApplicationAuth

type ApplicationAuth struct {
	ApplicationID int64
	Key           *rsa.PrivateKey
}

ApplicationAuth creates authorization headers based on the given ApplicationID and private key Both are provided by Github, see: https://developer.github.com/v3/apps/#get-the-authenticated-github-app

func NewApplicationAuth

func NewApplicationAuth(applicationID int64, key *rsa.PrivateKey) (*ApplicationAuth, error)

NewApplicationAuth generates and returns a new ApplicationAuth struct using given values

func (*ApplicationAuth) Dispose

func (a *ApplicationAuth) Dispose()

Dispose of values in ApplicationAuth struct

func (*ApplicationAuth) GetHeader

func (a *ApplicationAuth) GetHeader() (string, error)

GetHeader to implement Authorizer GetHeader generates a new JWT token using the ApplicationID and PEM from Github This header is used for authenticating a Github application against Githubs api

type Authorizer

type Authorizer interface {
	GetHeader() (string, error)
}

Authorizer implements a GetHeader() method which returns the value used inside the authorization header i.e. bearer token It is used inside client request methods and provides the request with authorization headers

type AuthorizerFunc

type AuthorizerFunc func() (string, error)

AuthorizerFunc is a wrapper for the Authorizer interface it follows the a similar design to http.Handler

func (AuthorizerFunc) GetHeader

func (a AuthorizerFunc) GetHeader() (string, error)

GetHeader wraps AuthorizerFunc, implementing the Authorizer interface

type Client

type Client struct {
	URL      string
	Protocol string
	Headers  []header
	// contains filtered or unexported fields
}

Client is used to process requests to and from Githubs v3 api URL and protocols can be changed

func NewGithubClient

func NewGithubClient(url string, protocol string) *Client

NewGithubClient creates and returns a new GithubClient structure with given values

func (*Client) AddHeader

func (c *Client) AddHeader(name string, value string)

AddHeader adds headers to the array of headers used in the request

func (*Client) Delete

func (c *Client) Delete(authorizer Authorizer, uri string) (*http.Response, error)

Delete makes DELETE using the providers information

func (*Client) Do

func (c *Client) Do(authorizer Authorizer, req *http.Request, v interface{}) (*http.Response, error)

Do performs the given request using the providers details This will also bind the JSON response to v

func (*Client) Get

func (c *Client) Get(authorizer Authorizer, uri string, params interface{}, v interface{}) (*http.Response, error)

Get makes GET requests using the providers information Additional parameters/querystring can be passed through either as a string, struct or left as nil The response body will be bound to v

func (*Client) Patch

func (c *Client) Patch(authorizer Authorizer, uri string, body interface{}, v interface{}) (*http.Response, error)

Patch makes PATCH requests using the providers information A request body can be passed through and attempt to be converted to JSON, this can also be left as nil The response body will be bound to v

func (*Client) Post

func (c *Client) Post(authorizer Authorizer, uri string, body interface{}, v interface{}) (*http.Response, error)

Post makes POST requests using the providers information A request body can be passed through and attempt to be converted to JSON, this can also be left as nil The response body will be bound to v

func (*Client) Put

func (c *Client) Put(authorizer Authorizer, uri string, body interface{}, v interface{}) (*http.Response, error)

Put makes PUT requests using the providers information A request body can be passed through and attempt to be converted to JSON, this can also be left as nil The response body will be bound to v

func (*Client) RemoveHeader

func (c *Client) RemoveHeader(name string)

RemoveHeader headers to the array of headers used in the request

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(client *http.Client)

SetHTTPClient allows the http.Client on GithubClient to be changed http.DefaultClient is used by default

type InstallationAuth

type InstallationAuth struct {
	ApplicationID  int64
	InstallationID int64
	Key            *rsa.PrivateKey
	Client         *Client
	LastUsed
}

InstallationAuth allows applications to authenticate as an installation against Githubs API Uses the ApplcationID and Key to generate an ApplicationAuth which is inturn used to get installation token from githubs api. This will also store the Last used header and time instead of getting a new token for each request. https://developer.github.com/v3/apps/#create-a-new-installation-token

func NewInstallationAuth

func NewInstallationAuth(applicationID int64, installationID int64, key *rsa.PrivateKey) (*InstallationAuth, error)

NewInstallationAuth generates a new ApplicationAuth structure using given values

func (*InstallationAuth) Dispose

func (a *InstallationAuth) Dispose()

Dispose of values in InstallationAuth

func (*InstallationAuth) GetHeader

func (a *InstallationAuth) GetHeader() (string, error)

GetHeader to implement Authorizer This produces the required auth headers for the installation It will make a request to the Clients API (by default Github) to get an access token for the application and installation IDs provided by InstallationAuth If InstallationAuth has already generated an auth token and it is still valid, this will be used instead https://developer.github.com/v3/apps/#create-a-new-installation-token

type LastUsed

type LastUsed struct {
	// contains filtered or unexported fields
}

LastUsed details when a token/header was last used and when it needs to be reused

type OAuth

type OAuth struct {
	Token string
}

OAuth authorizor for Githubs v3 API https://developer.github.com/v3/#authentication

func NewOAuth

func NewOAuth(token string) (*OAuth, error)

NewOAuth generates and returns an OAuth authorizer that uses given values

func (*OAuth) Dispose

func (a *OAuth) Dispose()

Dispose of values stored inside of OAuth

func (*OAuth) GetHeader

func (a *OAuth) GetHeader() (string, error)

GetHeader to implement Authorizer returns the Authorization header required for oauth authentication

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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