client

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	http.Client // Underlying HTTP Client.
	// contains filtered or unexported fields
}

Client is the HTTP client for HTTP request management.

func New

func New() *Client

New creates and returns a new HTTP client object.

func (*Client) BasicAuth

func (c *Client) BasicAuth(user, pass string) *Client

BasicAuth is a chaining function, which sets HTTP basic authentication information for next request.

func (*Client) Clone

func (c *Client) Clone() *Client

Clone deeply clones current client and returns a new one.

func (*Client) Connect

func (c *Client) Connect(url string, data ...interface{}) (*Response, error)

Connect send CONNECT request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) ConnectBytes

func (c *Client) ConnectBytes(url string, data ...interface{}) []byte

ConnectBytes sends a CONNECT request, retrieves and returns the result content as bytes.

func (*Client) ConnectContent

func (c *Client) ConnectContent(url string, data ...interface{}) string

ConnectContent is a convenience method for sending CONNECT request, which retrieves and returns the result content and automatically closes response object.

func (*Client) ConnectVar

func (c *Client) ConnectVar(url string, data ...interface{}) *gvar.Var

ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) ContentJson

func (c *Client) ContentJson() *Client

ContentJson is a chaining function, which sets the HTTP content type as "application/json" for the next request.

Note that it also checks and encodes the parameter to JSON format automatically.

func (*Client) ContentType

func (c *Client) ContentType(contentType string) *Client

ContentType is a chaining function, which sets HTTP content type for the next request.

func (*Client) ContentXml

func (c *Client) ContentXml() *Client

ContentXml is a chaining function, which sets the HTTP content type as "application/xml" for the next request.

Note that it also checks and encodes the parameter to XML format automatically.

func (*Client) Cookie

func (c *Client) Cookie(m map[string]string) *Client

Cookie is a chaining function, which sets cookie items with map for next request.

func (*Client) Ctx

func (c *Client) Ctx(ctx context.Context) *Client

Ctx is a chaining function, which sets context for next request of this client.

func (*Client) Delete

func (c *Client) Delete(url string, data ...interface{}) (*Response, error)

Delete send DELETE request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) DeleteBytes

func (c *Client) DeleteBytes(url string, data ...interface{}) []byte

DeleteBytes sends a DELETE request, retrieves and returns the result content as bytes.

func (*Client) DeleteContent

func (c *Client) DeleteContent(url string, data ...interface{}) string

DeleteContent is a convenience method for sending DELETE request, which retrieves and returns the result content and automatically closes response object.

func (*Client) DeleteVar

func (c *Client) DeleteVar(url string, data ...interface{}) *gvar.Var

DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) DoRequest

func (c *Client) DoRequest(method, url string, data ...interface{}) (resp *Response, err error)

DoRequest sends request with given HTTP method and data and returns the response object. Note that the response object MUST be closed if it'll be never used.

Note that it uses "multipart/form-data" as its Content-Type if it contains file uploading, else it uses "application/x-www-form-urlencoded". It also automatically detects the post content for JSON format, and for that it automatically sets the Content-Type as "application/json".

func (*Client) Dump

func (c *Client) Dump(dump ...bool) *Client

Dump is a chaining function, which enables/disables dump feature for this request.

func (*Client) Get

func (c *Client) Get(url string, data ...interface{}) (*Response, error)

Get send GET request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) GetBytes

func (c *Client) GetBytes(url string, data ...interface{}) []byte

GetBytes sends a GET request, retrieves and returns the result content as bytes.

func (*Client) GetContent

func (c *Client) GetContent(url string, data ...interface{}) string

GetContent is a convenience method for sending GET request, which retrieves and returns the result content and automatically closes response object.

func (*Client) GetVar

func (c *Client) GetVar(url string, data ...interface{}) *gvar.Var

GetVar sends a GET request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Head

func (c *Client) Head(url string, data ...interface{}) (*Response, error)

Head send HEAD request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) HeadBytes

func (c *Client) HeadBytes(url string, data ...interface{}) []byte

HeadBytes sends a HEAD request, retrieves and returns the result content as bytes.

func (*Client) HeadContent

func (c *Client) HeadContent(url string, data ...interface{}) string

HeadContent is a convenience method for sending HEAD request, which retrieves and returns the result content and automatically closes response object.

func (*Client) HeadVar

func (c *Client) HeadVar(url string, data ...interface{}) *gvar.Var

HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Header

func (c *Client) Header(m map[string]string) *Client

