-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
188 lines (165 loc) · 4.47 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
183
184
185
186
187
188
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package types
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
// Client defines golang client to call notify.APIServer
type Client interface {
// Creates a new Client using current settings and different context. The
// context is used with http.NewRequestWithContext to create *http.Request.
With(ctx context.Context) (ret Client)
// maps api endpoints to function
Send(id string, driver string, ep string, payload interface{}) (err error)
SendOnce(id string, driver string, ep string, payload interface{}) (err error)
Resend(id string) (err error)
Result(id string) (ret []byte, err error)
Status(id string) (ret Status, err error)
Detail(id string) (ret Detail, err error)
Delete(id string) (err error)
Clear(before time.Time) (err error)
ForceClear(before time.Time) (err error)
}
// NewClient creates a Client
//
// server is address of the server in "http(s)://example.com:1234" format (no path)
func NewClient(server string, hc *http.Client) (ret Client) {
if hc == nil {
hc = &http.Client{}
}
return &client{host: server, hc: hc, ctx: context.Background()}
}
type client struct {
host string
hc *http.Client
ctx context.Context
}
func (c *client) With(ctx context.Context) (ret Client) {
return &client{
host: c.host,
hc: c.hc,
ctx: ctx,
}
}
func (c *client) exec(path string, data interface{}) (err error) {
buf, err := json.Marshal(data)
if err != nil {
return
}
req, err := http.NewRequestWithContext(
c.ctx, "POST", c.host+path, bytes.NewReader(buf),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.hc.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
defer io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = fmt.Errorf("failed to call %s: %d", path, resp.StatusCode)
}
return
}
func (c *client) query(path string, data, ret interface{}) (err error) {
buf, err := json.Marshal(data)
if err != nil {
return
}
req, err := http.NewRequestWithContext(
c.ctx, "POST", c.host+path, bytes.NewReader(buf),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.hc.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
defer io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = fmt.Errorf("failed to call %s: %d", path, resp.StatusCode)
return
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(ret)
return
}
func (c *client) Send(id string, driver string, ep string, payload interface{}) (err error) {
data := map[string]interface{}{
"id": id,
"type": driver,
"endpoint": ep,
"payload": payload,
}
return c.exec("/send", data)
}
func (c *client) SendOnce(id string, driver string, ep string, payload interface{}) (err error) {
data := map[string]interface{}{
"id": id,
"type": driver,
"endpoint": ep,
"payload": payload,
}
return c.exec("/sendOnce", data)
}
func (c *client) Resend(id string) (err error) {
data := map[string]interface{}{"id": id}
return c.exec("/resend", data)
}
func (c *client) Result(id string) (ret []byte, err error) {
data := map[string]interface{}{"id": id}
buf, err := json.Marshal(data)
if err != nil {
return
}
req, err := http.NewRequestWithContext(
c.ctx, "POST", c.host+"/result", bytes.NewReader(buf),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.hc.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
ret, err = ioutil.ReadAll(resp.Body)
return
}
func (c *client) Status(id string) (ret Status, err error) {
data := map[string]interface{}{"id": id}
err = c.query("/status", data, &ret)
return
}
func (c *client) Detail(id string) (ret Detail, err error) {
data := map[string]interface{}{"id": id}
err = c.query("/detail", data, &ret)
return
}
func (c *client) Delete(id string) (err error) {
data := map[string]interface{}{"id": id}
return c.exec("/delete", data)
}
func (c *client) Clear(before time.Time) (err error) {
data := map[string]interface{}{"before": before.Unix()}
return c.exec("/clear", data)
}
func (c *client) ForceClear(before time.Time) (err error) {
data := map[string]interface{}{"before": before.Unix()}
return c.exec("/forceClear", data)
}