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