client

package
v0.0.0-...-da45c02 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

client 包 @author cbping

Index

Constants

View Source
const (
	SlowReqRecord  = "SlowReqRecord"
	ReqRecord      = "ReqRecord"
	ErrorReqRecord = "ErrorReqRecord"
)
View Source
const (
	JSONResponseFormat = ResponseFormat("JSON")
	XMLResponseFormat  = ResponseFormat("XML")
)
View Source
const Version = "0.2"

Variables

This section is empty.

Functions

func SetDefaultClient

func SetDefaultClient(title string, client *http.Client)

设置DefaultClient

func SetProxy

func SetProxy(proxy func(*http.Request) (*url.URL, error))

设置代理 example:

func(req *http.Request) (*url.URL, error) {
	u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
	return u, nil
}

内部调用DefaultClient

func SetRecord

func SetRecord(record func(tag, msg string))

设置记录 内部调用DefaultClient

func SetRetryCount

func SetRetryCount(retryCount int)

设置失败尝试次数 内部调用DefaultClient

func SetSlowReqLong

func SetSlowReqLong(long time.Duration)

设置慢请求时间限制 内部调用DefaultClient

func SetTimeOut

func SetTimeOut(timeout time.Duration)

设置超时时间 内部调用DefaultClient

func SetUserAgent

func SetUserAgent(userAgent string)

func SetVersion

func SetVersion(version string)

Types

type BaseRequest

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

请求基类

实现请求接口的基类,所有请求对象继承必须继承此基类

func (*BaseRequest) Clone

func (b *BaseRequest) Clone() interface{}

func (*BaseRequest) GetTimeOut

func (b *BaseRequest) GetTimeOut() time.Duration

func (*BaseRequest) HttpRequest

func (b *BaseRequest) HttpRequest() (*http.Request, error)

func (*BaseRequest) SetReqCount

func (b *BaseRequest) SetReqCount(reqCount int)

func (*BaseRequest) String

func (b *BaseRequest) String() string

type Client

type Client struct {
	// 采用默认&http.Client{}
	*http.Client
	// contains filtered or unexported fields
}

客户端 处理http请求

var DefaultClient *Client

func NewClient

func NewClient(title string, client *http.Client) *Client

func (*Client) DoRequest

func (c *Client) DoRequest(req Request) (resp *Response, err error)

处理请求

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool)

func (*Client) SetProxy

func (c *Client) SetProxy(proxy func(*http.Request) (*url.URL, error))

设置代理 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) SetRecord

func (c *Client) SetRecord(record func(tag, msg string))

func (*Client) SetRetryCount

func (c *Client) SetRetryCount(retryCount int)

func (*Client) SetSlowReqLong

func (c *Client) SetSlowReqLong(long time.Duration)

func (*Client) SetTimeOut

func (c *Client) SetTimeOut(timeout time.Duration)

func (*Client) SetUserAgent

func (c *Client) SetUserAgent(userAgent string)

func (*Client) SetVersion

func (c *Client) SetVersion(version string)

type Request

type Request interface {
	//返回*http.Request
	HttpRequest() (*http.Request, error)
	//返回请求相关内容格式化字符串
	String() string
	// 获取超时时间
	// =0代表此请求不启用超时设置
	// <0代表默认使用全局
	// >0代表自定义超时时间
	// @deprecated  2017-09-26
	GetTimeOut() time.Duration
	//克隆
	Clone() interface{}
	//设置尝试次数
	SetReqCount(reqCount int)
}

请求接口

Client接受处理的请求接口

type Response

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

封装标准库中的Response 方便处理响应内容信息

func DoRequest

func DoRequest(req Request) (*Response, error)

处理请求,内部调用DefaultClient

func (*Response) Bytes

func (resp *Response) Bytes() ([]byte, error)

返回响应的Response的body字节内容

func (*Response) ToFile

func (resp *Response) ToFile(filename string) error

响应的Response的body字节内容保存到文件中去(文件请求)

func (*Response) ToJSON

func (resp *Response) ToJSON(v interface{}) error

将响应的Response的body字节内容以JSON格式转化

func (*Response) ToString

func (resp *Response) ToString() string

将响应的Response的body字节内容以字符串格式 如果为空的话,有可能是转化失败

func (*Response) ToXML

func (resp *Response) ToXML(v interface{}) error

将响应的Response的body字节内容以XML格式转化

type ResponseFormat

type ResponseFormat string

Jump to

Keyboard shortcuts

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