reqKit

package
v3.0.923 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 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

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

func GetGlobalClient

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

func ReplaceGlobalClient(client *req.Client)

func SimpleGet

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

Types

type ClientOption

type ClientOption func(*clientOptions)

func WithCommonErrorResult

func WithCommonErrorResult(result interface{}) ClientOption

func WithDev

func WithDev() ClientOption

func WithInsecureSkipVerify

func WithInsecureSkipVerify(insecureSkipVerify bool) ClientOption

func WithLogger

func WithLogger(logger req.Logger) ClientOption

WithLogger

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

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

func WithOnAfterResponse

func WithOnAfterResponse(middleware req.ResponseMiddleware) ClientOption

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

type LoadBalancerClient

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

LoadBalancerClient http客户端的负载均衡

func NewLbClient

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

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

func (*LoadBalancerClient) Post

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