resty

package
v1.8.12 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package resty HTTP and REST client library with parallel feature

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultDialTimeout default timeout of a dialer
	DefaultDialTimeout = 5 * time.Second
	// DefaultRequestTimeout default time out of a http request
	DefaultRequestTimeout = 10 * time.Second
	// DefaultRetry retry times if a request is failed with 5xx status code
	DefaultRetry = 3
)
View Source
var CreateClient = func(t *http.Transport, timeout time.Duration) Client {
	client := &http.Client{
		Transport: t,
	}
	if timeout > 0 {
		client.Timeout = timeout
	}

	return client
}

CreateClient a function that create a client instance

View Source
var DefaultTransport = &http.Transport{
	Proxy:                 http.ProxyFromEnvironment,
	MaxIdleConns:          1000,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   10 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
	MaxIdleConnsPerHost:   5,
	ForceAttemptHTTP2:     true,

	DialContext: (&net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
		DualStack: true,
	}).DialContext,
}

Run the HTTP request in a goroutine and pass the response to f.

Functions

This section is empty.

Types

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

Client http client

type Handle

type Handle func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error

Handle handler of http response

type Option

type Option func(*Resty)

Option set restry option

func WithClient added in v1.8.11

func WithClient(c Client) Option

WithClient set client

func WithHeader

func WithHeader(header map[string]string) Option

WithHeader set header for http request

func WithRequestInterceptor added in v1.7.3

func WithRequestInterceptor(interceptor func(req *http.Request) error) Option

WithRequestInterceptor intercept request

func WithRetry

func WithRetry(retry int) Option

WithRetry set retry times if request is failure with 5xx status code. retry is ingore if it is less than 1.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout set timeout of http request.

func WithTransport added in v1.7.4

func WithTransport(transport *http.Transport) Option

WithTransport set transport

type Resty

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

Resty HTTP and REST client library with parallel feature

func New

func New(opts ...Option) *Resty

New create a Resty instance.

func (*Resty) Do added in v1.7.3

func (r *Resty) Do(ctx context.Context, method string, body io.Reader, urls ...string) *Resty

func (*Resty) DoDelete added in v1.7.3

func (r *Resty) DoDelete(ctx context.Context, urls ...string) *Resty

DoDelete execute http requests with DELETE method in parallel

func (*Resty) DoGet

func (r *Resty) DoGet(ctx context.Context, urls ...string) *Resty

DoGet execute http requests with GET method in parallel

func (*Resty) DoPost added in v1.7.3

func (r *Resty) DoPost(ctx context.Context, body io.Reader, urls ...string) *Resty

DoPost execute http requests with POST method in parallel

func (*Resty) DoPut added in v1.7.3

func (r *Resty) DoPut(ctx context.Context, body io.Reader, urls ...string) *Resty

DoPut execute http requests with PUT method in parallel

func (*Resty) First added in v1.8.12

func (r *Resty) First() []error

First successful result or errors

func (*Resty) Then added in v1.7.4

func (r *Resty) Then(fn Handle) *Resty

Then callback for http response

func (*Resty) Wait

func (r *Resty) Wait() []error

Wait wait all of requests to done

type Result

type Result struct {
	Request      *http.Request
	Response     *http.Response
	ResponseBody []byte
	Err          error
}

Result result of a http request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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