支持静态参数设置

This commit is contained in:
白茶清欢 2025-03-31 17:55:37 +08:00
parent 5d790ff1e7
commit 1d8ee18a03
2 changed files with 43 additions and 4 deletions

View File

@ -9,6 +9,7 @@ package httpclient
import ( import (
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache" "git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define" "git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"git.zhangdeman.cn/zhangdeman/network/httpclient/validate" "git.zhangdeman.cn/zhangdeman/network/httpclient/validate"
@ -41,6 +42,44 @@ func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*Http
requestFinishHandler: make([]define.RequestFinishHandler, 0), requestFinishHandler: make([]define.RequestFinishHandler, 0),
cacheInstance: cacheInstance, cacheInstance: cacheInstance,
} }
if len(reqConfig.Static) > 0 {
for loc, valMap := range reqConfig.Static {
if len(valMap) == 0 {
continue
}
l := strings.ToUpper(loc)
switch l {
case consts.RequestDataLocationHeader.String():
if reqConfig.Header == nil {
reqConfig.Header = make(map[string]any)
}
for k, v := range valMap {
reqConfig.Header[k] = v
}
case consts.RequestDataLocationCookie.String():
if reqConfig.Cookie == nil {
reqConfig.Cookie = make(map[string]any)
}
for k, v := range valMap {
reqConfig.Cookie[k] = v
}
case consts.RequestDataLocationBody.String():
if reqConfig.Body == nil {
reqConfig.Body = make(map[string]any)
}
for k, v := range valMap {
reqConfig.Body[k] = v
}
case consts.RequestDataLocationQuery.String():
if reqConfig.Query == nil {
reqConfig.Query = make(map[string]any)
}
for k, v := range valMap {
reqConfig.Query[k] = v
}
}
}
}
return hc, nil return hc, nil
} }

View File

@ -108,10 +108,10 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
c.resp.ErrorMessage = err.Error() c.resp.ErrorMessage = err.Error()
return false return false
} }
apiCfg.RequestCfg.Body = param[consts.RequestDataLocationBody.String()] // body apiCfg.RequestCfg.Body = param[strings.ToLower(consts.RequestDataLocationBody.String())] // body
apiCfg.RequestCfg.Header = param[consts.RequestDataLocationHeader.String()] // header apiCfg.RequestCfg.Header = param[strings.ToLower(consts.RequestDataLocationHeader.String())] // header
apiCfg.RequestCfg.Cookie = param[consts.RequestDataLocationCookie.String()] // cookie apiCfg.RequestCfg.Cookie = param[strings.ToLower(consts.RequestDataLocationCookie.String())] // cookie
apiCfg.RequestCfg.Query = param[consts.RequestDataLocationQuery.String()] // query apiCfg.RequestCfg.Query = param[strings.ToLower(consts.RequestDataLocationQuery.String())] // query
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err { if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了 // 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
c.resp.ErrorCode = "-500" c.resp.ErrorCode = "-500"