forked from tbalthazar/onesignal-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.go
347 lines (302 loc) · 9.97 KB
/
players.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package onesignal
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
// PlayersService handles communication with the player related
// methods of the OneSignal API.
type PlayersService struct {
client *Client
}
// Player represents a OneSignal player.
type Player struct {
ID string `json:"id"`
Playtime int `json:"playtime"`
SDK string `json:"sdk"`
Identifier string `json:"identifier"`
SessionCount int `json:"session_count"`
Language string `json:"language"`
Timezone int `json:"timezone"`
GameVersion string `json:"game_version"`
DeviceOS string `json:"device_os"`
DeviceType int `json:"device_type"`
DeviceModel string `json:"device_model"`
AdID string `json:"ad_id"`
Tags map[string]string `json:"tags"`
LastActive int `json:"last_active"`
AmountSpent float32 `json:"amount_spent"`
CreatedAt int `json:"created_at"`
InvalidIdentifier bool `json:"invalid_identifier"`
BadgeCount int `json:"badge_count"`
}
// PlayerRequest represents a request to create/update a player.
type PlayerRequest struct {
AppID string `json:"app_id"`
DeviceType int `json:"device_type"`
Identifier string `json:"identifier,omitempty"`
Language string `json:"language,omitempty"`
Timezone int `json:"timezone,omitempty"`
GameVersion string `json:"game_version,omitempty"`
DeviceOS string `json:"device_os,omitempty"`
DeviceModel string `json:"device_model,omitempty"`
AdID string `json:"ad_id,omitempty"`
SDK string `json:"sdk,omitempty"`
SessionCount int `json:"session_count,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
AmountSpent float32 `json:"amount_spent,omitempty"`
CreatedAt int `json:"created_at,omitempty"`
Playtime int `json:"playtime,omitempty"`
BadgeCount int `json:"badge_count,omitempty"`
LastActive int `json:"last_active,omitempty"`
TestType int `json:"test_type,omitempty"`
NotificationTypes string `json:"notification_types,omitempty"`
}
// PlayerListOptions specifies the parameters to the PlayersService.List method
type PlayerListOptions struct {
AppID string `json:"app_id"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
// PlayerListResponse wraps the standard http.Response for the
// PlayersService.List method
type PlayerListResponse struct {
TotalCount int `json:"total_count"`
Offset int `json:"offset"`
Limit int `json:"limit"`
Players []Player
}
// PlayerCreateResponse wraps the standard http.Response for the
// PlayersService.Create method
type PlayerCreateResponse struct {
Success bool `json:"success"`
ID string `json:"id"`
}
// PlayerOnSessionOptions specifies the parameters to the
// PlayersService.OnSession method
type PlayerOnSessionOptions struct {
Identifier string `json:"identifier,omitempty"`
Language string `json:"language,omitempty"`
Timezone int `json:"timezone,omitempty"`
GameVersion string `json:"game_version,omitempty"`
DeviceOS string `json:"device_os,omitempty"`
AdID string `json:"ad_id,omitempty"`
SDK string `json:"sdk,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
// Purchase represents a purchase in the options of the
// PlayersService.OnPurchase method
type Purchase struct {
SKU string `json:"sku"`
Amount float32 `json:"amount"`
ISO string `json:"iso"`
}
// PlayerOnPurchaseOptions specifies the parameters to the
// PlayersService.OnPurchase method
type PlayerOnPurchaseOptions struct {
Purchases []Purchase `json:"purchases"`
Existing bool `json:"existing,omitempty"`
}
// PlayerOnFocusOptions specifies the parameters to the
// PlayersService.OnFocus method
type PlayerOnFocusOptions struct {
State string `json:"state"`
ActiveTime int `json:"active_time"`
}
// PlayerCSVExportOptions specifies the parameters to the
// PlayersService.CSVExport method
type PlayerCSVExportOptions struct {
AppID string `json:"app_id"`
}
// PlayerCSVExportResponse wraps the standard http.Response for the
// PlayersService.CSVExport method
type PlayerCSVExportResponse struct {
CSVFileURL string `json:"csv_file_url"`
}
// List the players.
//
// OneSignal API docs: https://documentation.onesignal.com/docs/players-view-devices
func (s *PlayersService) List(opt *PlayerListOptions) (*PlayerListResponse, *http.Response, error) {
// build the URL with the query string
u, err := url.Parse("/players")
if err != nil {
return nil, nil, err
}
q := u.Query()
q.Set("app_id", opt.AppID)
q.Set("limit", strconv.Itoa(opt.Limit))
q.Set("offset", strconv.Itoa(opt.Offset))
u.RawQuery = q.Encode()
// create the request
req, err := s.client.NewRequest("GET", u.String(), nil, APP)
if err != nil {
return nil, nil, err
}
plResp := &PlayerListResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Get a single player.
//
// OneSignal API docs: https://documentation.onesignal.com/docs/playersid
func (s *PlayersService) Get(playerID string) (*Player, *http.Response, error) {
// build the URL
path := fmt.Sprintf("/players/%s", playerID)
u, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("GET", u.String(), nil, APP)
if err != nil {
return nil, nil, err
}
plResp := new(Player)
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
plResp.ID = playerID
return plResp, resp, err
}
// Create a player.
//
// OneSignal API docs:
// https://documentation.onesignal.com/docs/players-add-a-device
func (s *PlayersService) Create(player *PlayerRequest) (*PlayerCreateResponse, *http.Response, error) {
// build the URL
u, err := url.Parse("/players")
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("POST", u.String(), player, APP)
if err != nil {
return nil, nil, err
}
plResp := &PlayerCreateResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Create a new session for a player.
//
// OneSignal API docs:
// https://documentation.onesignal.com/docs/playersidon_session
func (s *PlayersService) OnSession(playerID string, opt *PlayerOnSessionOptions) (*SuccessResponse, *http.Response, error) {
// build the URL
path := fmt.Sprintf("/players/%s/on_session", playerID)
u, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("POST", u.String(), opt, APP)
if err != nil {
return nil, nil, err
}
plResp := &SuccessResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Create a new purchase for a player.
//
// OneSignal API docs: https://documentation.onesignal.com/docs/on_purchase
func (s *PlayersService) OnPurchase(playerID string, opt *PlayerOnPurchaseOptions) (*SuccessResponse, *http.Response, error) {
// build the URL
path := fmt.Sprintf("/players/%s/on_purchase", playerID)
u, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("POST", u.String(), opt, APP)
if err != nil {
return nil, nil, err
}
plResp := &SuccessResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Increment the total session length for a player.
//
// OneSignal API docs:
// https://documentation.onesignal.com/docs/playersidon_focus
func (s *PlayersService) OnFocus(playerID string, opt *PlayerOnFocusOptions) (*SuccessResponse, *http.Response, error) {
// build the URL
path := fmt.Sprintf("/players/%s/on_focus", playerID)
u, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("POST", u.String(), opt, APP)
if err != nil {
return nil, nil, err
}
plResp := &SuccessResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Generate a link to download a CSV list of all the players.
//
// OneSignal API docs:
// https://documentation.onesignal.com/docs/players_csv_export
func (s *PlayersService) CSVExport(opt *PlayerCSVExportOptions) (*PlayerCSVExportResponse, *http.Response, error) {
// build the URL with the query string
u, err := url.Parse("/players/csv_export")
if err != nil {
return nil, nil, err
}
q := u.Query()
q.Set("app_id", opt.AppID)
u.RawQuery = q.Encode()
// create the request
req, err := s.client.NewRequest("POST", u.String(), opt, APP)
if err != nil {
return nil, nil, err
}
plResp := &PlayerCSVExportResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}
// Update a player.
//
// OneSignal API docs: https://documentation.onesignal.com/docs/playersid-1
func (s *PlayersService) Update(playerID string, player *PlayerRequest) (*SuccessResponse, *http.Response, error) {
// build the URL
path := fmt.Sprintf("/players/%s", playerID)
u, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
// create the request
req, err := s.client.NewRequest("PUT", u.String(), player, APP)
if err != nil {
return nil, nil, err
}
plResp := &SuccessResponse{}
resp, err := s.client.Do(req, plResp)
if err != nil {
return nil, resp, err
}
return plResp, resp, err
}