支持静态参数设置

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 (
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"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),
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
}