Documentation ¶
Overview ¶
Package httpclient impl http client
Package httpclient impl http client
Index ¶
- Constants
- Variables
- type AfterDo
- type BasicAuth
- type DNSCache
- type DefaultTracer
- type HTTPClient
- func (c *HTTPClient) BackendClient() *http.Client
- func (c *HTTPClient) BasicAuth(user, password string) *HTTPClient
- func (c *HTTPClient) BearerTokenAuth(token string) *HTTPClient
- func (c *HTTPClient) Delete(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Get(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Head(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Method(method string, host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Patch(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Post(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) Put(host string, retry ...RetryOption) *Request
- func (c *HTTPClient) TokenAuth(token string) *HTTPClient
- type MultipartItem
- type OpOption
- func WithAcceptEncoding(ae string) OpOption
- func WithClusterDialer(clusterKey string) OpOption
- func WithCompleteRedirect() OpOption
- func WithCookieJar(jar http.CookieJar) OpOption
- func WithDebug(w io.Writer) OpOption
- func WithDialerKeepAlive(keepalive time.Duration) OpOption
- func WithDnsCache() OpOption
- func WithEnableAutoRetry(enableAutoRetry bool) OpOption
- func WithHTTPS() OpOption
- func WithHttpsCertFromJSON(certFile, keyFile, caCrt []byte) OpOption
- func WithLoadingPrint(desc string) OpOption
- func WithProxy(proxy string) OpOption
- func WithTimeout(dialTimeout, clientTimeout time.Duration) OpOption
- func WithTracer(w io.Writer, tracer Tracer) OpOption
- type Option
- type Request
- func (r *Request) Cookie(v *http.Cookie) *Request
- func (r *Request) Do() AfterDo
- func (r *Request) FormBody(form url.Values) *Request
- func (r *Request) GetUrl() string
- func (r *Request) Header(k, v string) *Request
- func (r *Request) Headers(hs http.Header) *Request
- func (r *Request) JSONBody(o interface{}) *Request
- func (r *Request) MultipartFormDataBody(fields map[string]MultipartItem) *Request
- func (r *Request) Param(k, v string) *Request
- func (r *Request) Params(kvs url.Values) *Request
- func (r *Request) Path(path string) *Request
- func (r *Request) RawBody(body io.Reader) *Request
- type Response
- func (r *Response) Body() []byte
- func (r *Response) Headers() http.Header
- func (r *Response) IsBadRequest() bool
- func (r *Response) IsConflict() bool
- func (r *Response) IsNotfound() bool
- func (r *Response) IsOK() bool
- func (r *Response) ResponseHeader(key string) string
- func (r *Response) StatusCode() int
- type RetryFn
- type RetryFunc
- type RetryOption
- type Tracer
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 ¶
This section is empty.
Types ¶
type AfterDo ¶
type AfterDo struct{ *Request }
func (AfterDo) DiscardBody ¶
func (AfterDo) StreamBody ¶
func (r AfterDo) StreamBody() (io.ReadCloser, *Response, error)
StreamBody 返回 response body, 用于流式读取。 场景:k8s & marathon event. 注意:使用完后,需要自己 close body.
type DNSCache ¶
type DNSCache struct { // 防止 refresh & lookup 中对 DnsCache.m 的并发访问 sync.Mutex // contains filtered or unexported fields }
DNSCache struct
func NewDNSCache ¶
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 WithClusterDialer ¶
func WithCompleteRedirect ¶
func WithCompleteRedirect() OpOption
func WithCookieJar ¶
func WithDialerKeepAlive ¶
func WithDnsCache ¶
func WithDnsCache() OpOption
func WithEnableAutoRetry ¶
func WithHttpsCertFromJSON ¶
func WithLoadingPrint ¶
func WithTimeout ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func (*Request) MultipartFormDataBody ¶
func (r *Request) MultipartFormDataBody(fields map[string]MultipartItem) *Request
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response 定义 http 应答对象.
func (*Response) ResponseHeader ¶
ResponseHeader 返回指定应答 header 值.
func (*Response) StatusCode ¶
StatusCode return http status code.
type RetryOption ¶
Click to show internal directories.
Click to hide internal directories.