Commit 077c1b7f authored by ibuler's avatar ibuler

[Update] 修改service

parent 46d2537d
...@@ -76,13 +76,7 @@ func (c *Client) ParseUrlQuery(url string, query map[string]string) string { ...@@ -76,13 +76,7 @@ func (c *Client) ParseUrlQuery(url string, query map[string]string) string {
return url return url
} }
func (c *Client) ParseUrl(url string, params ...map[string]string) string { func (c *Client) ParseUrl(url string) string {
if len(params) == 1 {
url = c.ParseUrlQuery(url, params[0])
}
for _, parser := range c.UrlParsers {
url = parser.parse(url, params...)
}
return url return url
} }
...@@ -117,14 +111,14 @@ func (c *Client) SetReqHeaders(req *http.Request, params ...map[string]string) { ...@@ -117,14 +111,14 @@ func (c *Client) SetReqHeaders(req *http.Request, params ...map[string]string) {
c.SetAuthHeader(req) c.SetAuthHeader(req)
} }
func (c *Client) NewRequest(method, url string, body interface{}, params ...map[string]string) (req *http.Request, err error) { func (c *Client) NewRequest(method, url string, body interface{}) (req *http.Request, err error) {
url = c.ParseUrl(url, params...) url = c.ParseUrl(url)
reader, err := c.marshalData(body) reader, err := c.marshalData(body)
if err != nil { if err != nil {
return return
} }
req, err = http.NewRequest(method, url, reader) req, err = http.NewRequest(method, url, reader)
c.SetReqHeaders(req, params...) c.SetReqHeaders(req)
return req, err return req, err
} }
...@@ -132,7 +126,7 @@ func (c *Client) NewRequest(method, url string, body interface{}, params ...map[ ...@@ -132,7 +126,7 @@ func (c *Client) NewRequest(method, url string, body interface{}, params ...map[
// params: // params:
// 1. query string if set {"name": "ibuler"} // 1. query string if set {"name": "ibuler"}
func (c *Client) Do(method, url string, data, res interface{}, params ...map[string]string) (err error) { func (c *Client) Do(method, url string, data, res interface{}, params ...map[string]string) (err error) {
req, err := c.NewRequest(method, url, data, params...) req, err := c.NewRequest(method, url, data)
resp, err := c.http.Do(req) resp, err := c.http.Do(req)
if err != nil { if err != nil {
return return
...@@ -158,8 +152,8 @@ func (c *Client) Do(method, url string, data, res interface{}, params ...map[str ...@@ -158,8 +152,8 @@ func (c *Client) Do(method, url string, data, res interface{}, params ...map[str
return return
} }
func (c *Client) Get(url string, res interface{}, params ...map[string]string) (err error) { func (c *Client) Get(url string, query map[string]string, res interface{}) (err error) {
return c.Do("GET", url, nil, res, params...) return c.Do("GET", url, nil, res)
} }
func (c *Client) Post(url string, data interface{}, res interface{}) (err error) { func (c *Client) Post(url string, data interface{}, res interface{}) (err error) {
......
package service package service
import (
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
)
// //
//func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (authInfo model.SystemUserAuthInfo, err error) { //func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (authInfo model.SystemUserAuthInfo, err error) {
// //
...@@ -13,21 +20,12 @@ package service ...@@ -13,21 +20,12 @@ package service
// //
//} //}
// //
//func (s *Service) GetSystemUserAuthInfo(systemUserID string) { func GetSystemUserAuthInfo(systemUserID string) {
// var authInfo model.SystemUserAuthInfo
// url := fmt.Sprintf("%s%s", s.Conf.CoreHost, err := client.Get("systemUserAuthInfo", nil, &authInfo)
// fmt.Sprintf(SystemUserAuthUrl, systemUserID)) if err != nil {
// buf, err := s.SendHTTPRequest("GET", url, nil) logger.Info("get User Assets Groups err:", err)
// if err != nil { return
// log.Info("get User Assets Groups err:", err) }
// return fmt.Println(authInfo)
// } }
// //err = json.Unmarshal(buf, &authInfo)
// fmt.Printf("%s", buf)
// if err != nil {
// log.Info(err)
// return
// }
// return
//
//}
...@@ -29,7 +29,7 @@ func (c *WrapperClient) SetAuthHeader(r *http.Request) { ...@@ -29,7 +29,7 @@ func (c *WrapperClient) SetAuthHeader(r *http.Request) {
} }
func (c *WrapperClient) ExpandUrl(url string, query map[string]string) string { func (c *WrapperClient) ExpandUrl(url string, query map[string]string) string {
return ""
} }
func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string { func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string {
...@@ -43,10 +43,6 @@ func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string ...@@ -43,10 +43,6 @@ func (c *WrapperClient) ParseUrl(url string, params ...map[string]string) string
if len(params) == 1 { if len(params) == 1 {
url = c.ParseUrlQuery(url, params[0]) url = c.ParseUrlQuery(url, params[0])
} }
if len(params) == 2 {
url =
}
return newUrl return newUrl
} }
......
package service
//func (s *Service) EnsureValidAuth() {
// for i := 0; i < 10; i++ {
// if !s.validateAuth() {
// msg := `Connect server error or access key is invalid,
// remove "./data/keys/.access_key" run again`
// logger.Error(msg)
// time.Sleep(time.Second * 3)
//
// } else {
// break
// }
// if i == 3 {
// os.Exit(1)
// }
// }
//}
//
//func (s *Service) validateAuth() bool {
//
// url := fmt.Sprintf("%s%s", s.Conf.CoreHost, UserProfileUrl)
// body, err := s.SendHTTPRequest(http.MethodGet, url, nil)
// if err != nil {
// log.Info("Read response Body err:", err)
// return false
// }
// result := model.User{}
// err = json.Unmarshal(body, &result)
// if err != nil {
// log.Info("json.Unmarshal", err)
// return false
// }
// log.Info(result)
// return result != model.User{}
//}
//func (s *Service) registerTerminalAndSave() error {
//
// postMap := map[string]string{
// "name": s.Conf.Name,
// "comment": s.Conf.Comment,
// }
// data, err := json.Marshal(postMap)
// if err != nil {
// log.Info("json encode failed:", err)
// return err
//
// }
// url := fmt.Sprintf("%s%s", s.Conf.CoreHost, TerminalRegisterUrl)
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
// if err != nil {
// log.Info("http NewRequest err:", err)
// return err
// }
// req.Header.Set("Content-Type", "application/json")
// req.Header.Set("Authorization", fmt.Sprintf("BootstrapToken %s", s.Conf.BootstrapToken))
// resp, err := s.http.Do(req)
// if err != nil {
// log.Info("http request err:", err)
// return err
//
// }
// defer resp.Body.Close()
// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Info("read resp body err:", err)
// return err
// }
// /*
// {
// "name": "sss2",
// "comment": "Coco",
// "service_account": {
// "id": "c2dece80-1811-42bc-bd5b-aef0f4180263",
// "name": "sss2",
// "access_key": {
// "id": "f9b2cf91-7f30-45ea-9edf-b73ec0f48d5a",
// "secret": "fd083b6c-e823-47bf-870c-0dd6051e69f1"
// }
// }
// }
// */
// log.Infof("%s", body)
//
// var resBody struct {
// ServiceAccount struct {
// Id string `json:"id"`
// Name string `json:"name"`
// Accesskey struct {
// Id string `json:"id"`
// Secret string `json:"secret"`
// } `json:"access_key"`
// } `json:"service_account"`
// }
//
// err = json.Unmarshal(body, &resBody)
// if err != nil {
// log.Info("json Unmarshal:", err)
// return err
// }
// if resBody.ServiceAccount.Name == "" {
// return errors.New(string(body))
// }
//
// s.auth = accessAuth{
// accessKey: resBody.ServiceAccount.Accesskey.Id,
// accessSecret: resBody.ServiceAccount.Accesskey.Secret,
// }
// return s.saveAccessKey()
//}
//
//func (s *Service) saveAccessKey() error {
// MakeSureDirExit(s.Conf.AccessKeyFile)
// f, err := os.Create(s.Conf.AccessKeyFile)
// fmt.Println("Create file path:", s.Conf.AccessKeyFile)
// if err != nil {
// return err
// }
// keyAndSecret := fmt.Sprintf("%s:%s", s.auth.accessKey, s.auth.accessSecret)
// _, err = f.WriteString(keyAndSecret)
// if err != nil {
// return err
// }
// err = f.Close()
// if err != nil {
// return err
// }
// return nil
//}
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