httpclient

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httpclient is *http.Client helpers

Index

Constants

This section is empty.

Variables

View Source
var ErrNonNilContext = errors.New("web/httpclient: context cannot be nil")

ErrNonNilContext when required context param is nil.

Functions

func LoggerMiddleware

func LoggerMiddleware(req *http.Request, handler mediary.Handler) (*http.Response, error)

LoggerMiddleware logs request and response of http calls.

func NewStdHTTPClient

func NewStdHTTPClient(opts ...Option) *http.Client

NewStdHTTPClient returns golang's default httpClient.

Types

type ContextHTTPClient

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

ContextHTTPClient wrapper of http.Client with required context param.

func NewContextHTTPClient

func NewContextHTTPClient(c *http.Client) *ContextHTTPClient

NewContextHTTPClient returns instance of ContextHTTPClient.

func (*ContextHTTPClient) Do

func (c *ContextHTTPClient) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it. If v is nil, and no error hapens, the response is returned as is. If rate limit is exceeded and reset time is in the future, Do returns *RateLimitError immediately without making a network API call.

The provided ctx must be non-nil, if it is nil an error is returned. If it is canceled or times out, ctx.Err() will be returned.

type Option

type Option func(*options)

Option sets options for http client.

func WithMaxConnection

func WithMaxConnection(m int) Option

WithMaxConnection returns an Option which sets http client connection pool size default is 50.

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout returns an Option which sets http client's timeout default is 5 seconds.

Jump to

Keyboard shortcuts

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