httpclient

package
v0.0.0-...-41077f7 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package httpclient impl http client

Package httpclient impl http client

Index

Constants

View Source
const (
	// DialTimeout 建立 tcp 连接的超时时间
	DialTimeout = 15 * time.Second
	// ClientDefaultTimeout 从建立 tcp 到读完 response body 超时时间
	ClientDefaultTimeout = 60 * time.Second
)
View Source
const (
	MAX_RETRY_TIMES_FOR_TIMEOUT = 2
)

Variables

View Source
var (
	Retry5XX = RetryOption{3, 2, []RetryFunc{
		func(req *http.Request, resp *http.Response, respErr error) bool {
			return resp.StatusCode/100 == 5
		},
	}}

	RetryErrResp = RetryOption{3, 2, []RetryFunc{
		func(req *http.Request, resp *http.Response, respErr error) bool {
			return respErr != nil
		},
	}}

	NoRetry = RetryOption{}
)

Functions

func Loading

func Loading(onelineDesc string, f func(), disappearAfterDone bool, eraseCursor bool)

Types

type AfterDo

type AfterDo struct{ *Request }

func (AfterDo) Body

func (r AfterDo) Body(b *bytes.Buffer) (*Response, error)

适用于a) 如果成功,不关心body内容及其结构体;

并且b) 如果失败,需要把body内容封装进error里返回给上层定位错误

func (AfterDo) DiscardBody

func (r AfterDo) DiscardBody() (*Response, error)

func (AfterDo) JSON

func (r AfterDo) JSON(o interface{}) (*Response, error)

func (AfterDo) RAW

func (r AfterDo) RAW() (*http.Response, error)

func (AfterDo) StreamBody

func (r AfterDo) StreamBody() (io.ReadCloser, *Response, error)

StreamBody 返回 response body, 用于流式读取。 场景:k8s & marathon event. 注意:使用完后,需要自己 close body.

type BasicAuth

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

type DNSCache

type DNSCache struct {
	// 防止 refresh & lookup 中对 DnsCache.m 的并发访问
	sync.Mutex
	// contains filtered or unexported fields
}

DNSCache struct

func NewDNSCache

func NewDNSCache(refreshInterval time.Duration) *DNSCache

NewDNSCache 创建 DnsCache

type DefaultTracer

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

func NewDefaultTracer

func NewDefaultTracer(w io.Writer) *DefaultTracer

func (*DefaultTracer) TraceRequest

func (t *DefaultTracer) TraceRequest(req *http.Request)

func (*DefaultTracer) TraceResponse

func (t *DefaultTracer) TraceResponse(r *http.Response)

type HTTPClient

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

func New

func New(ops ...OpOption) *HTTPClient

func (*HTTPClient) BackendClient

func (c *HTTPClient) BackendClient() *http.Client

func (*HTTPClient) BasicAuth

func (c *HTTPClient) BasicAuth(user, password string) *HTTPClient

func (*HTTPClient) BearerTokenAuth

func (c *HTTPClient) BearerTokenAuth(token string) *HTTPClient

func (*HTTPClient) Delete

func (c *HTTPClient) Delete(host string, retry ...RetryOption) *Request

func (*HTTPClient) Get

func (c *HTTPClient) Get(host string, retry ...RetryOption) *Request

func (*HTTPClient) Head

func (c *HTTPClient) Head(host string, retry ...RetryOption) *Request

func (*HTTPClient) Method

func (c *HTTPClient) Method(method string, host string, retry ...RetryOption) *Request

func (*HTTPClient) Patch

func (c *HTTPClient) Patch(host string, retry ...RetryOption) *Request

func (*HTTPClient) Post

func (c *HTTPClient) Post(host string, retry ...RetryOption) *Request

func (*HTTPClient) Put

func (c *HTTPClient) Put(host string, retry ...RetryOption) *Request

func (*HTTPClient) TokenAuth

func (c *HTTPClient) TokenAuth(token string) *HTTPClient

type MultipartItem

type MultipartItem struct {
	Reader io.ReadCloser
	// Filename 当 Reader 为 *os.File 时,该值在 Content-Disposition 中生效;默认取 *os.File 的 baseName
	// Content-Disposition: form-data; name=${fieldName}; filename=${Filename}
	// +optional
	Filename string
}

type OpOption

type OpOption func(*Option)

func WithAcceptEncoding

func WithAcceptEncoding(ae string) OpOption

func WithClusterDialer

func WithClusterDialer(clusterKey string) OpOption

func WithCompleteRedirect

func WithCompleteRedirect() OpOption

func WithCookieJar

func WithCookieJar(jar http.CookieJar) OpOption

func WithDebug

func WithDebug(w io.Writer) OpOption

func WithDialerKeepAlive

func WithDialerKeepAlive(keepalive time.Duration) OpOption

func WithDnsCache

func WithDnsCache() OpOption

func WithEnableAutoRetry

func WithEnableAutoRetry(enableAutoRetry bool) OpOption

func WithHTTPS

func WithHTTPS() OpOption

func WithHttpsCertFromJSON

func WithHttpsCertFromJSON(certFile, keyFile, caCrt []byte) OpOption

func WithLoadingPrint

func WithLoadingPrint(desc string) OpOption

func WithProxy

func WithProxy(proxy string) OpOption

func WithTimeout

func WithTimeout(dialTimeout, clientTimeout time.Duration) OpOption

func WithTracer

func WithTracer(w io.Writer, tracer Tracer) OpOption

type Option

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

type Request

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

func (*Request) Cookie

func (r *Request) Cookie(v *http.Cookie) *Request

func (*Request) Do

func (r *Request) Do() AfterDo

func (*Request) FormBody

func (r *Request) FormBody(form url.Values) *Request

func (*Request) GetUrl

func (r *Request) GetUrl() string

func (*Request) Header

func (r *Request) Header(k, v string) *Request

func (*Request) Headers

func (r *Request) Headers(hs http.Header) *Request

func (*Request) JSONBody

func (r *Request) JSONBody(o interface{}) *Request

func (*Request) MultipartFormDataBody

func (r *Request) MultipartFormDataBody(fields map[string]MultipartItem) *Request

func (*Request) Param

func (r *Request) Param(k, v string) *Request

func (*Request) Params

func (r *Request) Params(kvs url.Values) *Request

func (*Request) Path

func (r *Request) Path(path string) *Request

func (*Request) RawBody

func (r *Request) RawBody(body io.Reader) *Request

type Response

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

Response 定义 http 应答对象.

func (*Response) Body

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

func (*Response) Headers

func (r *Response) Headers() http.Header

Headers 返回resp的header信息

func (*Response) IsBadRequest

func (r *Response) IsBadRequest() bool

IsBadRequest 返回 400.

func (*Response) IsConflict

func (r *Response) IsConflict() bool

IsConflict 返回 409 与否.

func (*Response) IsNotfound

func (r *Response) IsNotfound() bool

IsNotfound 返回 404 与否.

func (*Response) IsOK

func (r *Response) IsOK() bool

IsOK 返回 200 与否.

func (*Response) ResponseHeader

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

ResponseHeader 返回指定应答 header 值.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode return http status code.

type RetryFn

type RetryFn func() (*http.Response, error)

type RetryFunc

type RetryFunc func(*http.Request, *http.Response, error) bool

request response response error

type RetryOption

type RetryOption struct {
	MaxTime  int         // default 3
	Interval int         // second, default: 2s
	Fns      []RetryFunc // fn1 || fn2 || ...
}

type Tracer

type Tracer interface {
	TraceRequest(*http.Request)
	// if read response body, you need to set it back
	TraceResponse(*http.Response)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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