httpclient

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: Apache-2.0 Imports: 21 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewApiClient(ClientConfig{
	ErrorRetriable:     DefaultErrorRetriable,
	ErrorMapper:        DefaultErrorMapper,
	HTTPTimeout:        30 * time.Second,
	RetryTimeout:       5 * time.Minute,
	RateLimitPerSecond: 30,
})

Functions

func DefaultErrorMapper

func DefaultErrorMapper(ctx context.Context, resp *http.Response, body io.ReadCloser) error

func DefaultErrorRetriable

func DefaultErrorRetriable(ctx context.Context, err error) bool

Types

type ApiClient

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

func NewApiClient

func NewApiClient(cfg ClientConfig) *ApiClient

func (*ApiClient) Do

func (c *ApiClient) Do(ctx context.Context, method, path string, opts ...DoOption) error

Do sends an HTTP request against path.

func (*ApiClient) InContextForOAuth2

func (c *ApiClient) InContextForOAuth2(ctx context.Context) context.Context

InContextForOAuth2 returns a context with a custom *http.Client to be used for only for token acquisition through golang.org/x/oauth2 package

func (*ApiClient) RoundTrip

func (c *ApiClient) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper to integrate with golang.org/x/oauth2

type ClientConfig

type ClientConfig struct {
	Visitors []RequestVisitor

	RetryTimeout       time.Duration
	HTTPTimeout        time.Duration
	InsecureSkipVerify bool
	DebugHeaders       bool
	DebugTruncateBytes int
	RateLimitPerSecond int

	ErrorMapper     func(ctx context.Context, resp *http.Response, body io.ReadCloser) error
	ErrorRetriable  func(ctx context.Context, err error) bool
	TransientErrors []string

	Transport http.RoundTripper
}

type DoOption

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

func WithRequestData

func WithRequestData(body any) DoOption

WithRequestData takes either a struct instance, map, string, bytes, or io.Reader and sends it either as query string for GET and DELETE calls, or as request body for POST, PUT, and PATCH calls.

Experimental: this method may eventually be split into more granular options.

func WithRequestHeader

func WithRequestHeader(k, v string) DoOption

WithRequestHeader adds a request visitor, that sets a header on a request

func WithRequestHeaders

func WithRequestHeaders(headers map[string]string) DoOption

WithRequestHeaders adds a request visitor, that set all headers from a map

func WithRequestVisitor

func WithRequestVisitor(visitor func(r *http.Request) error) DoOption

WithRequestVisitor applies given function on a request

func WithResponseHeader

func WithResponseHeader(key string, value *string) DoOption

func WithResponseUnmarshal

func WithResponseUnmarshal(response any) DoOption

func WithTokenSource

func WithTokenSource(ts oauth2.TokenSource) DoOption

WithTokenSource uses the specified golang.org/x/oauth2 token source on a request

type HttpError

type HttpError struct {
	*http.Response
	Message string
	// contains filtered or unexported fields
}

func (*HttpError) Error

func (r *HttpError) Error() string

func (*HttpError) Unwrap

func (r *HttpError) Unwrap() error

type RequestVisitor

type RequestVisitor func(*http.Request) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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