Commit cd41446b authored by Eric's avatar Eric

wrapper http client

parent 17a481d4
package main
import (
"cocogo/pkg/auth"
"cocogo/pkg/config"
"cocogo/pkg/sshd"
)
func init() {
config.Initial()
auth.Initial()
sshd.Initial()
}
......
......@@ -152,7 +152,7 @@ func (c *Client) Do(method, url string, data, res interface{}, params ...map[str
return
}
func (c *Client) Get(url string, query map[string]string, res interface{}) (err error) {
func (c *Client) Get(url string, res interface{}) (err error) {
return c.Do("GET", url, nil, res)
}
......
package service
import (
"cocogo/pkg/logger"
"errors"
"fmt"
"io/ioutil"
......@@ -9,6 +8,7 @@ import (
"strings"
"cocogo/pkg/common"
"cocogo/pkg/logger"
)
var (
......
package service
import (
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"fmt"
)
//
......@@ -22,10 +21,10 @@ import (
//
func GetSystemUserAuthInfo(systemUserID string) {
var authInfo model.SystemUserAuthInfo
err := client.Get("systemUserAuthInfo", nil, &authInfo)
systemUrl := fmt.Sprintf(urls["SystemUserAuthInfo"], systemUserID)
err := client.Get(systemUrl, &authInfo, true)
if err != nil {
logger.Info("get User Assets Groups err:", err)
return
}
fmt.Println(authInfo)
}
package service
import (
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/model"
"net/http"
"path"
"path/filepath"
"strings"
"cocogo/pkg/common"
"cocogo/pkg/config"
)
type ClientAuth interface {
......@@ -16,18 +14,11 @@ type ClientAuth interface {
}
type WrapperClient struct {
*common.Client
Http *common.Client
Auth ClientAuth
BaseHost string
}
func (c *WrapperClient) SetAuthHeader(r *http.Request) {
if c.Auth != nil {
signature := c.Auth.Sign()
r.Header.Add("Authorization", signature)
}
}
func (c *WrapperClient) ExpandUrl(url string, query map[string]string) string {
return ""
}
......@@ -41,7 +32,7 @@ func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string
newUrl = strings.TrimRight(c.BaseHost, "/") + newUrl
}
if len(params) == 1 {
url = c.ParseUrlQuery(url, params[0])
url = c.Http.ParseUrlQuery(url, params[0])
}
return newUrl
}
......@@ -62,9 +53,17 @@ func (c *WrapperClient) LoadAuth() error {
func (c *WrapperClient) CheckAuth() error {
var user model.User
err := c.Get("UserProfileUrl", &user)
err := c.Http.Get("UserProfileUrl", &user)
if err != nil {
return err
}
return nil
}
func (c *WrapperClient) Get(url string, res interface{}, needAuth bool) error {
if needAuth {
c.Http.SetAuth(c.Auth.Sign())
}
return c.Http.Get(c.BaseHost+url, res)
}
......@@ -5,7 +5,7 @@ var urls = map[string]string{
"UserProfileUrl": "/api/users/v1/profile/", // 获取当前用户的基本信息
"SystemUserAssetAuthUrl": "/api/assets/v1/system-user/%s/asset/%s/auth-info/", // 该系统用户对某资产的授权
"SystemUserAuthUrl": "/api/assets/v1/system-user/%s/auth-info/", // 该系统用户的授权
"SystemUserAuthInfo": "/api/assets/v1/system-user/%s/auth-info/", // 该系统用户的授权
"TerminalRegisterUrl": "/api/terminal/v2/terminal-registrations/", // 注册当前coco
"TerminalConfigUrl": "/api/terminal/v1/terminal/config/", // 从jumpserver获取coco的配置
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment