reqKit

package
v3.0.916 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypePlainText = "text/plain; charset=utf-8"

	ContentTypeJson = "application/json; charset=utf-8"

	ContentTypeXml = "text/xml; charset=utf-8"

	ContentTypeForm = "application/x-www-form-urlencoded; charset=utf-8"
)
View Source
const (
	// DefaultTimeout 默认超时时间
	DefaultTimeout = time.Second * 30
)

Variables

View Source
var (
	NewLogger func(output io.Writer, prefix string, flag int) req.Logger = req.NewLogger

	NewLoggerFromStandardLogger func(l *log.Logger) req.Logger = req.NewLoggerFromStandardLogger
)

Functions

func Get

func Get(ctx context.Context, url string, queryParams map[string][]string) *req.Response

Get

PS: 要注意 req.Response 结构体的 Err 字段.

@param queryParams 可以为nil

func GetAndInto added in v3.0.911

func GetAndInto(ctx context.Context, url string, queryParams map[string][]string, v interface{}) error

func GetGlobalClient added in v3.0.911

func GetGlobalClient() (client *req.Client)

func NewClient

func NewClient(options ...ClientOption) (client *req.Client)

NewClient

!!!: (1) 不要每次发请求都创建 Client,造成不必要的开销,通常可以复用同一 Client 发所有请求; (2) 自动重试(retry): 可以在返回值上自行添加.

@param options 不传参的情况下, (1) 生产模式

(2) 超时时间: 30s
(3) 跳过https证书验证
(4) 日志输出: os.Stdout
(5) 不重试

func Post

func Post(ctx context.Context, url string, contentType string, body interface{}) *req.Response

Post TODO: 待验证.

PS: 要注意 req.Response 结构体的 Err 字段.

@param body Set the request Body, accepts string、[]byte、io.Reader、map and struct.

func ReplaceGlobalClient added in v3.0.911

func ReplaceGlobalClient(client *req.Client)

func SimpleGet added in v3.0.911

func SimpleGet(ctx context.Context, url string) *req.Response

Types

type ClientOption added in v3.0.909

type ClientOption func(*clientOptions)

func WithCommonErrorResult added in v3.0.911

func WithCommonErrorResult(result interface{}) ClientOption

func WithDev added in v3.0.910

func WithDev() ClientOption

func WithInsecureSkipVerify added in v3.0.909

func WithInsecureSkipVerify(insecureSkipVerify bool) ClientOption

func WithLogger added in v3.0.909

func WithLogger(logger req.Logger) ClientOption

WithLogger

@param logger (1) 可以是 *zap.SugaredLogger 实例 || *logrus.Logger 实例

(2) 可以为nil(disable log, 禁用输出)

func WithOnAfterResponse added in v3.0.911

func WithOnAfterResponse(middleware req.ResponseMiddleware) ClientOption

func WithTimeout added in v3.0.909

func WithTimeout(timeout time.Duration) ClientOption

type LoadBalancerClient added in v3.0.915

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

LoadBalancerClient http客户端的负载均衡

func NewLbClient added in v3.0.915

func NewLbClient(baseClient *req.Client, urls []string, commonRetryInterval time.Duration, commonRetryCondition req.RetryConditionFunc) (*LoadBalancerClient, error)

NewLbClient http客户端的负载均衡.

原理: 请求失败retry时,修改请求的url.

@param baseClient (1) 可以为nil,将采用默认值

(2) !!!: 本函数会修改此传参

@param commonRetryInterval 重试周期 @param commonRetryCondition 重试条件

func (*LoadBalancerClient) Get added in v3.0.915

func (lbc *LoadBalancerClient) Get(queryParams map[string][]string) (*req.Response, error)

func (*LoadBalancerClient) Post added in v3.0.915

func (lbc *LoadBalancerClient) Post(contentType string, body interface{}) (*req.Response, error)

Jump to

Keyboard shortcuts

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