httpclient

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

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

Go to latest
Published: Jun 27, 2020 License: MIT Imports: 13 Imported by: 8

README

pinpt-logo

HTTPClient is a zero dependency client which supports pluggable retry and pagination

Setup

go get -u github.com/pinpt/httpclient

Usage

The most basic usage which mimics http.Default:

httpclient.Default.Get("https://foo.com")

Customize the client by setting Config:

config := httpclient.NewConfig()
config.Retryable = httpclient.NewBackoffRetry(time.Millisecond, time.Millisecond*50, 10*time.Second, 2)
client := httpclient.NewHTTPClient(context.Background(), config, http.DefaultClient)
resp, err := client.Get("https://foo.com")

Pluggable

The httpclient package is very customizable. You can pass in any implementation of the Client interface which http.Client implements. You can implement the Retryable and Paginator interfaces for customizing how to Retry failed requests and how to handle pagination.

License

Copyright © 2018-2019 by Pinpoint Software, Inc. MIT License. Pull requests welcome! 🙏🏻

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug when true will print additional retry details to console

View Source
var ErrInvalidClientImpl = errors.New("httpclient: invalid response from Do")

ErrInvalidClientImpl is an error that's returned when the Client Do returns nil to both response and error

View Source
var ErrRequestTimeout = errors.New("httpclient: timeout")

ErrRequestTimeout is an error that's returned when the max timeout is reached for a request

Functions

This section is empty.

Types

type Client

type Client interface {
	Get(url string) (*http.Response, error)
	Post(url string, contentType string, body io.Reader) (*http.Response, error)
	Do(req *http.Request) (*http.Response, error)
}

Client is an interface that mimics http.Client

var Default Client = NewHTTPClientDefault()

Default can be used to get a basic implementation of Client interface

type Config

type Config struct {
	Paginator Paginator
	Retryable Retryable
}

Config is the configuration for the HTTPClient

func NewConfig

func NewConfig() *Config

NewConfig returns an empty Config by no pagination and no retry

type HTTPClient

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

HTTPClient is an implementation of the Client interface

func NewHTTPClient

func NewHTTPClient(ctx context.Context, config *Config, client Client) *HTTPClient

NewHTTPClient returns a configured HTTPClient instance

func NewHTTPClientDefault

func NewHTTPClientDefault() *HTTPClient

NewHTTPClientDefault returns a default HTTPClient instance

func (*HTTPClient) Do

func (c *HTTPClient) Do(req *http.Request) (*http.Response, error)

Do will invoke the http request

func (*HTTPClient) Get

func (c *HTTPClient) Get(url string) (*http.Response, error)

Get is a convenience method for making a Get request to a url

func (*HTTPClient) Post

func (c *HTTPClient) Post(url string, contentType string, body io.Reader) (*http.Response, error)

Post is a convenience method for making a Post request to a url

type HTTPError

type HTTPError struct {
	Body       []byte
	StatusCode int
	URL        *url.URL
	Headers    http.Header
}

HTTPError is a struct which carries HTTP error details

func (*HTTPError) Error

func (h *HTTPError) Error() string

type Paginator

type Paginator interface {
	HasMore(page int, req *http.Request, resp *http.Response) (bool, *http.Request)
}

Paginator is an interface for handling request pagination

func InBodyPaginator

func InBodyPaginator() Paginator

InBodyPaginator returns a new paginator that it's pagination info it's inside body

func NewLinkPaginator

func NewLinkPaginator() Paginator

NewLinkPaginator returns a new Paginator that uses the HTTP Link Header for building the next request

func NoPaginator

func NoPaginator() Paginator

NoPaginator returns a no-op paginator that doesn't do pagination

type Retryable

type Retryable interface {
	RetryError(err error) bool
	RetryResponse(resp *http.Response) bool
	RetryDelay(retry int) time.Duration
	RetryMaxDuration() time.Duration
}

Retryable is an interface for retrying a request

func NewBackoffRetry

func NewBackoffRetry(initialTimeout time.Duration, incrementingTimeout time.Duration, maxTimeout time.Duration, exponentFactor float64) Retryable

NewBackoffRetry will return a Retryable that will support expotential backoff

func NewNoRetry

func NewNoRetry() Retryable

NewNoRetry will return a struct that implements Retryable but doesn't retry at all

Jump to

Keyboard shortcuts

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