http

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EncodeJson    = "_json"
	EncodeForm    = "_form"
	EncodeRaw     = "_raw"
	EncodeRawByte = "_raw_byte"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpClientConf

type HttpClientConf struct {
	Service         string        `yaml:"service"`
	Domain          string        `yaml:"domain"`
	Timeout         time.Duration `yaml:"timeout"`
	ConnectTimeout  time.Duration `yaml:"connectTimeout"`
	Retry           int           `yaml:"retry"`
	HttpStat        bool          `yaml:"httpStat"`
	Host            string        `yaml:"host"`
	Proxy           string        `yaml:"proxy"`
	MaxIdleConns    int           `yaml:"maxIdleConns"`
	IdleConnTimeout time.Duration `yaml:"idleConnTimeout"`
	// request body 最大长度展示,0表示采用默认的10240,-1表示不打印
	MaxReqBodyLen int `yaml:"maxReqBodyLen"`
	// response body 最大长度展示,0表示采用默认的10240,-1表示不打印。指定长度的时候需注意,返回的json可能被截断
	MaxRespBodyLen int `yaml:"maxRespBodyLen"`
	// 配置中设置了该值后当 err!=nil || httpCode >= retryHttpCode 时会重试(该策略优先级最低)
	RetryHttpCode int `yaml:"retryHttpCode"`

	HTTPClient *http.Client `json:"-"`

	BasicAuth struct {
		Username string `yaml:"username"`
		Password string `yaml:"password"`
	} `yaml:"basicAuth"`
	// contains filtered or unexported fields
}

func (*HttpClientConf) DoStream

func (client *HttpClientConf) DoStream(ctx *gin.Context, req *http.Request, opts *HttpRequestOptions, urlData []byte, f func(data string) error) (err error)

func (*HttpClientConf) GetRetryPolicy

func (client *HttpClientConf) GetRetryPolicy(opts *HttpRequestOptions) (retryPolicy RetryPolicy)

func (*HttpClientConf) GetTransPort

func (client *HttpClientConf) GetTransPort() *http.Transport

func (*HttpClientConf) HttpDelete

func (client *HttpClientConf) HttpDelete(ctx *gin.Context, path string, opts HttpRequestOptions) (*HttpResult, error)

func (*HttpClientConf) HttpGet

func (client *HttpClientConf) HttpGet(ctx *gin.Context, path string, opts HttpRequestOptions) (*HttpResult, error)

func (*HttpClientConf) HttpPost

func (client *HttpClientConf) HttpPost(ctx *gin.Context, path string, opts HttpRequestOptions) (*HttpResult, error)

func (*HttpClientConf) HttpPostStream

func (client *HttpClientConf) HttpPostStream(ctx *gin.Context, path string, opts HttpRequestOptions, f func(data string) error) (err error)

func (*HttpClientConf) HttpPut

func (client *HttpClientConf) HttpPut(ctx *gin.Context, path string, opts HttpRequestOptions) (*HttpResult, error)

func (*HttpClientConf) SetRetryPolicy

func (client *HttpClientConf) SetRetryPolicy(retry RetryPolicy)

type HttpRequestOptions

type HttpRequestOptions struct {
	// 通用请求体,可通过Encode来对body做编码
	RequestBody interface{}
	// 请求头指定
	Headers map[string]string
	// cookie 设定
	Cookies map[string]string
	/*
		httpGet / httPost 默认 application/x-www-form-urlencoded
		httpPostJson 默认 application/json
	*/
	ContentType string
	// 针对 RequestBody 的编码
	Encode string
	// 接口请求级timeout。不管retry是多少,那么每次执行的总时间都是timeout。
	// 这个timeout与client.Timeout 没有直接关系,总执行超时时间取二者最小值。
	Timeout time.Duration

	// 重试策略,可不指定,默认使用`defaultRetryPolicy`(只有在`api.yaml`中指定retry>0 时生效)
	RetryPolicy RetryPolicy `json:"-"`
}

func (*HttpRequestOptions) GetContentType

func (o *HttpRequestOptions) GetContentType() (cType string)

type HttpResult

type HttpResult struct {
	HttpCode int
	Response []byte
	Header   http.Header
	Ctx      *gin.Context
}

type RetryPolicy

type RetryPolicy func(resp *http.Response, err error) bool

retry 原因

Jump to

Keyboard shortcuts

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