bearer

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: Apache-2.0 Imports: 16 Imported by: 2

README

Bearer Golang

😄 Bearer API Client for Golang

GoDoc License Go Report Card GolangCI

Installation

go get github.com/Bearer/bearer-go

Usage

Get your Bearer Secret Key and integration ID from the Dashboard and use the Bearer client as follows:

import "github.com/Bearer/bearer-go"

func main() {
        // configure the default HTTP client to use Bearer
        bearer.ReplaceGlobals(bearer.Init(os.Getenv("BEARER_SECRETKEY")))

        // then use your app normally:
        resp, _ := http.Get("...")
        fmt.Println("response: ", resp)
}

See more documentation and examples on GoDoc

Development

# test
$ go test -v ./... -race

# lint
$ golint ./...

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"
	"os"

	bearer "github.com/Bearer/bearer-go"
)

func main() {
	bearer.ReplaceGlobals(bearer.Init(os.Getenv("BEARER_SECRETKEY")))

	// perform request
	resp, err := http.Get("...")
	if err != nil {
		panic(err)
	}
	fmt.Println("resp", resp)
}
Output:

Example (Advanced)
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	bearer "github.com/Bearer/bearer-go"
	"go.uber.org/zap"
)

func main() {
	logger, _ := zap.NewDevelopment()
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	agent := &bearer.Agent{
		SecretKey: os.Getenv("BEARER_SECRETKEY"),
		Logger:    logger,
		Transport: http.DefaultTransport,
		Context:   ctx,
	}
	defer agent.Flush()
	client := &http.Client{Transport: agent}

	// perform request
	resp, err := client.Get("...")
	if err != nil {
		panic(err)
	}
	fmt.Println("resp", resp)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlockedDomain is raised when your program tries to make requests to a blacklisted domain.
	ErrBlockedDomain = errors.New("bearer: blocked domain")
)

Functions

func ReplaceGlobals

func ReplaceGlobals(n http.RoundTripper) func()

ReplaceGlobals replaces the global http.DefaultTransport, and returns a function to restore the original value.

Types

type Agent

type Agent struct {
	// Agent implements the http.RoundTripper interface
	http.RoundTripper

	// SecretKey is your Bearer Secret Key; available on https://app.bearer.sh/keys
	// Required
	SecretKey string

	// If set, the RoundTripper interface actually used to make requests
	// If nil, an equivalent of http.DefaultTransport is used
	Transport http.RoundTripper

	// If set, will be used for internal logging.
	Logger *zap.Logger

	// If set, this context will be used by the agent for managing its internal goroutines
	// and performing operational requests.
	Context context.Context

	// Duration between two config refreshes.
	// If empty, will use 5s as default.
	RefreshConfigEvery time.Duration
	// contains filtered or unexported fields
}

Agent is the main object of this library. You need to initialize an agent first, and then you need to configure your HTTP clients to use it as a RoundTripper.

func Init

func Init(secretKey string) *Agent

Init configures the default http.DefaultTransport with sane default values

func (Agent) Config

func (a Agent) Config() (*Config, error)

Config fetches and returns a fresh Bearer configuration for your current token

func (Agent) Flush

func (a Agent) Flush() error

Flush flushes any buffered log entries. Applications should take care to call Flush before exiting.

func (*Agent) RoundTrip

func (a *Agent) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface

type Config

type Config struct {
	BlockedDomains []string `json:"blockedDomains"`
}

Config is retrieved from Bearer's API.

Jump to

Keyboard shortcuts

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