Commit cd41446b authored by Eric's avatar Eric

wrapper http client

parent 17a481d4
package main package main
import ( import (
"cocogo/pkg/auth"
"cocogo/pkg/config" "cocogo/pkg/config"
"cocogo/pkg/sshd" "cocogo/pkg/sshd"
) )
func init() { func init() {
config.Initial() config.Initial()
auth.Initial()
sshd.Initial() sshd.Initial()
} }
......
...@@ -152,7 +152,7 @@ func (c *Client) Do(method, url string, data, res interface{}, params ...map[str ...@@ -152,7 +152,7 @@ func (c *Client) Do(method, url string, data, res interface{}, params ...map[str
return 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) return c.Do("GET", url, nil, res)
} }
......
package service package service
import ( import (
"cocogo/pkg/logger"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -9,6 +8,7 @@ import ( ...@@ -9,6 +8,7 @@ import (
"strings" "strings"
"cocogo/pkg/common" "cocogo/pkg/common"
"cocogo/pkg/logger"
) )
var ( var (
......
package service package service
import ( import (
"fmt"
"cocogo/pkg/logger" "cocogo/pkg/logger"
"cocogo/pkg/model" "cocogo/pkg/model"
"fmt"
) )
// //
...@@ -22,10 +21,10 @@ import ( ...@@ -22,10 +21,10 @@ import (
// //
func GetSystemUserAuthInfo(systemUserID string) { func GetSystemUserAuthInfo(systemUserID string) {
var authInfo model.SystemUserAuthInfo 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 { if err != nil {
logger.Info("get User Assets Groups err:", err) logger.Info("get User Assets Groups err:", err)
return return
} }
fmt.Println(authInfo)
} }
package service package service
import ( import (
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/model" "cocogo/pkg/model"
"net/http"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"cocogo/pkg/common"
"cocogo/pkg/config"
) )
type ClientAuth interface { type ClientAuth interface {
...@@ -16,18 +14,11 @@ type ClientAuth interface { ...@@ -16,18 +14,11 @@ type ClientAuth interface {
} }
type WrapperClient struct { type WrapperClient struct {
*common.Client Http *common.Client
Auth ClientAuth Auth ClientAuth
BaseHost string 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 { func (c *WrapperClient) ExpandUrl(url string, query map[string]string) string {
return "" return ""
} }
...@@ -41,7 +32,7 @@ func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string ...@@ -41,7 +32,7 @@ func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string
newUrl = strings.TrimRight(c.BaseHost, "/") + newUrl newUrl = strings.TrimRight(c.BaseHost, "/") + newUrl
} }
if len(params) == 1 { if len(params) == 1 {
url = c.ParseUrlQuery(url, params[0]) url = c.Http.ParseUrlQuery(url, params[0])
} }
return newUrl return newUrl
} }
...@@ -62,9 +53,17 @@ func (c *WrapperClient) LoadAuth() error { ...@@ -62,9 +53,17 @@ func (c *WrapperClient) LoadAuth() error {
func (c *WrapperClient) CheckAuth() error { func (c *WrapperClient) CheckAuth() error {
var user model.User var user model.User
err := c.Get("UserProfileUrl", &user) err := c.Http.Get("UserProfileUrl", &user)
if err != nil { if err != nil {
return err return err
} }
return nil 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{ ...@@ -5,7 +5,7 @@ var urls = map[string]string{
"UserProfileUrl": "/api/users/v1/profile/", // 获取当前用户的基本信息 "UserProfileUrl": "/api/users/v1/profile/", // 获取当前用户的基本信息
"SystemUserAssetAuthUrl": "/api/assets/v1/system-user/%s/asset/%s/auth-info/", // 该系统用户对某资产的授权 "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 "TerminalRegisterUrl": "/api/terminal/v2/terminal-registrations/", // 注册当前coco
"TerminalConfigUrl": "/api/terminal/v1/terminal/config/", // 从jumpserver获取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