httpclient

package
v1.5.11 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultRetryTimes 如果请求失败,最多重试3次
	DefaultRetryTimes = 3
	// DefaultRetryDelay 在重试前,延迟等待100毫秒
	DefaultRetryDelay = time.Millisecond * 100
)
View Source
const (
	// DefaultTTL 一次http请求最长执行1分钟
	DefaultTTL = time.Minute
)

Variables

This section is empty.

Functions

func BehaviorIsAccess added in v1.2.5

func BehaviorIsAccess(koalaUrl string) (isAccess bool, err error)

func Delete

func Delete(url string, form httpURL.Values, options ...Option) (body []byte, err error)

Delete delete 请求

func Get

func Get(url string, form httpURL.Values, options ...Option) (body []byte, err error)

Get get 请求

func GetHttpClient added in v1.3.7

func GetHttpClient(url string) (*http.Client, error)

func PatchFrom

func PatchFrom(url string, form httpURL.Values, options ...Option) (body []byte, err error)

PatchFrom patch form 请求

func PatchJSON

func PatchJSON(url string, raw json.RawMessage, options ...Option) (body []byte, err error)

PatchJSON patch json 请求

func PostForm

func PostForm(url string, form httpURL.Values, options ...Option) (body []byte, err error)

PostForm post form 请求

func PostJSON

func PostJSON(url string, raw json.RawMessage, options ...Option) (body []byte, err error)

PostJSON post json 请求

func PublicRequire

func PublicRequire(url, method string, ReqByte []byte, header *http.Header, hc gin.HandlersChain, c *gin.Context) (resBody []byte, resCode int, err error)

反向代理

func PublicRequireByGet

func PublicRequireByGet(url string, header http.Header) (resBody []byte, resCode int, err error)

反向代理 GET

func PublicRequireByGetWithoutLog added in v1.4.5

func PublicRequireByGetWithoutLog(url string, header http.Header, reqByte []byte) (resBody []byte, resCode int, err error)

反向代理 GET, 不产生反向代理日志,url为网关api中登记的接口才可调用

func PublicRequireByPUT

func PublicRequireByPUT(url string, header http.Header, reqByte []byte) (resBody []byte, resCode int, err error)

反向代理 PUT

func PublicRequireByPUTWithoutLog added in v1.4.16

func PublicRequireByPUTWithoutLog(url string, header http.Header, reqByte []byte) (resBody []byte, resCode int, err error)

反向代理 PUT,不产生反向代理日志,url为网关api中登记的接口才可调用

func PublicRequireByPost

func PublicRequireByPost(url string, header http.Header, reqByte []byte) (resBody []byte, resCode int, err error)

反向代理 POST

func PublicRequireByPostForm

func PublicRequireByPostForm(url string, header http.Header, urlV url.Values) (resBody []byte, resCode int, err error)

反向代理 POST 传入内容为表单

func PublicRequireByPostFormWithoutLog added in v1.4.16

func PublicRequireByPostFormWithoutLog(url string, header http.Header, urlV url.Values) (resBody []byte, resCode int, err error)

反向代理 POST 传入内容为表单,不产生反向代理日志,url为网关api中登记的接口才可调用

func PublicRequireByPostWithoutLog added in v1.4.5

func PublicRequireByPostWithoutLog(url string, header http.Header, reqByte []byte) (resBody []byte, resCode int, err error)

反向代理 POST, 不产生反向代理日志,url为网关api中登记的接口才可调用

func PutForm

func PutForm(url string, form httpURL.Values, options ...Option) (body []byte, err error)

PutForm put form 请求

func PutJSON

func PutJSON(url string, raw json.RawMessage, options ...Option) (body []byte, err error)

PutJSON put json 请求

func RetProxyLog added in v1.4.5

func RetProxyLog(header http.Header, resCode int, timeNow time.Time, method string, url string, resBody, ReqByte []byte, err error)

生成代理日志

Types

type AlarmObject

type AlarmObject interface {
	Send(subject, body string) error
}

type AlarmVerify

type AlarmVerify func(body []byte) (shouldAlarm bool)

AlarmVerify Verify parse the body and verify that it is correct

type Mock

type Mock func() (body []byte)

Mock 定义接口Mock数据

type Option

type Option func(*option)

Option 自定义设置http请求

func WithHeader

func WithHeader(key, value string) Option

WithHeader 设置http header,可以调用多次设置多对key-value

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger 设置logger以便打印关键日志

func WithMock

func WithMock(m Mock) Option

WithMock 设置 mock 数据

func WithOnFailedAlarm

func WithOnFailedAlarm(alarmTitle string, alarmObject AlarmObject, alarmVerify AlarmVerify) Option

WithOnFailedAlarm 设置告警通知

func WithOnFailedRetry

func WithOnFailedRetry(retryTimes int, retryDelay time.Duration, retryVerify RetryVerify) Option

WithOnFailedRetry 设置失败重试

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL 本次http请求最长执行时间

func WithTrace

func WithTrace(t trace.T) Option

WithTrace 设置trace信息

type PublicProxyLog added in v1.2.2

type PublicProxyLog struct {
	Status  int         `gorm:"column:status;type:int(3);not null;comment:'http状态码'" json:"status"`     // http状态码
	Method  string      `gorm:"column:method;type:varchar(32);not null;comment:'http方法'" json:"method"` // http方法
	URL     string      `gorm:"column:url;type:varchar(255);not null;comment:'路径'" json:"url"`          // 路径
	Header  http.Header `gorm:"column:header;type:varchar(255);not null;comment:'请求头'" json:"header"`   // 请求头
	Body    string      `gorm:"column:body;type:text;default:null;comment:'请求body信息'" json:"body"`      // 请求body信息
	ResInfo string      `gorm:"column:res_info;type:text;not null;comment:'返回信息'" json:"res_info"`      // 返回信息
	ResErr  string      `gorm:"column:res_err;type:text;default:null" json:"res_err"`
	Latency int         `gorm:"column:latency;type:int(32);not null;comment:'延迟(单位:毫秒)'" json:"latency"` // 延迟(单位:毫秒)
}

PublicProxyLog [...]

func NewProxyLog added in v1.2.2

func NewProxyLog(status, latency int, method, url string, header http.Header, resInfo, body, resErr string) PublicProxyLog

type ReplyErr

type ReplyErr interface {
	error
	StatusCode() int
	Body() []byte
}

ReplyErr 错误响应,当 resp.StatusCode != http.StatusOK 时用来包装返回的 httpcode 和 body 。

func ToReplyErr

func ToReplyErr(err error) (ReplyErr, bool)

ToReplyErr 尝试将 err 转换为 ReplyErr

type RetryVerify

type RetryVerify func(body []byte) (shouldRetry bool)

RetryVerify Verify parse the body and verify that it is correct

Jump to

Keyboard shortcuts

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