hihttp

package module
v0.0.0-...-464fe19 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: MIT Imports: 10 Imported by: 0

README

介绍

该库对go愿生http库的封装,添加了错误处理,超时处理及重试功能。

Todo

  • 错误处理;
  • 超时处理;
  • 重试功能;
  • 错误回调;
  • GET、POST支持;
  • DELETE、PUT、PATCH支持;

全局参数设置

hihttp.Load(
		hihttp.WithTimeout(time.Second), // 设置全局超时时间
		hihttp.WithRetryCount(1), // 设置全局重试次数
		hihttp.WithRetryWait(time.Second),// 设置全局重试等待时间
  	// 设置全局重试错误时的回调方法
		hihttp.WithRetryError(func(ctx context.Context, c hiclient) error {
			return nil
		}),
	)

GET示例

// 正常发送一个Get请求
res, err := hihttp.New().Get(context.Background(), "http://www.google.com")
if err != nil {
	t.Error(err)
}

// 添加header和cookie
hihttp.New().
	SetHeader("token", "1234567890").
	SetCookies(&http.Cookie{
			Name:  "token",
			Value: "abcdefg",
	}).
	Get(context.Background(), "http://www.google.com")

// 添加cookie

POST 示例

// -- multipart/form-data -- // 
// 以map的形式添加post参数
hihttp.New().Post(context.Background(), "http://www.yumontime.com/test/login", map[string]interface{}{
		"user_name": "yumontime", "password": "123123",
})
// 以[]string的形式添加post参数,第一个参数对应的是key,第二个参数对应的是value,以此类推
hihttp.New().Post(context.Background(), "http://www.yumontime.com/test/login", "user_name", "yumontime", "password", "123123")

// -- application/json -- //
hihttp.New().SetHeader(hihttp.SerializationType,hihttp.SerializationTypeJSON).Post(context.Background(), "http://www.yumontime.com/test/login", "user_name=yumontime&password=123123")

Documentation

Index

Constants

View Source
const (
	// MethodGet HTTP method
	GET = "GET"
	// MethodPost HTTP method
	POST = "POST"
	// MethodPut HTTP method
	PUT = "PUT"
	// MethodDelete HTTP method
	DELETE = "DELETE"
	// MethodDelete HTTP method
	PATCH = "PATCH"
)
View Source
const (
	SerializationType          string = "Content-Type"
	SerializationTypeFormData  string = "multipart/form-data"
	SerializationTypeJSON      string = "application/json"
	SerializationTypeWWWFrom   string = "application/x-www-form-urlencoded"
	SerializationTypePlainText string = "text/plain; charset=utf-8"
)

Variables

This section is empty.

Functions

func Load

func Load(opts ...Option)

设置client的全局参数

func NewFormPayload

func NewFormPayload(data map[string]interface{}) *formPayload

NewFormPayload 会根据序列化类型,生成一个payload

func NewJSONPayload

func NewJSONPayload(data interface{}) *jsonPayload

NewPayload 会根据序列化类型,生成一个payload

func NewKVParam

func NewKVParam(key string, value interface{}) *kvParam

func NewMapParams

func NewMapParams(m map[string]interface{}) *mapParams

func NewQueryParam

func NewQueryParam(query string) *queryParam

Types

type HiHTTP

type HiHTTP interface {
	Get(ctx context.Context, urlStr string, data ...Param) ([]byte, error)
	Post(ctx context.Context, urlStr string, p Payload) ([]byte, error)
	Put(ctx context.Context, urlStr string, p Payload) ([]byte, error)
	Delete(ctx context.Context, urlStr string, data ...Param) ([]byte, error)
	Patch(ctx context.Context, urlStr string, p Payload) ([]byte, error)
}

type Option

type Option func(*Options)

func WithRetryCount

func WithRetryCount(retryCount int) Option

func WithRetryError

func WithRetryError(retryError RetryErrorFunc) Option

func WithRetryWait

func WithRetryWait(retryWait time.Duration) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

type Options

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

几个公共参数

type Param

type Param interface {
	Marshal() string
}

type Payload

type Payload interface {
	Serialize() io.Reader
	ContentType() string
}

type Request

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

func New

func New(opts ...Option) *Request

公共参

func (*Request) Delete

func (r *Request) Delete(ctx context.Context, urlStr string, data ...Param) ([]byte, error)

func (*Request) Get

func (r *Request) Get(ctx context.Context, urlStr string, data ...Param) ([]byte, error)

send get request 也可以把参数直接放到URL后面,则data传nil即可

func (*Request) Patch

func (r *Request) Patch(ctx context.Context, urlStr string, p Payload) ([]byte, error)

func (*Request) Post

func (r *Request) Post(ctx context.Context, urlStr string, p Payload) ([]byte, error)

send post request

func (*Request) Put

func (r *Request) Put(ctx context.Context, urlStr string, p Payload) ([]byte, error)

func (*Request) SetCookies

func (r *Request) SetCookies(hc ...*http.Cookie) *Request

SetCookies 设置cookie

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader 以k-v格式设置header

func (*Request) SetHeaders

func (r *Request) SetHeaders(args map[string]string) *Request

Setting the header parameter should be 'map[string]string{}' Usually you need to set up 'Content-Type' Example: c.Headers(map[string]string{"key":"value"})

type RetryErrorFunc

type RetryErrorFunc func(ctx context.Context, c hiclient) error

Jump to

Keyboard shortcuts

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