core

package
v1.5.23 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT, Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

@author cbping

Index

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

func SetDefaultClient

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

设置DefaultClient

func SetProxy

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

设置代理 example:

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

内部调用DefaultClient

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

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

客户端 处理http请求

var DefaultClient *Client

func AppendHook

func AppendHook(hook ...Hook) *Client

func NewClient

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

NewClient

func NewClientCtx

func NewClientCtx(ctx Context, title string, client *http.Client) *Client

NewClientCtx

func SetContext

func SetContext(ctx Context) *Client

func SetMaxBadRetryCount

func SetMaxBadRetryCount(retryCount int) *Client

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

func SetTimeOut

func SetTimeOut(timeout time.Duration) *Client

设置超时时间 内部调用DefaultClient

func SetUserAgent

func SetUserAgent(userAgent string) *Client

func SetVersion

func SetVersion(version string) *Client

func (*Client) AppendHook

func (c *Client) AppendHook(hook ...Hook) *Client

func (*Client) DoRequest

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

处理请求

func (*Client) SetContext

func (c *Client) SetContext(ctx Context) *Client

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool) *Client

func (*Client) SetMaxBadRetryCount

func (c *Client) SetMaxBadRetryCount(retryCount int) *Client

func (*Client) SetProxy

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

设置代理 example:

	func(cqe *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) SetTimeOut

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

func (*Client) SetUserAgent

func (c *Client) SetUserAgent(userAgent string) *Client

func (*Client) SetVersion

func (c *Client) SetVersion(version string) *Client

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

	// 请求处理后执行
	// s 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

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) Close

func (resp *Response) Close() error

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