https

package
v0.1.30 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 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, url string, param, header map[string]string) error

POST下载文件

file	下载文件的存储文件名【如果存在会追加写入】
url		请求地址
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 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 SwitchHttps added in v0.1.23

func SwitchHttps(bol bool)

切换Https参数验证规则

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

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提取处理结束时间
}

网址服务对象

func Get

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

GET请求

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

func Post

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

Post请求

url		请求地址
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) // 日志记录函数
	Https    bool          // 是否跳过HTTPS证书效验
	TimeOut  time.Duration // 请求超时时间
	// 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