Header is a chaining function, which sets custom HTTP headers with map for next request.

func (*Client) HeaderRaw

func (c *Client) HeaderRaw(headers string) *Client

Header is a chaining function, which sets custom HTTP header using raw string for next request.

func (*Client) Next

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

Next calls next middleware. This is should only be call in HandlerFunc.

func (*Client) Options

func (c *Client) Options(url string, data ...interface{}) (*Response, error)

Options send OPTIONS request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) OptionsBytes

func (c *Client) OptionsBytes(url string, data ...interface{}) []byte

OptionsBytes sends a OPTIONS request, retrieves and returns the result content as bytes.

func (*Client) OptionsContent

func (c *Client) OptionsContent(url string, data ...interface{}) string

OptionsContent is a convenience method for sending OPTIONS request, which retrieves and returns the result content and automatically closes response object.

func (*Client) OptionsVar

func (c *Client) OptionsVar(url string, data ...interface{}) *gvar.Var

OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Patch

func (c *Client) Patch(url string, data ...interface{}) (*Response, error)

Patch send PATCH request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) PatchBytes

func (c *Client) PatchBytes(url string, data ...interface{}) []byte

PatchBytes sends a PATCH request, retrieves and returns the result content as bytes.

func (*Client) PatchContent

func (c *Client) PatchContent(url string, data ...interface{}) string

PatchContent is a convenience method for sending PATCH request, which retrieves and returns the result content and automatically closes response object.

func (*Client) PatchVar

func (c *Client) PatchVar(url string, data ...interface{}) *gvar.Var

PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Post

func (c *Client) Post(url string, data ...interface{}) (*Response, error)

Post sends request using HTTP method POST and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) PostBytes

func (c *Client) PostBytes(url string, data ...interface{}) []byte

PostBytes sends a POST request, retrieves and returns the result content as bytes.

func (*Client) PostContent

func (c *Client) PostContent(url string, data ...interface{}) string

PostContent is a convenience method for sending POST request, which retrieves and returns the result content and automatically closes response object.

func (*Client) PostVar

func (c *Client) PostVar(url string, data ...interface{}) *gvar.Var

PostVar sends a POST request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Prefix

func (c *Client) Prefix(prefix string) *Client

Prefix is a chaining function, which sets the URL prefix for next request of this client.

func (*Client) Proxy

func (c *Client) Proxy(proxyURL string) *Client

Proxy is a chaining function, which sets proxy for next request. Make sure you pass the correct `proxyURL`. The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. Only `http` and `socks5` proxies are supported currently.

func (*Client) Put

func (c *Client) Put(url string, data ...interface{}) (*Response, error)

Put send PUT request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) PutBytes

func (c *Client) PutBytes(url string, data ...interface{}) []byte

PutBytes sends a PUT request, retrieves and returns the result content as bytes.

func (*Client) PutContent

func (c *Client) PutContent(url string, data ...interface{}) string

PutContent is a convenience method for sending PUT request, which retrieves and returns the result content and automatically closes response object.

func (*Client) PutVar

func (c *Client) PutVar(url string, data ...interface{}) *gvar.Var

PutVar sends a PUT request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) RedirectLimit

func (c *Client) RedirectLimit(redirectLimit int) *Client

RedirectLimit is a chaining function, which sets the redirect limit the number of jumps for the request.

func (*Client) RequestBytes

func (c *Client) RequestBytes(method string, url string, data ...interface{}) []byte

RequestBytes sends request using given HTTP method and data, retrieves returns the result as bytes. It reads and closes the response object internally automatically.

func (*Client) RequestContent

func (c *Client) RequestContent(method string, url string, data ...interface{}) string

RequestContent is a convenience method for sending custom http method request, which retrieves and returns the result content and automatically closes response object.

func (*Client) RequestVar

func (c *Client) RequestVar(method string, url string, data ...interface{}) *gvar.Var

RequestVar sends request using given HTTP method and data, retrieves converts the result to specified pointer. It reads and closes the response object internally automatically. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Retry

func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client

Retry is a chaining function, which sets retry count and interval when failure for next request.

func (*Client) SetAgent

func (c *Client) SetAgent(agent string) *Client

SetAgent sets the User-Agent header for client.

func (*Client) SetBasicAuth

func (c *Client) SetBasicAuth(user, pass string) *Client

SetBasicAuth sets HTTP basic authentication information for the client.

func (*Client) SetBrowserMode

func (c *Client) SetBrowserMode(enabled bool) *Client

