-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
269 lines (257 loc) · 7.01 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package push
import (
"context"
"encoding/json"
"github.com/rabee-inc/go-pkg/jsonrpc2"
"github.com/rabee-inc/go-pkg/log"
)
// Client ... プッシュ通知のクライアント
type Client struct {
appID string
endpoint string
headers map[string]string
}
// Entry ... 登録する
func (c *Client) Entry(ctx context.Context, userID string, pf Platform, deviceID string, token string) error {
params := &struct {
AppID string `json:"app_id"`
UserID string `json:"user_id"`
Platform Platform `json:"platform"`
DeviceID string `json:"device_id"`
Token string `json:"token"`
}{
AppID: c.appID,
UserID: userID,
Platform: pf,
DeviceID: deviceID,
Token: token,
}
client := jsonrpc2.NewClient(c.endpoint, c.headers)
_, resError, err := client.DoSingle(ctx, "entry", params)
if err != nil {
log.Error(ctx, err)
return err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return err
}
return nil
}
// Leave ... 解除する
func (c *Client) Leave(ctx context.Context, userID string, pf Platform, deviceID string) error {
params := &struct {
AppID string `json:"app_id"`
UserID string `json:"user_id"`
Platform Platform `json:"platform"`
DeviceID string `json:"device_id"`
}{
AppID: c.appID,
UserID: userID,
Platform: pf,
DeviceID: deviceID,
}
client := jsonrpc2.NewClient(c.endpoint, c.headers)
_, resError, err := client.DoSingle(ctx, "leave", params)
if err != nil {
log.Error(ctx, err)
return err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return err
}
return nil
}
// SendByUsers ... 指定したユーザーに送信する
func (c *Client) SendByUsers(ctx context.Context, userIDs []string, pushID string, msg *Message) error {
params := &struct {
AppID string `json:"app_id"`
UserIDs []string `json:"user_ids"`
PushID string `json:"push_id"`
Message *Message `json:"message"`
}{
AppID: c.appID,
UserIDs: userIDs,
PushID: pushID,
Message: msg,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
_, resError, err := cli.DoSingle(ctx, "send_by_users", params)
if err != nil {
log.Error(ctx, err)
return err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return err
}
return nil
}
// SendByAllUsers ... 全員に送信する
func (c *Client) SendByAllUsers(ctx context.Context, pushID string, msg *Message) error {
params := &struct {
AppID string `json:"app_id"`
PushID string `json:"push_id"`
Message *Message `json:"message"`
}{
AppID: c.appID,
PushID: pushID,
Message: msg,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
_, resError, err := cli.DoSingle(ctx, "send_by_all_users", params)
if err != nil {
log.Error(ctx, err)
return err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return err
}
return nil
}
// GetReserve ... 予約を取得する
func (c *Client) GetReserve(ctx context.Context, reserveID string) (*Reserve, error) {
params := &struct {
AppID string `json:"app_id"`
ReserveID string `json:"reserve_id"`
}{
AppID: c.appID,
ReserveID: reserveID,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
resResult, resError, err := cli.DoSingle(ctx, "get_reserve", params)
if err != nil {
log.Error(ctx, err)
return nil, err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return nil, err
}
ret := &struct {
Reserve *Reserve `json:"reserve"`
}{}
err = json.Unmarshal([]byte(*resResult), ret)
if err != nil {
log.Error(ctx, err)
return nil, err
}
return ret.Reserve, nil
}
// ListReserve ... 予約リストを取得する
func (c *Client) ListReserve(ctx context.Context, limit int, cursor string) ([]*Reserve, string, error) {
params := &struct {
AppID string `json:"app_id"`
Limit int `json:"limit"`
Cursor string `json:"cursor"`
}{
AppID: c.appID,
Limit: limit,
Cursor: cursor,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
resResult, resError, err := cli.DoSingle(ctx, "list_reserve", params)
if err != nil {
log.Error(ctx, err)
return nil, "", err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return nil, "", err
}
ret := &struct {
Reserves []*Reserve `json:"reserves"`
NextCursor string `json:"next_cursor"`
}{}
err = json.Unmarshal([]byte(*resResult), ret)
if err != nil {
log.Error(ctx, err)
return nil, "", err
}
return ret.Reserves, ret.NextCursor, nil
}
// CreateReserve ... 予約を作成する
func (c *Client) CreateReserve(ctx context.Context, userIDs []string, msg *Message, reservedAt int64, unmanaged bool) (*Reserve, error) {
params := &struct {
AppID string `json:"app_id"`
UserIDs []string `json:"user_ids"`
Message *Message `json:"message"`
ReservedAt int64 `json:"reserved_at"`
Unmanaged bool `json:"unmanaged"`
}{
AppID: c.appID,
UserIDs: userIDs,
Message: msg,
ReservedAt: reservedAt,
Unmanaged: unmanaged,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
resResult, resError, err := cli.DoSingle(ctx, "create_reserve", params)
if err != nil {
log.Error(ctx, err)
return nil, err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return nil, err
}
ret := &struct {
Reserve *Reserve `json:"reserve"`
}{}
err = json.Unmarshal([]byte(*resResult), ret)
if err != nil {
log.Error(ctx, err)
return nil, err
}
return ret.Reserve, nil
}
// UpdateReserve ... 予約を更新する
func (c *Client) UpdateReserve(ctx context.Context, reserveID string, userIDs []string, msg *Message, reservedAt int64, status ReserveStatus) (*Reserve, error) {
params := &struct {
AppID string `json:"app_id"`
ReserveID string `json:"reserve_id"`
UserIDs []string `json:"user_ids"`
Message *Message `json:"message"`
ReservedAt int64 `json:"reserved_at"`
Status ReserveStatus `json:"status"`
}{
AppID: c.appID,
ReserveID: reserveID,
UserIDs: userIDs,
Message: msg,
ReservedAt: reservedAt,
Status: status,
}
cli := jsonrpc2.NewClient(c.endpoint, c.headers)
resResult, resError, err := cli.DoSingle(ctx, "update_reserve", params)
if err != nil {
log.Error(ctx, err)
return nil, err
}
if resError != nil {
err = log.Errore(ctx, "code: %d, message: %s", resError.Code, resError.Message)
return nil, err
}
ret := &struct {
Reserve *Reserve `json:"reserve"`
}{}
err = json.Unmarshal([]byte(*resResult), ret)
if err != nil {
log.Error(ctx, err)
return nil, err
}
return ret.Reserve, nil
}
// NewClient ... クライアントを作成する
func NewClient(appID string, endpoint string, authToken string) *Client {
headers := map[string]string{
"Authorization": authToken,
}
return &Client{
appID: appID,
endpoint: endpoint,
headers: headers,
}
}