feat: 支持自定义请求 header
This commit is contained in:
@@ -17,18 +17,29 @@ type Api struct {
|
||||
Client *http.Client
|
||||
// 禁用自动重定向
|
||||
NoDirect *http.Client
|
||||
|
||||
CustomHeader map[string]interface{}
|
||||
}
|
||||
|
||||
func (a *Api) Init(https bool, domain string, client *http.Client) {
|
||||
type ApiConfig struct {
|
||||
Https bool
|
||||
Domain string
|
||||
Client *http.Client
|
||||
CustomHeader map[string]interface{}
|
||||
}
|
||||
|
||||
func (a *Api) Init(conf *ApiConfig) {
|
||||
a.BaseUrl = "http"
|
||||
if https {
|
||||
if conf.Https {
|
||||
a.BaseUrl += "s"
|
||||
}
|
||||
a.BaseUrl = a.BaseUrl + "://" + domain + "/"
|
||||
a.BaseUrl = a.BaseUrl + "://" + conf.Domain + "/"
|
||||
|
||||
a.CustomHeader = conf.CustomHeader
|
||||
|
||||
// 初始化 http client
|
||||
a.Client = client
|
||||
copyClient := *client
|
||||
a.Client = conf.Client
|
||||
copyClient := *conf.Client
|
||||
a.NoDirect = ©Client
|
||||
a.NoDirect.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
@@ -48,6 +59,7 @@ func (a *Api) request(path string, query map[string]interface{}) (map[string]int
|
||||
req, err := httpTool.GenReq("GET", &tool.DoHttpReq{
|
||||
Url: a.BaseUrl + path,
|
||||
Query: query,
|
||||
Header: a.CustomHeader,
|
||||
})
|
||||
if err != nil {
|
||||
log.Debugln(err)
|
||||
@@ -84,7 +96,14 @@ func (a *Api) DetectAcid() (string, error) {
|
||||
addr := a.BaseUrl
|
||||
for {
|
||||
log.Debugln("HTTP GET ", addr)
|
||||
res, err := a.NoDirect.Get(addr)
|
||||
req, err := http.NewRequest("GET", addr, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for k, v := range a.CustomHeader {
|
||||
req.Header.Set(k, fmt.Sprint(v))
|
||||
}
|
||||
res, err := a.NoDirect.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -14,13 +14,19 @@ type Conf struct {
|
||||
//登录参数,不可缺省
|
||||
LoginInfo LoginInfo
|
||||
Client *http.Client
|
||||
CustomHeader map[string]interface{}
|
||||
}
|
||||
|
||||
func New(conf *Conf) *Srun {
|
||||
srun := &Srun{
|
||||
LoginInfo: conf.LoginInfo,
|
||||
}
|
||||
srun.api.Init(conf.Https, conf.LoginInfo.Form.Domain, conf.Client)
|
||||
srun.api.Init(&ApiConfig{
|
||||
Https: conf.Https,
|
||||
Domain: conf.LoginInfo.Form.Domain,
|
||||
Client: conf.Client,
|
||||
CustomHeader: conf.CustomHeader,
|
||||
})
|
||||
return srun
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user