工具基本实现write功能
This commit is contained in:
16
pkg/saashttp/cfg.go
Normal file
16
pkg/saashttp/cfg.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package saashttp
|
||||
|
||||
type ApiUrls struct {
|
||||
BaseUrl string
|
||||
WritePath string
|
||||
ReadPath string
|
||||
ColumnWritePath string
|
||||
TaskListPath string
|
||||
TaskCancelPath string
|
||||
TaskDetailPath string
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Account string
|
||||
Token string
|
||||
}
|
||||
140
pkg/saashttp/httpcli.go
Normal file
140
pkg/saashttp/httpcli.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package saashttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"e.coding.net/rta/public/saasapi"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type ResponseEncoder int
|
||||
|
||||
const (
|
||||
RESPONSE_ENCODER_PROTOBUF ResponseEncoder = iota
|
||||
RESPONSE_ENCODER_JSON = 1
|
||||
)
|
||||
|
||||
type SaasClient struct {
|
||||
Client http.Client
|
||||
ApiUrls ApiUrls
|
||||
Auth Auth
|
||||
ResponseEncoder ResponseEncoder
|
||||
}
|
||||
|
||||
func (c *SaasClient) Write(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.WritePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) Read(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ReadPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ColumnWrite(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ColumnWritePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) TaskList(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.TaskListPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) TaskCancel(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.TaskCancelPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) TaskDetail(saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.TaskDetailPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) makeUrl(baseUrl, path string) string {
|
||||
url, err := url.Parse(baseUrl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
url = url.JoinPath(path)
|
||||
queryValues := url.Query()
|
||||
switch c.ResponseEncoder {
|
||||
case RESPONSE_ENCODER_PROTOBUF:
|
||||
queryValues.Add("resmode", "protobuf")
|
||||
case RESPONSE_ENCODER_JSON:
|
||||
queryValues.Add("resmode", "json")
|
||||
default:
|
||||
queryValues.Add("resmode", "protobuf")
|
||||
}
|
||||
url.RawQuery = queryValues.Encode()
|
||||
|
||||
return url.String()
|
||||
}
|
||||
|
||||
func (c *SaasClient) post(url string, saasReq *saasapi.SaasReq) (succ uint32, err error) {
|
||||
postBuf, err := proto.Marshal(saasReq)
|
||||
if err != nil {
|
||||
fmt.Println("marshal saas req error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(postBuf))
|
||||
if err != nil {
|
||||
fmt.Println("http new request error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
md5byte := md5.Sum([]byte(c.Auth.Account + c.Auth.Token + timeStamp))
|
||||
authorization := hex.EncodeToString(md5byte[:])
|
||||
|
||||
req.Header.Add("Content-Type", "application/protobuf")
|
||||
req.Header.Add("Account", c.Auth.Account)
|
||||
req.Header.Add("Time", timeStamp)
|
||||
req.Header.Add("Authorization", authorization)
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("http send error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("http read body error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
saasRes := &saasapi.SaasRes{}
|
||||
if c.ResponseEncoder == RESPONSE_ENCODER_PROTOBUF {
|
||||
err = proto.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to protobuf error", err)
|
||||
return 0, err
|
||||
}
|
||||
} else {
|
||||
err = json.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to json error", err)
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if saasRes.Code != saasapi.ErrorCode(saasapi.CmdErrorCode_OK) {
|
||||
fmt.Println("saas api error", saasRes.Code, saasRes.Status)
|
||||
return 0, err
|
||||
}
|
||||
return saasRes.GetSuccCmdCount(), nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user