-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.go
175 lines (149 loc) · 4.4 KB
/
apps.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
package onesignal
import (
"encoding/json"
"net/url"
)
// AppsServices handles communication with onesignal api.
type AppsServices struct {
client *Client
}
// Browse function
//
// Browse apps, you can see all the apps you have
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-apps-apps
func (c *AppsServices) Browse() ([]interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "apps")
if err != nil {
return nil, err
}
c.client.UseAuthKey = true
resp, err := GET(u.String(), c.client)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data []interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return data, nil
}
// Get function
//
// View the details of a single OneSignal app. This function returns a JSON object not a struct like others
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-an-app
func (c *AppsServices) Get(id string) (interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "apps/" + id)
if err != nil {
return Success{}, err
}
c.client.UseAuthKey = true
resp, err := GET(u.String(), c.client)
if err != nil {
return Success{}, err
}
defer resp.Body.Close()
var data interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return Success{}, err
}
return data, nil
}
// Create function
//
// Create an OneSignal applications
//
// This method is used to create new apps in your OneSignal account. This function relates to https://onesignal.com/api/v1/apps
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/create-an-app
func (c *AppsServices) Create(opt *AppsOpt) (interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "apps")
if err != nil {
return Success{}, err
}
b, err := EncodeBody(opt)
if err != nil {
return Success{}, err
}
c.client.UseAuthKey = true
resp, err := POST(u.String(), b, c.client)
if err != nil {
return Success{}, err
}
return resp, nil
}
// Update function
//
// Updates the name or configuration settings of an existing OneSignal application
//
// This method can be used to update the name or configuration settings of one of your existing apps. This function relates to https://onesignal.com/api/v1/apps/:id
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/update-an-app
func (c *AppsServices) Update(id string, opt *AppsOpt) (interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "apps/" + id)
if err != nil {
return Success{}, err
}
b, err := EncodeBody(opt)
if err != nil {
return Success{}, err
}
c.client.UseAuthKey = true
resp, err := PUT(u.String(), b, c.client)
if err != nil {
return Success{}, err
}
return resp, nil
}
// BrowseDevice function
//
// View the details of multiple devices in one of your OneSignal apps
//
// This function relates to https://onesignal.com/api/v1/players?app_id=:app_id&limit=:limit&offset=:offset
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-devices
func (c *AppsServices) BrowseDevice(limit, offset string) (DeviceResponse, error) {
u, err := url.Parse(c.client.BaseURL.String() + "players?app_id=" + c.client.AppID + "&limit=" + limit + "&offset=" + offset)
if err != nil {
return DeviceResponse{}, err
}
resp, err := GET(u.String(), c.client)
if err != nil {
return DeviceResponse{}, err
}
defer resp.Body.Close()
var data DeviceResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return DeviceResponse{}, err
}
return data, nil
}
// GetDevice function
//
// View the details of an existing device in one of your OneSignal apps
//
// This function relates to https://onesignal.com/api/v1/players/:id
//
// For your reference, you can read this API reference on https://documentation.onesignal.com/reference/view-device
func (c *AppsServices) GetDevice(id string) (interface{}, error) {
u, err := url.Parse(c.client.BaseURL.String() + "players/" + id)
if err != nil {
return Success{}, err
}
resp, err := GET(u.String(), c.client)
if err != nil {
return Success{}, err
}
defer resp.Body.Close()
var data interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return Success{}, err
}
return data, nil
}