https

package
v0.2.30 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: BSD-3-Clause-Clear Imports: 16 Imported by: 0

Documentation

Overview

网络请求组件

此组件依赖model.debugs进行日志记录,请注意协调

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientClose added in v0.1.24

func ClientClose()

重置_default配置项下的client客户端配置

func DownloadGet added in v0.1.6

func DownloadGet(file, url string, param, header map[string]string) error

GET下载文件

file	下载文件的存储文件名【如果存在会追加写入】
url		请求地址
param	请求参数
header	请求头,自动追加accept、Content-Type等属性

func DownloadPost added in v0.1.6

func DownloadPost(file, uri string, param, header map[string]string) error

POST下载文件

file	下载文件的存储文件名【如果存在会追加写入】
uri		请求地址
param	请求参数
header	请求头,自动追加accept、Content-Type等属性

func DownloadPostJson added in v0.1.6

func DownloadPostJson(file, url string, param map[string]any, header map[string]string) error

POSTJSON下载文件

file	下载文件的存储文件名【如果存在会追加写入】
url		请求地址
param	请求参数【如果不想封装外层map的话,可直接传入map[string]any{"_":xxx}(会在map最外层有且仅有_下标时才会直接取其内容进行传输)】
header	请求头,自动追加accept、Content-Type等属性

func Gatway

func Gatway(h http.Handler, intef GatwayInterface) http.Handler

网址转发操作【此操作暂不支持通配网址信息】 比如,在gin框架中使用时,用以下方式进行嵌套:

wraphh类的来源:import wraphh "github.com/turtlemonvh/gin-wraphh"
r.GET("xxxx", wraphh.WrapHH(func(h http.Handler) http.Handler {
    // 假设声明实现该接口的类型为XXX
    target_urls := XXX{}
    return Gatway(h, target_urls)
}))

h	HTTP请求中间件会传入的参数
intef	HTTP转发过程中的数据交互钩子

func Regedit added in v0.0.19

func Regedit(c *Config)

注入配置项

func SwitchAddNoBody added in v0.2.8

func SwitchAddNoBody(h string)

添加不需要展示Body的URL网址

h	移除body的URL网址

func SwitchAddNoHeader added in v0.2.8

func SwitchAddNoHeader(h string)

添加不需要展示的Header头

h	要去除的header中的key标示

func SwitchClient added in v0.1.24

func SwitchClient(client *http.Client)

配置自定义的Http.Client配置项【一旦传入此配置项,将不再使用组件内的配置项,所以一旦传入此项,则超时、跳过证书等选项将失效】 另,此配置项具有继承性,所以在使用完以后请再次使用此项进行重置 defer https.SwitchClient(nil) 或者 defer https.ClientClose() 或若需要继续使用,则直接将其放在https.Regedit()注入配置附近

client	自定义的Client配置

func SwitchGetNoBody added in v0.2.8

func SwitchGetNoBody() []string

获取不显示Body的配置列表

func SwitchGetNoHeader added in v0.2.8

func SwitchGetNoHeader() []string

获取不显示Header的配置列表

func SwitchHttps added in v0.1.23

func SwitchHttps(bol bool)

切换Https参数验证规则

bol	是否跳过HTTPS证书效验 true跳过 false继续验证证书

func SwitchRemoveNoBody added in v0.2.8

func SwitchRemoveNoBody(h string)

移除不需要展示Body的URL网址

h	要显示的body的URL网址信息

func SwitchRemoveNoHeader added in v0.2.8

func SwitchRemoveNoHeader(h string)

移除不需要展示的Header头

h	要显示的header中的key标示

func SwitchTimeout added in v0.1.24

func SwitchTimeout(t time.Duration)

设置HTTP请求超时时间

t	设置超时时间,默认0,无超时设置

Types

type CURL

type CURL struct {
	Uri       string            `json:"uri"`        // 请求网址
	Param     map[string]string `json:"param"`      // 请求参数
	HttpCode  int               `json:"http_code"`  // HTTP返回的code值
	ParamJson map[string]any    `json:"param_json"` // JSON推送参数
	Body      string            `json:"body"`       // 返回值
	Error     error             `json:"error"`      // 错误信息
	Header    map[string]string `json:"header"`     // 请求Header头
	StartTime time.Time         `json:"start_time"` // 请求开始时间
	EndTime   time.Time         `json:"end_time"`   // 请求结束时间
	OverTime  time.Time         `json:"over_time"`  // Json提取处理结束时间
	ClientIp  string            `json:"client_ip"`  // 请求的客户端IP【兼容日志处理模块的预留字段】
}

网址服务对象

func Get

func Get(uri string, param, header map[string]string) *CURL

GET请求

uri		请求地址
param	请求参数
header	请求头,自动追加accept、Content-Type等属性

func Post

func Post(uri string, param, header map[string]string) *CURL

Post请求

uri		请求地址
param	请求参数
header	请求头,自动追加accept、Content-Type等属性

func PostJson

func PostJson(url string, param map[string]any, header map[string]string) *CURL

POST-JSON请求

url		请求地址
param	请求参数【如果不想封装外层map的话,可直接传入map[string]any{"_":xxx}(会在map最外层有且仅有_下标时才会直接取其内容进行传输)】
header	请求头,自动追加accept、Content-Type等属性

func (*CURL) Json

func (curl *CURL) Json(obj any, first ...string) error

获取JSON中的数据

obj			要获取的obj对象,需要提供给json.Unmarshal
first...	simplejson.GetPath传入的string列表

func (*CURL) JsonData

func (curl *CURL) JsonData(obj any) error

获取JSON中的Data对象

obj	要转化的对象,需要在json中的data里面

type Config added in v0.0.19

type Config struct {
	LogsFunc func(c *CURL) // 日志记录函数【其param和param_json可能为截取后的结果,但返回值body为完整返回值】
	Https    bool          // 是否跳过HTTPS证书效验
	TimeOut  time.Duration // 请求超时时间

	MaxParamLength int      // 最大参数长度【超过该长度的字符串将会进行截取`MaxParamLength - 3 + "..."`】(直接传入PostJson(_:xxx)的方法无法进行截取)
	NoBodyRouter   []string // 不记录Body返回值的网址请求,不支持正则表达式,支持开头匹配
	NoHeaderKeys   []string // 不需要记录的Header信息[不区分大小写]
	// contains filtered or unexported fields
}

配置请求信息

type GatwayInterface

type GatwayInterface interface {
	// 超时时间
	Timeout() time.Duration

	// 长连接超时时间
	KeepAlive() time.Duration

	// TLS握手超时时间
	TLSHandshakeTimeout() time.Duration

	// 负载均衡的URL列表[此处采用随机的方式进行请求访问]
	Urls() []string

	// 请求的网址信息[可做额外处理,如追加header参数等](此处不建议重写URL)
	// 追加Header方法:request.Header.Set("","")
	QuestUrl(request *http.Request)

	// 响应处理[可做额外处理,如重写返回信息等](socket长连接不支持此方法)
	// 重写方法:将重写结果作为[]byte进行返回(若此值为nul则表示不进行重写)
	ResponseUrl(response *http.Response) ([]byte, error)

	// 错误处理方法
	// 错误回调 :关闭real_server时测试,错误回调
	// 范围:transport.RoundTrip发生的错误、以及ModifyResponse发生的错误
	Error(w http.ResponseWriter, r *http.Request, err error)
}

传入的接口信息,用于处理响应的回调操作

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