-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
182 lines (158 loc) · 4.57 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package goao
import (
"bytes"
"encoding/json"
"fmt"
"github.com/permadao/goao/schema"
"github.com/permadao/goar"
goarSchema "github.com/permadao/goar/schema"
"gopkg.in/h2non/gentleman.v2"
"net/http"
"strconv"
"time"
)
type Client struct {
muCli *gentleman.Client
cuCli *gentleman.Client
bundler *goar.Bundler
}
func NewClient(muURL, cuURL string, signer interface{}) (*Client, error) {
muCli := gentleman.New().URL(muURL)
cuCli := gentleman.New().URL(cuURL)
bundler, err := goar.NewBundler(signer)
if err != nil {
return nil, err
}
return &Client{
muCli: muCli,
cuCli: cuCli,
bundler: bundler,
}, nil
}
func (c *Client) Send(processId, data, msgType string, tags []goarSchema.Tag) (res schema.ResponseMu, err error) {
tags = append(tags, goarSchema.Tag{Name: "Data-Protocol", Value: schema.DataProtocol})
tags = append(tags, goarSchema.Tag{Name: "Variant", Value: schema.Variant})
tags = append(tags, goarSchema.Tag{Name: "Type", Value: msgType})
tags = append(tags, goarSchema.Tag{Name: "SDK", Value: schema.SDK})
item, err := c.bundler.CreateAndSignItem([]byte(data), processId, "", tags)
if err != nil {
return
}
req := c.muCli.Post()
req.SetHeader("content-type", "application/octet-stream")
req.SetHeader("accept", "application/json")
req.Body(bytes.NewBuffer(item.Binary))
resp, err := req.Send()
if err != nil {
return
}
if !resp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
err = json.Unmarshal(resp.Bytes(), &res)
return
}
func (c *Client) Eval(processId, code string) (res schema.ResponseMu, err error) {
return c.Send(
processId, code, schema.TypeMessage,
[]goarSchema.Tag{
goarSchema.Tag{Name: "Action", Value: "Eval"},
},
)
}
func (c *Client) Spawn(processName, appName, module, scheduler string) (res schema.ResponseMu, err error) {
return c.Send(
"", strconv.Itoa(int(time.Now().UnixNano())), schema.TypeProcess,
[]goarSchema.Tag{
{Name: "Name", Value: processName},
{Name: "App-Name", Value: appName},
{Name: "Module", Value: module},
{Name: "Scheduler", Value: scheduler},
},
)
}
func (c *Client) Result(processId, messageId string) (res schema.ResponseCu, err error) {
req := c.cuCli.Get()
req.AddPath(fmt.Sprintf("/result/%v", messageId))
req.AddQuery("process-id", processId)
resp, err := req.Send()
if err != nil {
return
}
if !resp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer resp.Close()
respBody := resp.Bytes()
// golang http not handle Temporary Redirect(307)
if resp.StatusCode == http.StatusTemporaryRedirect {
var redirectResp *gentleman.Response
redirectResp, err = c.cuCli.Request().URL(resp.Header.Get("Location")).Send()
if err != nil {
return
}
if !redirectResp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer redirectResp.Close()
respBody = redirectResp.Bytes()
}
err = json.Unmarshal(respBody, &res)
return
}
func (c *Client) DryRun(processId, data string, tags []goarSchema.Tag) (res schema.ResponseCu, err error) {
tags = append(tags, goarSchema.Tag{Name: "Data-Protocol", Value: schema.DataProtocol})
tags = append(tags, goarSchema.Tag{Name: "Variant", Value: schema.Variant})
tags = append(tags, goarSchema.Tag{Name: "Type", Value: schema.TypeMessage})
tags = append(tags, goarSchema.Tag{Name: "SDK", Value: schema.SDK})
item := struct {
Id string `json:"Id"`
Target string `json:"Target"`
Owner string `json:"Owner"`
Data string `json:"Data"`
Tags []goarSchema.Tag `json:"Tags"`
Anchor string `json:"Anchor"`
}{
Id: "0000000000000000000000000000000000000000001",
Target: processId,
Owner: "0000000000000000000000000000000000000000001",
Data: data,
Tags: tags,
}
payload, err := json.Marshal(item)
if err != nil {
return
}
req := c.cuCli.Post()
req.AddPath("/dry-run")
req.AddQuery("process-id", processId)
req.JSON(payload)
resp, err := req.Send()
if err != nil {
return
}
if !resp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer resp.Close()
respBody := resp.Bytes()
if resp.StatusCode == http.StatusTemporaryRedirect {
var redirectResp *gentleman.Response
redirectResp, err = gentleman.New().URL(resp.Header.Get("Location")).Post().JSON(payload).Send()
if err != nil {
return
}
if !redirectResp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer redirectResp.Close()
respBody = redirectResp.Bytes()
}
err = json.Unmarshal(respBody, &res)
return
}