httpc

package
v0.0.0-...-680e691 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeJSON = "application/json"
	TypeXML  = "application/xml"
	TypeForm = "application/x-www-form-urlencoded"
	TypeHTML = "text/html"
	TypeText = "text/plain"
)

预定义常见类型

View Source
const (
	CharsetUTF8 = "UTF-8"
)

Variables

View Source
var (
	ErrNoData      = errors.New("httpc: no data")
	ErrNotSupport  = errors.New("httpc: not support")
	ErrInvalidType = errors.New("httpc: invalid type")
)

some erorrs

Functions

func IsStatusErr

func IsStatusErr(e error) bool

IsStatusErr 判断是否是StatusErr错误

func SetDefault

func SetDefault(c *Client)

SetDefault 设置默认client

Types

type BaseHook

type BaseHook struct {
}

func (BaseHook) OnError

func (BaseHook) OnError(ctx context.Context, err error)

func (BaseHook) OnRequest

func (BaseHook) OnRequest(ctx context.Context, req *http.Request) error

func (BaseHook) OnResponse

func (BaseHook) OnResponse(ctx context.Context, rsp *http.Response) error

type CallOptions

type CallOptions struct {
	BaseUrl     string            // 默认url地址
	ContentType string            // 编码格式,默认application/json
	Charset     string            // 编码格式,默认空,若非空则与contentType拼接到一起,比如 text/plain;charset=UTF-8
	Params      map[string]string // path中参数,例如: http://www.baidu.com/im/v1/chats/:chat_id
	Query       url.Values        // query参数,例如:http://www.baidu.com?aa=xxx&bb=xxx
	Header      http.Header       // 消息头
	Cookies     []*http.Cookie    //
	Retry       int               // 重试次数
	Hook        Hook              // 回调函数
}

func (*CallOptions) AddAuthorization

func (o *CallOptions) AddAuthorization(auth string)

func (*CallOptions) AddBasicAuth

func (o *CallOptions) AddBasicAuth(username, password string)

func (*CallOptions) AddBearAuth

func (o *CallOptions) AddBearAuth(auth string)

func (*CallOptions) AddCookie

func (o *CallOptions) AddCookie(cookie *http.Cookie)

func (*CallOptions) AddHeader

func (o *CallOptions) AddHeader(key string, value interface{})

func (*CallOptions) AddParam

func (o *CallOptions) AddParam(key string, value string)

func (*CallOptions) AddQuery

func (o *CallOptions) AddQuery(key string, value interface{})

func (*CallOptions) AddXAuthToken

func (o *CallOptions) AddXAuthToken(token string)

func (*CallOptions) AddXJwtToken

func (o *CallOptions) AddXJwtToken(token string)

type Client

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

Client 简单封装http client,方便调用,注意,CallOptions最多只能传1个

1: 构建Request
2: body编解码
3: retry
4: hook
5: rsp wrapper

func NewClient

func NewClient(o *Config) *Client

NewClient 新建client

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Client) Options

func (c *Client) Options(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func (*Client) Put

func (c *Client) Put(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func (*Client) Trace

func (c *Client) Trace(ctx context.Context, url string, opts ...*CallOptions) *Response

type Config

type Config struct {
	Timeout          time.Duration
	DialTimeout      time.Duration
	HandshakeTimeout time.Duration
	KeepAlive        time.Duration
	BaseUrl          string
}

Config .

type Hook

type Hook interface {
	OnRequest(ctx context.Context, req *http.Request) error
	OnResponse(ctx context.Context, rsp *http.Response) error
	OnError(ctx context.Context, err error)
}

Hook 回调函数,可增加签名等通用处理

type Response

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

func Connect

func Connect(ctx context.Context, url string, opts ...*CallOptions) *Response

func Delete

func Delete(ctx context.Context, url string, opts ...*CallOptions) *Response

func Get

func Get(ctx context.Context, url string, opts ...*CallOptions) *Response
func Head(ctx context.Context, url string, opts ...*CallOptions) *Response

func Options

func Options(ctx context.Context, url string, opts ...*CallOptions) *Response

func Patch

func Patch(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func Post

func Post(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func Put

func Put(ctx context.Context, url string, reqBody interface{}, opts ...*CallOptions) *Response

func Trace

func Trace(ctx context.Context, url string, opts ...*CallOptions) *Response

func (*Response) Decode

func (rsp *Response) Decode(out interface{}) error

Decode 解析消息

func (*Response) Error

func (rsp *Response) Error() error

type StatusErr

type StatusErr struct {
	Code int    `json:"code"`
	Info string `json:"info"`
}

StatusErr 当返回码非http.StatusOk时,返回此错误

func (*StatusErr) Error

func (e *StatusErr) Error() string

Jump to

Keyboard shortcuts

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