审查视图

pkg/pushdata/api.go 2.7 KB
1 2
package pushdata
3 4 5 6 7 8 9 10
import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"github.com/dgrijalva/jwt-go"
tangxvhui authored
11
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
12 13
)
14 15 16 17 18
type FieldName struct {
	Name string `json:"name"`
}

type RespData struct {
19 20
	Code int    `json:"code"`
	Msg  string `json:"msg"`
21 22 23 24 25 26
}

func (resp *RespData) IsOk() bool {
	return resp.Code == 0
}
27 28 29 30
type Client struct {
	Host      string
	AppSecret string // 调试用 3Oo4dG64X0
	AppKey    string // 调试用 GnAmG4jybB
31 32
}
33 34
func NewClient() *Client {
	return &Client{
tangxvhui authored
35 36 37
		Host:      constant.PUSH_DATA_HOST,
		AppSecret: constant.PUSH_DATA_APPSECRET,
		AppKey:    constant.PUSH_DATA_APPKEY,
38
	}
39 40
}
41 42 43 44 45 46 47 48 49 50
func (c *Client) useHeader() (h http.Header) {
	token := jwt.New(jwt.SigningMethodHS256)
	tk, err := token.SignedString([]byte(c.AppSecret))
	if err != nil {
		fmt.Printf("err:%s \n", err)
	}
	h = http.Header{}
	h.Add("x-mmm-accesstoken", tk)
	h.Add("x-mmm-appkey", c.AppKey)
	return
51 52
}
53 54 55 56
type ReqAppendData struct {
	Name   string              `json:"name"`   // 应用表名称
	Fields []FieldName         `json:"fields"` // 应用字段
	Data   []map[string]string `json:"data"`   // 数据
57 58
}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
// AppendData 追加应用表数据
func (c *Client) AppendData(reqData ReqAppendData) error {
	apiUrl := c.Host + `/api/app-table-file/append-data`
	bodyData, err := json.Marshal(reqData)
	if err != nil {
		return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
	}
	req, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewReader(bodyData))
	if err != nil {
		return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
	}
	req.Header = c.useHeader()
	req.Header.Add("Content-Type", "application/json")
	httpClient := http.DefaultClient
	httpClient.Timeout = 60 * time.Second
	resp, err := httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
	}
	defer resp.Body.Close()
	var respData RespData
	j := json.NewDecoder(resp.Body)
	err = j.Decode(&respData)
	if err != nil {
		return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
	}
	if !respData.IsOk() {
		return fmt.Errorf("推送数据到字库,追加应用表数据响应:%s", respData.Msg)
	}
	return nil
89 90
}
91 92
type ReqSearchTable struct {
	Name string `json:"name"`
93 94
}
95 96 97
// SearchTable 查询应用数据表
func (c *Client) SearchTable(name string) {
	apiUrl := `/api/app-table-file/list`
tangxvhui authored
98
	//TODO
99
	_ = apiUrl
100 101
}
102 103 104 105
type ReqCreateTable struct {
	Name   string              `json:"name"`   // 应用表名称
	Fields []FieldName         `json:"fields"` // 应用字段
	Data   []map[string]string `json:"data"`
106 107
}
108 109 110
// CreateTable 创建应用表
func (c *Client) CreateTable() {
	apiUrl := `/api/app-table-file/create`
tangxvhui authored
111
	//TODO
112
	_ = apiUrl
113
}