httpcli

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StdClient = &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			DialContext: defaultTransportDialContext(&net.Dialer{
				Timeout:   3 * time.Second,
				KeepAlive: 30 * time.Second,
			}),
			ForceAttemptHTTP2:     true,
			MaxIdleConns:          512,
			MaxIdleConnsPerHost:   32,
			IdleConnTimeout:       90 * time.Second,
			TLSHandshakeTimeout:   5 * time.Second,
			ExpectContinueTimeout: 1 * time.Second,
			ReadBufferSize:        8 * 1024,
			WriteBufferSize:       8 * 1024,
		},
	}
)

Functions

func NoRetries

func NoRetries() (RetryCondition, Retryer)

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

type Client

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

func New

func New(opts ...Option) *Client

func NewWithClient

func NewWithClient(cli *http.Client, opts ...Option) *Client

func (*Client) Delete

func (c *Client) Delete(url string) *RequestBuilder

func (*Client) Get

func (c *Client) Get(url string) *RequestBuilder

func (*Client) GlobalRequestConfig

func (c *Client) GlobalRequestConfig() *GlobalRequestConfig

func (*Client) Patch

func (c *Client) Patch(url string) *RequestBuilder

func (*Client) Post

func (c *Client) Post(url string) *RequestBuilder

func (*Client) Put

func (c *Client) Put(url string) *RequestBuilder

type ErrorResponse

type ErrorResponse struct {
	Url        *url.URL
	StatusCode int
	Body       []byte
}

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

type GlobalRequestConfig

type GlobalRequestConfig struct {
	Timeout   time.Duration
	BaseUrl   string
	BasicAuth *BasicAuth
	Cookies   []*http.Cookie
	Headers   map[string]string
}

func NewGlobalRequestConfig

func NewGlobalRequestConfig() *GlobalRequestConfig

type Middleware

type Middleware func(next RoundTripper) RoundTripper

type MultipartData

type MultipartData struct {
	Files  map[string]MultipartFieldFile
	Values map[string]string
}

type MultipartFieldFile

type MultipartFieldFile struct {
	Filename string
	Reader   io.ReadCloser
}

type Option

type Option func(c *Client)

func WithMiddlewares

func WithMiddlewares(mws ...Middleware) Option

type Request

type Request struct {
	Raw *http.Request
	// contains filtered or unexported fields
}

func (*Request) Body

func (r *Request) Body() []byte

Body Returns request body in bytes Always returns empty slice if you use RequestBuilder.MultipartRequestBody

type RequestBodyWriter

type RequestBodyWriter interface {
	Write(req *http.Request, w io.Writer) error
}

type RequestBuilder

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

func NewRequestBuilder

func NewRequestBuilder(method string, url string, cfg *GlobalRequestConfig, execute func(ctx context.Context, req *RequestBuilder) (*Response, error)) *RequestBuilder

func (*RequestBuilder) BasicAuth

func (b *RequestBuilder) BasicAuth(ba BasicAuth) *RequestBuilder

func (*RequestBuilder) Cookie

func (b *RequestBuilder) Cookie(cookie *http.Cookie) *RequestBuilder

func (*RequestBuilder) Do

func (b *RequestBuilder) Do(ctx context.Context) (*Response, error)

func (*RequestBuilder) DoAndReadBody

func (b *RequestBuilder) DoAndReadBody(ctx context.Context) ([]byte, int, error)

func (*RequestBuilder) DoWithoutResponse

func (b *RequestBuilder) DoWithoutResponse(ctx context.Context) error

func (*RequestBuilder) FormDataRequestBody

func (b *RequestBuilder) FormDataRequestBody(data map[string][]string) *RequestBuilder

func (*RequestBuilder) Header

func (b *RequestBuilder) Header(name string, value string) *RequestBuilder

func (*RequestBuilder) JsonRequestBody

func (b *RequestBuilder) JsonRequestBody(value any) *RequestBuilder

func (*RequestBuilder) JsonResponseBody

func (b *RequestBuilder) JsonResponseBody(responsePtr any) *RequestBuilder

JsonResponseBody If response status code between 200 and 299, unmarshal response body to responsePtr

func (*RequestBuilder) MultipartRequestBody

func (b *RequestBuilder) MultipartRequestBody(data *MultipartData) *RequestBuilder

MultipartRequestBody Useful function to transfer "big" files because it does not load files into memory Does not support Request.Body in middlewares and ignores Retry

func (*RequestBuilder) QueryParams

func (b *RequestBuilder) QueryParams(queryParams map[string]any) *RequestBuilder

func (*RequestBuilder) RequestBody

func (b *RequestBuilder) RequestBody(body []byte) *RequestBuilder

func (*RequestBuilder) Retry

func (b *RequestBuilder) Retry(cond RetryCondition, retryer Retryer) *RequestBuilder

func (*RequestBuilder) StatusCodeToError

func (b *RequestBuilder) StatusCodeToError() *RequestBuilder

StatusCodeToError If set and Response.IsSuccess is false, Do return ErrorResponse as error

func (*RequestBuilder) Timeout

func (b *RequestBuilder) Timeout(timeout time.Duration) *RequestBuilder

Timeout Set per request attempt timeout, default timeout 15 seconds

type Response

type Response struct {
	Raw *http.Response
	// contains filtered or unexported fields
}

func (*Response) Body

func (r *Response) Body() ([]byte, error)

Body Read and return full response body Be careful, after calling Close returned data is no longer available Do not call close or copy slice if you want to use data outside the calling function

func (*Response) BodyCopy

func (r *Response) BodyCopy() ([]byte, error)

BodyCopy Return copy of response body Slice is available after calling Close

func (*Response) Close

func (r *Response) Close()

Close Release all resources associated with Response (buffer, tcp connection, context) After call, bytes slice returned by Body can not be used

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

func (*Response) StatusCode

func (r *Response) StatusCode() int

type ResponseBodyReader

type ResponseBodyReader interface {
	Read(r io.Reader) error
}

type RetryCondition

type RetryCondition func(err error, response *Response) error

func IfErrorOr5XXStatus

func IfErrorOr5XXStatus() RetryCondition

type Retryer

type Retryer interface {
	Do(ctx context.Context, f func() error) error
}

type RoundTripper

type RoundTripper interface {
	RoundTrip(ctx context.Context, request *Request) (*Response, error)
}

type RoundTripperFunc

type RoundTripperFunc func(ctx context.Context, request *Request) (*Response, error)

func (RoundTripperFunc) RoundTrip

func (f RoundTripperFunc) RoundTrip(ctx context.Context, request *Request) (*Response, error)

Jump to

Keyboard shortcuts

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