xhttp

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodGet     = "GET"
	MethodPost    = "POST"
	MethodPut     = "PUT"
	MethodDelete  = "DELETE"
	MethodPatch   = "PATCH"
	MethodHead    = "HEAD"
	MethodOptions = "OPTIONS"
)

Method ...

View Source
const (
	HeaderKeyUserAgent       = "User-Agent"
	HeaderKeyAccept          = "Accept"
	HeaderKeyContentType     = "Content-Type"
	HeaderKeyContentLength   = "Content-Length"
	HeaderKeyContentEncoding = "Content-Encoding"
	HeaderKeyLocation        = "Location"
	HeaderKeyAuthorization   = "Authorization"
	HeaderKeyAcceptEncoding  = "Accept-Encoding"

	HeaderKeyContentTypeValueJSON     = "application/json"
	HeaderKeyContentTypeValueForm     = "application/x-www-form-urlencoded"
	HeaderKeyContentTypeValueFormData = "multipart/form-data"
	HeaderKeyContentEncodingValueGzip = "gzip"
)

HeaderKey header 字段, 同 http.CanonicalHeaderKey() 效果一致

Variables

...

Functions

func BuildFormData

func BuildFormData(header http.Header, m map[string]interface{}) (http.Header, io.Reader)

BuildFormData ...

func BuildReader

func BuildReader(data interface{}, types ...string) io.Reader

BuildReader ...

func BuildURL

func BuildURL(url string, m map[string]interface{}) string

BuildURL ...

func BuildValues

func BuildValues(m map[string]interface{}) url.Values

BuildValues ...

func Get

func Get(ctx context.Context, url string) string

Get ...

func ParseQuery

func ParseQuery(str string) map[string]string

ParseQuery ...

func ParseURL

func ParseURL(str string) (*url.URL, error)

ParseURL ...

func Post

func Post(ctx context.Context, url string, data interface{}) string

Post ...

func ToResponse

func ToResponse(r *Response, err error) (*http.Response, error)

ToResponse ...

func ToString

func ToString(r *Response, err error) string

ToString ...

Types

type Client

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

Client ...

func New

func New(options ...OptionFn) *Client

New ...

func (*Client) Delete

func (t *Client) Delete(ctx context.Context, url string, header http.Header) (*Response, error)

Delete ...

func (*Client) Do

func (t *Client) Do(
	ctx context.Context, method string, url string, header http.Header, data interface{},
) (*Response, error)

Do ...

func (*Client) Get

func (t *Client) Get(ctx context.Context, url string, header http.Header) (*Response, error)

Get ...

func (*Client) Patch

func (t *Client) Patch(ctx context.Context, url string, header http.Header) (*Response, error)

Patch ...

func (*Client) Post

func (t *Client) Post(ctx context.Context, url string, header http.Header, data interface{}) (*Response, error)

Post ...

func (*Client) Put

func (t *Client) Put(ctx context.Context, url string, header http.Header, data interface{}) (*Response, error)

Put ...

type Option

type Option struct {
	RetryTimes     int             // 请求重试次数
	TraceEnable    bool            // Trace 开关
	RequestTimeout time.Duration   // 请求超时
	Dialer         *net.Dialer     // dialer 配置
	Transport      *http.Transport // transport 配置
}

Option ...

type OptionFn

type OptionFn func(*Option)

OptionFn ...

func WithDialer

func WithDialer(v *net.Dialer) OptionFn

WithDialer ...

func WithRequestTimeout

func WithRequestTimeout(v time.Duration) OptionFn

WithRequestTimeout 请求超时

func WithRetryTimes

func WithRetryTimes(v int) OptionFn

WithRetryTimes 重试次数

func WithTraceEnable

func WithTraceEnable() OptionFn

WithTrace 可以通过返回 Response.Trace 查询

func WithTransport

func WithTransport(v *http.Transport) OptionFn

WithTransport ...

type Response

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

Response ...

func (*Response) Bytes

func (t *Response) Bytes() []byte

Bytes ...

func (*Response) String

func (t *Response) String() string

String ...

func (*Response) Trace

func (t *Response) Trace() *Trace

Trace ...

type Trace

type Trace struct {
	DNSLookup      time.Duration `json:"dns_lookup"`
	ConnTime       time.Duration `json:"conn_time"`
	TCPConnTime    time.Duration `json:"tcp_conn_time"`
	TLSHandshake   time.Duration `json:"tls_handshake"`
	ServerTime     time.Duration `json:"server_time"`
	ResponseTime   time.Duration `json:"response_time"`
	TotalTime      time.Duration `json:"total_time"`
	IsConnReused   bool          `json:"is_conn_reused"`
	IsConnWasIdle  bool          `json:"is_conn_was_idle"`
	ConnIdleTime   time.Duration `json:"conn_idle_time"`
	RequestAttempt int           `json:"request_attempt"`
	RemoteAddr     net.Addr      `json:"remote_addr"`
	// contains filtered or unexported fields
}

Trace ...

func (*Trace) Finish

func (t *Trace) Finish()

Finish ...

func (*Trace) WithClientTrace

func (t *Trace) WithClientTrace(ctx context.Context) context.Context

WithClientTrace ...

Jump to

Keyboard shortcuts

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