improve: 优化 pkg/srun api 抽象层级
This commit is contained in:
@@ -13,8 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Api struct {
|
type Api struct {
|
||||||
inited bool
|
|
||||||
|
|
||||||
BaseUrl string
|
BaseUrl string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
// 禁用自动重定向
|
// 禁用自动重定向
|
||||||
@@ -22,10 +20,6 @@ type Api struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Api) Init(https bool, domain string, client *http.Client) {
|
func (a *Api) Init(https bool, domain string, client *http.Client) {
|
||||||
if a.inited {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
a.BaseUrl = "http"
|
a.BaseUrl = "http"
|
||||||
if https {
|
if https {
|
||||||
a.BaseUrl += "s"
|
a.BaseUrl += "s"
|
||||||
@@ -38,8 +32,6 @@ func (a *Api) Init(https bool, domain string, client *http.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
|
||||||
}
|
}
|
||||||
|
|
||||||
a.inited = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Api) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
|
func (a *Api) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package srun
|
package srun
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoginForm struct {
|
type LoginForm struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
UserName string `json:"username"`
|
UserName string `json:"username"`
|
||||||
@@ -23,17 +19,3 @@ type LoginInfo struct {
|
|||||||
Form LoginForm
|
Form LoginForm
|
||||||
Meta LoginMeta
|
Meta LoginMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conf struct {
|
|
||||||
//调用 API 时直接访问 https URL
|
|
||||||
Https bool
|
|
||||||
//登录参数,不可缺省
|
|
||||||
LoginInfo LoginInfo
|
|
||||||
Client *http.Client
|
|
||||||
|
|
||||||
api Api
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Conf) initApi() {
|
|
||||||
a.api.Init(a.Https, a.LoginInfo.Form.Domain, a.Client)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,12 +4,33 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoginStatus(c *Conf) (online bool, ip string, e error) {
|
type Conf struct {
|
||||||
c.initApi()
|
//调用 API 时直接访问 https URL
|
||||||
|
Https bool
|
||||||
|
//登录参数,不可缺省
|
||||||
|
LoginInfo LoginInfo
|
||||||
|
Client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(conf *Conf) *Srun {
|
||||||
|
srun := &Srun{
|
||||||
|
LoginInfo: conf.LoginInfo,
|
||||||
|
}
|
||||||
|
srun.api.Init(conf.Https, conf.LoginInfo.Form.Domain, conf.Client)
|
||||||
|
return srun
|
||||||
|
}
|
||||||
|
|
||||||
|
type Srun struct {
|
||||||
|
//登录参数,不可缺省
|
||||||
|
LoginInfo LoginInfo
|
||||||
|
api Api
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Srun) LoginStatus() (online bool, ip string, e error) {
|
||||||
res, e := c.api.GetUserInfo()
|
res, e := c.api.GetUserInfo()
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return false, "", e
|
return false, "", e
|
||||||
@@ -38,7 +59,7 @@ func LoginStatus(c *Conf) (online bool, ip string, e error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoLogin(clientIP string, c *Conf) error {
|
func (c *Srun) DoLogin(clientIP string) error {
|
||||||
log.Debugln("正在获取 Token")
|
log.Debugln("正在获取 Token")
|
||||||
|
|
||||||
if c.LoginInfo.Form.UserType != "" {
|
if c.LoginInfo.Form.UserType != "" {
|
||||||
Reference in New Issue
Block a user