SetBrowserMode enables browser mode of the client. When browser mode is enabled, it automatically saves and sends cookie content from and to server.

func (*Client) SetContentType

func (c *Client) SetContentType(contentType string) *Client

SetContentType sets HTTP content type for the client.

func (*Client) SetCookie

func (c *Client) SetCookie(key, value string) *Client

SetCookie sets a cookie pair for the client.

func (*Client) SetCookieMap

func (c *Client) SetCookieMap(m map[string]string) *Client

SetCookieMap sets cookie items with map.

func (*Client) SetCtx

func (c *Client) SetCtx(ctx context.Context) *Client

SetCtx sets context for each request of this client.

func (*Client) SetDump

func (c *Client) SetDump(dump bool) *Client

SetDump enables/disables dump feature for this request.

func (*Client) SetHeader

func (c *Client) SetHeader(key, value string) *Client

SetHeader sets a custom HTTP header pair for the client.

func (*Client) SetHeaderMap

func (c *Client) SetHeaderMap(m map[string]string) *Client

SetHeaderMap sets custom HTTP headers with map.

func (*Client) SetHeaderRaw

func (c *Client) SetHeaderRaw(headers string) *Client

SetHeaderRaw sets custom HTTP header using raw string.

func (*Client) SetPrefix

func (c *Client) SetPrefix(prefix string) *Client

SetPrefix sets the request server URL prefix.

func (*Client) SetProxy

func (c *Client) SetProxy(proxyURL string)

SetProxy set proxy for the client. This func will do nothing when the parameter `proxyURL` is empty or in wrong pattern. The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. Only `http` and `socks5` proxies are supported currently.

func (*Client) SetRedirectLimit

func (c *Client) SetRedirectLimit(redirectLimit int) *Client

SetRedirectLimit limit the number of jumps

func (*Client) SetRetry

func (c *Client) SetRetry(retryCount int, retryInterval time.Duration) *Client

SetRetry sets retry count and interval.

func (*Client) SetTimeout

func (c *Client) SetTimeout(t time.Duration) *Client

SetTimeOut sets the request timeout for the client.

func (*Client) Timeout

func (c *Client) Timeout(t time.Duration) *Client

TimeOut is a chaining function, which sets the timeout for next request.

func (*Client) Trace

func (c *Client) Trace(url string, data ...interface{}) (*Response, error)

Trace send TRACE request and returns the response object. Note that the response object MUST be closed if it'll be never used.

func (*Client) TraceBytes

func (c *Client) TraceBytes(url string, data ...interface{}) []byte

TraceBytes sends a TRACE request, retrieves and returns the result content as bytes.

func (*Client) TraceContent

func (c *Client) TraceContent(url string, data ...interface{}) string

TraceContent is a convenience method for sending TRACE request, which retrieves and returns the result content and automatically closes response object.

func (*Client) TraceVar

func (c *Client) TraceVar(url string, data ...interface{}) *gvar.Var

TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer. The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.

func (*Client) Use

func (c *Client) Use(handlers ...HandlerFunc) *Client

Use adds one or more middleware handlers to client.

type HandlerFunc

type HandlerFunc = func(c *Client, r *http.Request) (*Response, error)

HandlerFunc middleware handler func

type Response

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

Response is the struct for client request response.

func MiddlewareTracing

func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err error)

MiddlewareTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.

func (*Response) Close

func (r *Response) Close() error

Close closes the response when it will never be used.

func (*Response) GetCookie

func (r *Response) GetCookie(key string) string

GetCookie retrieves and returns the cookie value of specified <key>.

func (*Response) GetCookieMap

func (r *Response) GetCookieMap() map[string]string

GetCookieMap retrieves and returns a copy of current cookie values map.

func (*Response) Raw

func (r *Response) Raw() string

Raw returns the raw text of the request and the response.

func (*Response) RawDump

func (r *Response) RawDump()

RawDump outputs the raw text of the request and the response to stdout.

func (*Response) RawRequest

func (r *Response) RawRequest() string

RawRequest returns the raw content of the request.

func (*Response) RawResponse

func (r *Response) RawResponse() string

RawResponse returns the raw content of the response.

func (*Response) ReadAll

func (r *Response) ReadAll() []byte

ReadAll retrieves and returns the response content as []byte.

func (*Response) ReadAllString

func (r *Response) ReadAllString() string

ReadAllString retrieves and returns the response content as string.

Jump to

Keyboard shortcuts

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