httpclient

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httpclient provides a tiny http client with retry capability.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAuthorizationBearerHeaderToRequest

func AddAuthorizationBearerHeaderToRequest(req *http.Request, token string)

AddAuthorizationBearerHeaderToRequest adds bearer authorization header to request.

func NewJsonRequest

func NewJsonRequest(ctx context.Context, method,
	url string, data any) (*http.Request, error)

NewJsonRequest returns an *http.Request with a json encoded body.

func NewJsonRequestWithHeaders

func NewJsonRequestWithHeaders(ctx context.Context, method, url string,
	data any, headers map[string]string) (*http.Request, error)

NewJsonRequestWithHeaders returns an *http.Request with a json encoded body and specified headers.

func NewRequest

func NewRequest(ctx context.Context, method, url string) (*http.Request, error)

NewRequest returns an *http.Request.

func NewRequestWithHeaders

func NewRequestWithHeaders(ctx context.Context, method, url string,
	headers map[string]string) (*http.Request, error)

NewRequestWithHeaders returns an *http.Request with specified headers.

Types

type Client

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

Client represents an http client.

func New

func New(options ...Option) *Client

New returns a new Client.

func (*Client) SendRequest

func (c *Client) SendRequest(req *http.Request) (*http.Response, error)

SendRequest sends an HTTP request and returns an HTTP response.

func (*Client) SendRequestAndUnmarshallJsonResponse

func (c *Client) SendRequestAndUnmarshallJsonResponse(req *http.Request, v any) (*http.Response, error)

SendRequestAndUnmarshallJsonResponse sends an HTTP request \ and unmarshalls the responseBody to the given interface.

type HttpError

type HttpError struct {
	Url        string
	StatusCode int
	Body       string
	Err        error
}

HttpError is an error that wraps an HTTP response and/or an error.

func (*HttpError) Error

func (e *HttpError) Error() string

Error returns the error message. It implements the error interface.

func (*HttpError) Is

func (e *HttpError) Is(targetErr error) bool

Is returns true if the error is an HTTPError with the given status code, body and error.

type Option

type Option func(*Client)

Option represents a Client option.

func WithCheckRetryPolicy

func WithCheckRetryPolicy(checkRetryPolicy retryablehttp.CheckRetry) Option

WithCheckRetryPolicy specifies the policy for handling retries, and is called after each request.

func WithHttpClient

func WithHttpClient(httpClient *http.Client) Option

WithHttpClient adds a specified httpClient to be used.

func WithMaxConnsPerHost

func WithMaxConnsPerHost(n int) Option

WithMaxConnsPerHost limits the total number of connections per host.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns defines the maximum number of idle (keep-alive) connections across all hosts.

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) Option

WithMaxIdleConnsPerHost defines the maximum idle (keep-alive) connections to keep per-host.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries limits the maximum number of retries.

func WithRequestDumpLogger

func WithRequestDumpLogger(requestDumpLogger func(dump []byte), dumpRequestBody bool) Option

WithRequestDumpLogger specifies a function that receives the request dump along its body (optionally) for logging purposes.

func WithResponseDumpLogger added in v1.0.2

func WithResponseDumpLogger(responseDumpLogger func(dump []byte), dumpResponseBody bool) Option

WithResponseDumpLogger specifies a function that receives the response dump along its body (optionally) for logging purposes.

func WithRetryWaitMax

func WithRetryWaitMax(n time.Duration) Option

WithRetryWaitMax specifies maximum time to wait before retrying.

func WithRetryWaitMin

func WithRetryWaitMin(n time.Duration) Option

WithRetryWaitMin specifies minimum time to wait before retrying.

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout adds a timeout to the client.

Directories

Path Synopsis
retry

Jump to

Keyboard shortcuts

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