forked from JointCloud/pcm-coordinator
update octopusHttp token
This commit is contained in:
parent
3f35a27a6c
commit
e718d0c015
|
@ -3,6 +3,7 @@ package octopusHttp
|
|||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"time"
|
||||
)
|
||||
|
@ -47,15 +48,37 @@ func NewToken(server, host, user, pwd string) (*Token, error) {
|
|||
|
||||
token, err := generateToken(jsonStr, tokenUrl, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return &Token{token: "", host: host, user: user, pwd: pwd, tokenUrl: tokenUrl}, err
|
||||
}
|
||||
return &Token{token: token, host: host, user: user, pwd: pwd, tokenUrl: tokenUrl}, nil
|
||||
}
|
||||
|
||||
func (t *Token) Get() (string, error) {
|
||||
if t.token == "" {
|
||||
err := t.Update()
|
||||
if err != nil {
|
||||
return "", errors.New("get token failed")
|
||||
}
|
||||
}
|
||||
return t.token, nil
|
||||
}
|
||||
|
||||
func (t *Token) Update() error {
|
||||
login := Login{
|
||||
Username: t.user,
|
||||
Password: t.pwd,
|
||||
}
|
||||
jsonStr, _ := json.Marshal(login)
|
||||
tokenUrl := t.server + GetToken
|
||||
token, err := generateToken(jsonStr, tokenUrl, t.host)
|
||||
if err != nil {
|
||||
return errors.New("get token failed")
|
||||
}
|
||||
|
||||
t.token = token
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateToken(jsonStr []byte, tokenUrl string, host string) (string, error) {
|
||||
client := resty.New().SetTimeout(time.Duration(5) * time.Second)
|
||||
req := client.R()
|
||||
|
|
Loading…
Reference in New Issue