-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
68 lines (60 loc) · 1.62 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package core
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/bububa/oppo-omni/core/internal/debug"
"github.com/bububa/oppo-omni/model"
"github.com/bububa/oppo-omni/util"
)
type SDKClient struct {
AppID string
AppKey string
debug bool
}
func NewSDKClient(appID string, appKey string) *SDKClient {
return &SDKClient{
AppID: appID,
AppKey: appKey,
}
}
func (c *SDKClient) SetDebug(debug bool) {
c.debug = debug
}
func (c *SDKClient) token(ownerID uint64, ts int64) string {
rawToken := fmt.Sprintf("%d,%s,%d,%s", ownerID, c.AppID, ts, c.sign(ts))
return base64.StdEncoding.EncodeToString([]byte(rawToken))
}
func (c *SDKClient) sign(ts int64) string {
return util.Sha1(fmt.Sprintf("%s%s%d", c.AppID, c.AppKey, ts))
}
func (c *SDKClient) Post(req model.Request, resp model.Response) error {
reqBytes, _ := json.Marshal(req)
reqURL := fmt.Sprintf("%s/%s/%s/%s", req.GetBaseUrl(), req.GetVersion(), req.ResourceName(), req.ResourceAction())
ts := time.Now().Unix()
token := c.token(req.GetOwnerID(), ts)
debug.PrintPostJSONRequest(reqURL, reqBytes, c.debug)
httpReq, err := http.NewRequest("POST", reqURL, bytes.NewReader(reqBytes))
if err != nil {
return err
}
httpReq.Header.Add("Content-Type", req.GetContentType())
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return err
}
defer httpResp.Body.Close()
err = debug.DecodeJSONHttpResponse(httpResp.Body, resp, c.debug)
if err != nil {
debug.PrintError(err, c.debug)
return err
}
if resp.IsError() {
return resp
}
return nil
}