xhttp

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2023 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 added in v0.0.6

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

BuildFormData ...

func BuildReader added in v0.0.6

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

BuildReader ...

func BuildURL added in v0.0.6

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

BuildURL ...

func BuildValues added in v0.0.6

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

BuildValues ...

func Get

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

Get ...

func ParseQuery added in v0.0.6

func ParseQuery(str string) map[string]string

ParseQuery ...

func ParseURL added in v0.0.6

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

ParseURL ...

func Post

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

Post ...

func ToResponse added in v0.0.6

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

ToResponse ...

func ToString

func ToString(r *Response, err error) string

ToString ...

Types

type Client added in v0.0.5

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

Client ...

func New added in v0.0.5

func New(options ...OptionFn) *Client

New ...

func (*Client) Delete added in v0.0.5

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

Delete ...

func (*Client) Do added in v0.0.5

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

Do ...

func (*Client) Get added in v0.0.5

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

Get ...

func (*Client) Patch added in v0.0.6

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

Patch ...

func (*Client) Post added in v0.0.5

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

Post ...

func (*Client) Put added in v0.0.5

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 added in v0.0.6

type OptionFn func(*Option)

OptionFn ...

func WithDialer added in v0.0.6

func WithDialer(v *net.Dialer) OptionFn

WithDialer ...

func WithRequestTimeout added in v0.0.6

func WithRequestTimeout(v time.Duration) OptionFn

WithRequestTimeout 请求超时

func WithRetryTimes added in v0.0.6

func WithRetryTimes(v int) OptionFn

WithRetryTimes 重试次数

func WithTraceEnable added in v0.0.6

func WithTraceEnable() OptionFn

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

func WithTransport added in v0.0.6

func WithTransport(v *http.Transport) OptionFn

WithTransport ...

type Response added in v0.0.6

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

Response ...

func (*Response) Bytes added in v0.0.6

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

Bytes ...

func (*Response) String added in v0.0.6

func (t *Response) String() string

String ...

func (*Response) Trace added in v0.0.6

func (t *Response) Trace() *Trace

Trace ...

type Trace added in v0.0.6

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 added in v0.0.6

func (t *Trace) Finish()

Finish ...

func (*Trace) WithClientTrace added in v0.0.6

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