-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrands.go
190 lines (172 loc) · 4.74 KB
/
errands.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
183
184
185
186
187
188
189
190
package errands
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
schemas "github.com/polygon-io/errands-server/schemas"
)
type ErrandsAPI struct {
EndpointURL string
Processors []*Processor
}
func New(url string) *ErrandsAPI {
obj := &ErrandsAPI{}
obj.EndpointURL = url
return obj
}
//easyjson:json
type ErrandsResponse struct {
Results []schemas.Errand `json:"results"`
Status string `json:"status"`
}
//easyjson:json
type ErrandResponse struct {
Results schemas.Errand `json:"results"`
Status string `json:"status"`
}
func (e *ErrandsAPI) GetErrands() (*ErrandsResponse, error) {
res, err := e.get("/v1/errands/")
if err != nil {
return &ErrandsResponse{}, err
}
return parseErrandsResponse(res)
}
// ListErrands queries the errands API for a list of errands that match the given query.
// Possible options for key are: status and type.
func (e *ErrandsAPI) ListErrands(key, val string) (*ErrandsResponse, error) {
path := fmt.Sprintf("/v1/errands/list/%s/%s", key, val)
res, err := e.get(path)
if err != nil {
return &ErrandsResponse{}, err
}
return parseErrandsResponse(res)
}
func (e *ErrandsAPI) CreateErrand(errand *schemas.Errand) (*ErrandResponse, error) {
errandBytes, err := errand.MarshalJSON()
if err != nil {
return &ErrandResponse{}, err
}
resp, err := http.Post(e.EndpointURL+"/v1/errands/", "application/json", bytes.NewBuffer(errandBytes))
if err != nil {
return &ErrandResponse{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrandResponse{}, err
}
return parseErrandResponse(body)
}
func (e *ErrandsAPI) RequestErrandToProcess(topic string) (*ErrandResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", e.EndpointURL+"/v1/errands/process/"+topic, nil)
if err != nil {
return &ErrandResponse{}, err
}
resp, err := client.Do(req)
if err != nil {
return &ErrandResponse{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrandResponse{}, err
}
return parseErrandResponse(body)
}
//easyjson:json
type FailErrandReq struct {
Reason string `json:"reason"`
}
func (e *ErrandsAPI) FailErrand(errandId, reason string) (*ErrandResponse, error) {
failReq := &FailErrandReq{reason}
failReqBytes, err := failReq.MarshalJSON()
if err != nil {
return &ErrandResponse{}, err
}
client := &http.Client{}
req, err := http.NewRequest("PUT", e.EndpointURL+"/v1/errand/"+errandId+"/failed", bytes.NewBuffer(failReqBytes))
if err != nil {
return &ErrandResponse{}, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return &ErrandResponse{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrandResponse{}, err
}
return parseErrandResponse(body)
}
//easyjson:json
type CompleteErrandReq struct {
Results map[string]interface{} `json:"results"`
}
func (e *ErrandsAPI) CompleteErrand(errandId string, results map[string]interface{}) (*ErrandResponse, error) {
compReq := &CompleteErrandReq{results}
compReqBytes, err := compReq.MarshalJSON()
if err != nil {
return &ErrandResponse{}, err
}
client := &http.Client{}
req, err := http.NewRequest("PUT", e.EndpointURL+"/v1/errand/"+errandId+"/completed", bytes.NewBuffer(compReqBytes))
if err != nil {
return &ErrandResponse{}, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return &ErrandResponse{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrandResponse{}, err
}
return parseErrandResponse(body)
}
func (e *ErrandsAPI) DeleteErrand(errandId string) (*ErrandResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("DELETE", e.EndpointURL+"/v1/errand/"+errandId, nil)
if err != nil {
return &ErrandResponse{}, err
}
resp, err := client.Do(req)
if err != nil {
return &ErrandResponse{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrandResponse{}, err
}
return parseErrandResponse(body)
}
func parseErrandResponse(res []byte) (*ErrandResponse, error) {
errandRes := &ErrandResponse{}
if err := errandRes.UnmarshalJSON(res); err != nil {
return &ErrandResponse{}, err
}
return errandRes, nil
}
func parseErrandsResponse(res []byte) (*ErrandsResponse, error) {
errandRes := &ErrandsResponse{}
if err := errandRes.UnmarshalJSON(res); err != nil {
return &ErrandsResponse{}, err
}
return errandRes, nil
}
func (e *ErrandsAPI) get(url string) ([]byte, error) {
resp, err := http.Get(e.EndpointURL + url)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}