httpclient

package
v1.0.2050-b46fae6 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package httpclient provides an HTTP client instrumented with the ex/o11y package, it includes resiliency behaviour such as configurable timeouts, retries, authentication and connection pooling, with support for backing off when a 429 response code is seen.

Index

Constants

View Source
const JSON = "application/json; charset=utf-8"

Variables

View Source
var (
	ErrNoContent     = o11y.NewWarning("no content")
	ErrServerBackoff = errors.New("server requested explicit backoff")
)

Functions

func HasStatusCode

func HasStatusCode(err error, codes ...int) bool

HasStatusCode tests err for HTTPError and returns true if any of the codes match the stored code.

func IsNoContent

func IsNoContent(err error) bool

func IsRequestProblem

func IsRequestProblem(err error) bool

IsRequestProblem checks the err for HTTPError and returns true if the stored status code is in the 4xx range

func UnixTransport

func UnixTransport(socket string) *http.Transport

Types

type Client

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

Client is the o11y instrumented http client.

func New

func New(cfg Config) *Client

New creates a client configured with the config param

func (*Client) Call

func (c *Client) Call(ctx context.Context, r Request) (err error)

Call makes the request call. It will trace out a top level span and a span for any retry attempts. Retries will be attempted on any 5XX responses. If the http call completed with a non 2XX status code then an HTTPError will be returned containing details of result of the call.

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections is only used for testing.

type Config

type Config struct {
	// Name is used to identify the client in spans
	Name string
	// BaseURL is the URL and optional path prefix to the server that this is a client of.
	BaseURL string
	// AuthHeader the name of the header that the AuthToken will be set on. If empty then
	// the AuthToken will be used in a bearer token authorization header
	AuthHeader string
	// AuthToken is the token to use for authentication.
	AuthToken string
	// AcceptType if set will be used to set the Accept header.
	AcceptType string
	// Timeout is the maximum time any call can take including any retries.
	// Note that a zero Timeout is not defaulted, but means the client will retry indefinitely.
	Timeout time.Duration
	// MaxConnectionsPerHost sets the connection pool size
	MaxConnectionsPerHost int
	// UserAgent that will be used for every request
	UserAgent string
	// Transport allows overriding the default HTTP transport the client will use.
	Transport *http.Transport
}

Config provides the client configuration

type Decoder

type Decoder func(r io.Reader) error

func NewBytesDecoder

func NewBytesDecoder(resp *[]byte) Decoder

NewBytesDecoder decodes the response body into a byte slice

func NewJSONDecoder

func NewJSONDecoder(resp interface{}) Decoder

NewJSONDecoder returns a decoder func enclosing the resp param the func returned takes an io reader which will be passed to a json decoder to decode into the resp.

func NewStringDecoder

func NewStringDecoder(resp *string) Decoder

NewStringDecoder decodes the response body into a string

type HTTPError

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

HTTPError represents an error in an HTTP call when the response status code is not 2XX

func (*HTTPError) Code

func (e *HTTPError) Code() int

Code returns the status code recorded in this error.

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Is

func (e *HTTPError) Is(target error) bool

Is checks that this error is being checked for the special o11y error that is not added to the trace as an error. If the error is due to relatively expected failure response codes return true so it does not appear in the traces as an error.

type Request

type Request struct {
	Method string
	Route  string
	Body   interface{} // If set this will be sent as JSON

	// Deprecated: Use Decoders instead. If set will be used to decode the response body
	Decoder Decoder

	Decoders      map[int]Decoder // If set will be used to decode the response body by http status code
	Cookie        *http.Cookie
	Headers       map[string]string
	Timeout       time.Duration // The individual per call timeout
	Query         url.Values
	NoPropagation bool
	// contains filtered or unexported fields
}

Request is an individual http request that the Client will send

func NewRequest

func NewRequest(method, route string, timeout time.Duration, routeParams ...interface{}) Request

NewRequest should be used to create a new request rather than constructing a Request directly. This encourages the user to specify a "route" for the tracing, and avoid high cardinality routes (when parts of the url may contain many varying values). The returned Request can be further altered before being passed to the client.Call.

func (Request) AddDecoder

func (r Request) AddDecoder(status int, decoder Decoder) Request

AddDecoder returns a new request with a specific response body decoder to some http status code Note this will not modify the original request. Example usage:

err := NewRequest("POST", "/bad", time.Second).
	AddDecoder(400, NewStringDecoder(&s)).
 	Call(ctx, client)

func (Request) AddSuccessDecoder

func (r Request) AddSuccessDecoder(decoder Decoder) Request

AddSuccessDecoder returns a new request with a decoder for all 2xx statuses

func (Request) Call

func (r Request) Call(ctx context.Context, c *Client) error

Call is a convenience method to invoke call on the request itself

Jump to

Keyboard shortcuts

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