Documentation ¶
Index ¶
- Constants
- Variables
- func Get(ctx context.Context, url string, queryParams map[string][]string) *req.Response
- func GetAndInto(ctx context.Context, url string, queryParams map[string][]string, ...) error
- func GetGlobalClient() (client *req.Client)
- func NewClient(options ...ClientOption) (client *req.Client)
- func Post(ctx context.Context, url string, contentType string, body interface{}) *req.Response
- func ReplaceGlobalClient(client *req.Client)
- func SimpleGet(ctx context.Context, url string) *req.Response
- type ClientOption
- func WithCommonErrorResult(result interface{}) ClientOption
- func WithDev() ClientOption
- func WithInsecureSkipVerify(insecureSkipVerify bool) ClientOption
- func WithLogger(logger req.Logger) ClientOption
- func WithOnAfterResponse(middleware req.ResponseMiddleware) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- type LoadBalancerClient
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 ¶
Functions ¶
func GetAndInto ¶
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 ¶
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)
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)
Click to show internal directories.
Click to hide internal directories.