Documentation ¶
Overview ¶
@author cbping
Index ¶
- Constants
- Variables
- func SetDefaultClient(title string, client *http.Client)
- func SetProxy(proxy func(*http.Request) (*url.URL, error))
- type BaseRequest
- func (b *BaseRequest) Clone() interface{}
- func (b *BaseRequest) HookData(key string) (data interface{}, ok bool)
- func (b *BaseRequest) HttpRequest() (*http.Request, error)
- func (b *BaseRequest) ReqCount() int
- func (b *BaseRequest) ReqLongTime() time.Duration
- func (b *BaseRequest) Response() *Response
- func (b *BaseRequest) ServerName() string
- func (b *BaseRequest) SetHookData(key string, data interface{}) (ok bool)
- func (b *BaseRequest) String() string
- func (b *BaseRequest) TimeOut() time.Duration
- type Client
- func AppendHook(hook ...Hook) *Client
- func NewClient(title string, client *http.Client) *Client
- func NewClientCtx(ctx Context, title string, client *http.Client) *Client
- func SetContext(ctx Context) *Client
- func SetMaxBadRetryCount(retryCount int) *Client
- func SetTimeOut(timeout time.Duration) *Client
- func SetUserAgent(userAgent string) *Client
- func SetVersion(version string) *Client
- func (c *Client) AppendHook(hook ...Hook) *Client
- func (c *Client) DoRequest(req Request) (resp *Response, err error)
- func (c *Client) SetContext(ctx Context) *Client
- func (c *Client) SetDebug(debug bool) *Client
- func (c *Client) SetMaxBadRetryCount(retryCount int) *Client
- func (c *Client) SetProxy(proxy func(*http.Request) (*url.URL, error))
- func (c *Client) SetTimeOut(timeout time.Duration) *Client
- func (c *Client) SetUserAgent(userAgent string) *Client
- func (c *Client) SetVersion(version string) *Client
- type Context
- type Hook
- type Request
- type Response
- type ResponseFormat
Constants ¶
View Source
const ( JSONResponseFormat = ResponseFormat("JSON") XMLResponseFormat = ResponseFormat("XML") )
View Source
const Version = "1.0"
Variables ¶
View Source
var ( RawRespNilErr = errors.New("raw http response is nil. ") RawRespBodyNilErr = errors.New("the body of raw http response is nil. ") )
Functions ¶
Types ¶
type BaseRequest ¶
type BaseRequest struct { Resp *Response // contains filtered or unexported fields }
请求基类
实现请求接口的基类,所有请求对象继承必须继承此基类
func (*BaseRequest) Clone ¶
func (b *BaseRequest) Clone() interface{}
func (*BaseRequest) HookData ¶
func (b *BaseRequest) HookData(key string) (data interface{}, ok bool)
func (*BaseRequest) HttpRequest ¶
func (b *BaseRequest) HttpRequest() (*http.Request, error)
返回*http.Request
func (*BaseRequest) ReqCount ¶
func (b *BaseRequest) ReqCount() int
func (*BaseRequest) ReqLongTime ¶
func (b *BaseRequest) ReqLongTime() time.Duration
func (*BaseRequest) Response ¶
func (b *BaseRequest) Response() *Response
func (*BaseRequest) ServerName ¶
func (b *BaseRequest) ServerName() string
func (*BaseRequest) SetHookData ¶
func (b *BaseRequest) SetHookData(key string, data interface{}) (ok bool)
func (*BaseRequest) String ¶
func (b *BaseRequest) String() string
func (*BaseRequest) TimeOut ¶
func (b *BaseRequest) TimeOut() time.Duration
type Client ¶
客户端 处理http请求
var DefaultClient *Client
func AppendHook ¶
func NewClientCtx ¶
NewClientCtx
func SetContext ¶
func SetMaxBadRetryCount ¶
设置失败尝试次数 内部调用DefaultClient
func SetUserAgent ¶
func SetVersion ¶
func (*Client) AppendHook ¶
func (*Client) SetContext ¶
func (*Client) SetMaxBadRetryCount ¶
func (*Client) SetProxy ¶
设置代理 example:
func(req *http.Request) (*url.URL, error) { u, _ := url.ParseRequestURI("http://127.0.0.1:8118") return u, nil } 你也可以通过设置环境变量 HTTP_PROXY 来设置代理,如: os.Setenv("HTTP_PROXY", "http://127.0.0.1:8888")
func (*Client) SetUserAgent ¶
func (*Client) SetVersion ¶
type Context ¶
type Context interface { Done() <-chan struct{} Err() error }
为了兼容1.7或者更早的版本
func BackgroundContext ¶
func BackgroundContext() Context
type Hook ¶
type Hook interface { // 请求处理前执行 // 如果返回错误 // 将提前终止请求 // 并将此错误返回 BeforeRequest(req Request, client Client) error // 请求处理后执行 // @params err 请求处理错误信息,如果不为nil,代表请求失败 AfterRequest(cErr error, req Request, client Client) }
钩子接口
type Request ¶
type Request interface { //返回*http.Request HttpRequest() (*http.Request, error) // 返回请求服务名 // 以便归类不同服务的统计数据 // 如果使用断路器,则必须根据不同服务端返回不同名字。 // 以 主机+端口 是一个不错的选择 ServerName() string // Clone() interface{} String() string ReqCount() int // 钩子存放获取数据 SetHookData(key string, data interface{}) (ok bool) HookData(key string) (data interface{}, ok bool) Response() *Response ReqLongTime() time.Duration // contains filtered or unexported methods }
请求接口
type Response ¶
封装标准库中的Response 方便处理响应内容信息
type ResponseFormat ¶
type ResponseFormat string
Click to show internal directories.
Click to hide internal directories.