client

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: Apache-2.0 Imports: 20 Imported by: 116

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error

Do performs the given http request and fills the given http response.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.©

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func DoDeadline

func DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error

DoDeadline performs the given request and waits for response until the given deadline.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned until the given deadline.

ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func DoRedirects

func DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, maxRedirectsCount int) error

DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects. When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

Response is ignored if resp is nil.

ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func DoTimeout

func DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error

DoTimeout performs the given request and waits for response during the given timeout duration.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned during the given timeout.

ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

Warning: DoTimeout does not terminate the request itself. The request will continue in the background and the response will be discarded. If requests take too long and the connection pool gets filled up please try using a Client and setting a ReadTimeout.

func Get

func Get(ctx context.Context, dst []byte, url string) (statusCode int, body []byte, err error)

Get returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

func GetDeadline

func GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error)

GetDeadline returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched until the given deadline.

func GetTimeout

func GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error)

GetTimeout returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched during the given timeout.

func Post

func Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args) (statusCode int, body []byte, err error)

Post sends POST request to the given url with the given POST arguments.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

Empty POST body is sent if postArgs is nil.

func WithClientReadTimeout

func WithClientReadTimeout(t time.Duration) config.ClientOption

WithClientReadTimeout sets maximum duration for full response reading (including body).

func WithDialFunc

func WithDialFunc(f client.DialFunc) config.ClientOption

WithDialFunc is used to set dialer function

NOTE: By default, hertz client uses dialer.DialConnection as DialFunc.

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) config.ClientOption

WithDialTimeout sets dial timeout.

func WithDialer

func WithDialer(d network.Dialer) config.ClientOption

WithDialer sets the specific dialer.

func WithDisableHeaderNamesNormalizing added in v0.2.0

func WithDisableHeaderNamesNormalizing(disable bool) config.ClientOption

WithDisableHeaderNamesNormalizing is used to set whether disable header names normalizing.

func WithDisablePathNormalizing added in v0.2.0

func WithDisablePathNormalizing(isDisablePathNormalizing bool) config.ClientOption

WithDisablePathNormalizing sets whether disable path normalizing.

func WithKeepAlive

func WithKeepAlive(b bool) config.ClientOption

WithKeepAlive determines whether use keep-alive connection.

func WithMaxConnDuration

func WithMaxConnDuration(t time.Duration) config.ClientOption

WithMaxConnDuration sets max connection duration, keep-alive connections are closed after this duration.

func WithMaxConnWaitTimeout

func WithMaxConnWaitTimeout(t time.Duration) config.ClientOption

WithMaxConnWaitTimeout sets maximum duration for waiting for a free connection.

func WithMaxConnsPerHost

func WithMaxConnsPerHost(mc int) config.ClientOption

WithMaxConnsPerHost sets maximum number of connections per host which may be established.

func WithMaxIdempotentCallAttempts

func WithMaxIdempotentCallAttempts(n int) config.ClientOption

WithMaxIdempotentCallAttempts sets maximum number of attempts for idempotent calls.

func WithMaxIdleConnDuration

func WithMaxIdleConnDuration(t time.Duration) config.ClientOption

WithMaxIdleConnDuration sets max idle connection duration, idle keep-alive connections are closed after this duration.

func WithName added in v0.2.0

func WithName(name string) config.ClientOption

WithName sets client name which used in User-Agent Header.

func WithNoDefaultUserAgentHeader added in v0.2.0

func WithNoDefaultUserAgentHeader(isNoDefaultUserAgentHeader bool) config.ClientOption

WithNoDefaultUserAgentHeader sets whether no default User-Agent header.

func WithResponseBodyStream

func WithResponseBodyStream(b bool) config.ClientOption

WithResponseBodyStream is used to determine whether read body in stream or not.

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) config.ClientOption

WithTLSConfig sets tlsConfig to create a tls connection.

Types

type Client

type Client struct {

	// Proxy specifies a function to return a proxy for a given
	// Request. If the function returns a non-nil error, the
	// request is aborted with the provided error.
	//
	// The proxy type is determined by the URL scheme.
	// "http" and "https" are supported. If the scheme is empty,
	// "http" is assumed.
	//
	// If Proxy is nil or returns a nil *URL, no proxy is used.
	Proxy protocol.Proxy

	// RetryIf controls whether a retry should be attempted after an error.
	//
	// By default will use isIdempotent function
	RetryIf func(request *protocol.Request) bool
	// contains filtered or unexported fields
}

Client implements http client.

Copying Client by value is prohibited. Create new instance instead.

It is safe calling Client methods from concurrently running goroutines.

func NewClient

func NewClient(opts ...config.ClientOption) (*Client, error)

NewClient return a client with options

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections closes any connections which were previously connected from previous requests but are now sitting idle in a "keep-alive" state. It does not interrupt any connections currently in use.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error

Do performs the given http request and fills the given http response.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

Response is ignored if resp is nil.

The function doesn't follow redirects. Use Get* for following redirects.

ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*Client) DoDeadline

func (c *Client) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error

DoDeadline performs the given request and waits for response until the given deadline.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned until the given deadline.

ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*Client) DoRedirects

func (c *Client) DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, maxRedirectsCount int) error

DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects. When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

Response is ignored if resp is nil.

ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*Client) DoTimeout

func (c *Client) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error

DoTimeout performs the given request and waits for response during the given timeout duration.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned during the given timeout.

ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

Warning: DoTimeout does not terminate the request itself. The request will continue in the background and the response will be discarded. If requests take too long and the connection pool gets filled up please try setting a ReadTimeout.

func (*Client) Get

func (c *Client) Get(ctx context.Context, dst []byte, url string, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)

Get returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

func (*Client) GetDeadline

func (c *Client) GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)

GetDeadline returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched until the given deadline.

func (*Client) GetOptions added in v0.2.0

func (c *Client) GetOptions() *config.ClientOptions

func (*Client) GetTimeout

func (c *Client) GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)

GetTimeout returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched during the given timeout.

func (*Client) Post

func (c *Client) Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)

Post sends POST request to the given url with the given POST arguments.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

Empty POST body is sent if postArgs is nil.

func (*Client) SetClientFactory added in v0.2.0

func (c *Client) SetClientFactory(cf suite.ClientFactory)

func (*Client) SetDialFunc added in v0.2.0

func (c *Client) SetDialFunc(dial func(addr string) (network.Conn, error))

SetDialFunc is used to set custom dial func.

func (*Client) SetProxy

func (c *Client) SetProxy(p protocol.Proxy)

SetProxy is used to set client proxy.

Don't SetProxy twice for a client. If you want to use another proxy, please create another client and set proxy to it.

func (*Client) SetRetryIf added in v0.2.0

func (c *Client) SetRetryIf(fn func(request *protocol.Request) bool)

SetRetryIf is used to set RetryIf func.

func (*Client) Use

func (c *Client) Use(mws ...Middleware)

type Endpoint

type Endpoint func(ctx context.Context, req *protocol.Request, resp *protocol.Response) (err error)

Endpoint represent one method for calling from remote.

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware deal with input Endpoint and output Endpoint.

Jump to

Keyboard shortcuts

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