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 (Agent) Config ¶
Config fetches and returns a fresh Bearer configuration for your current token
Click to show internal directories.
Click to hide internal directories.