forked from tbalthazar/onesignal-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onesignal.go
172 lines (147 loc) · 4.09 KB
/
onesignal.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
// Package onesignal provides the binding for OneSignal API.
package onesignal
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strings"
)
const (
defaultBaseURL = "https://onesignal.com/api/v1/"
)
// AuthKeyType specifies the token used to authentify the requests
type AuthKeyType uint
const (
APP AuthKeyType = iota
USER
)
// A Client manages communication with the OneSignal API.
type Client struct {
BaseURL *url.URL
AppKey string
UserKey string
Client *http.Client
Apps *AppsService
Players *PlayersService
Notifications *NotificationsService
}
// SuccessResponse wraps the standard http.Response for several API methods
// that just return a Success flag.
type SuccessResponse struct {
Success bool `json:"success"`
}
// ErrorResponse reports one or more errors caused by an API request.
type ErrorResponse struct {
Messages []string `json:"errors"`
}
func (e *ErrorResponse) Error() string {
msg := "OneSignal returned those error messages:\n - "
return msg + strings.Join(e.Messages, "\n - ")
}
// NewClient returns a new OneSignal API client.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseURL, err := url.Parse(defaultBaseURL)
if err != nil {
log.Fatal(err)
}
c := &Client{
BaseURL: baseURL,
Client: httpClient,
}
c.Apps = &AppsService{client: c}
c.Players = &PlayersService{client: c}
c.Notifications = &NotificationsService{client: c}
return c
}
// NewRequest creates an API request. path is a relative URL, like "/apps". The
// value pointed to by body is JSON encoded and included as the request body.
// The AuthKeyType will determine which authorization token (APP or USER) is
// used for the request.
func (c *Client) NewRequest(method, path string, body interface{}, authKeyType AuthKeyType) (*http.Request, error) {
// build the URL
u, err := url.Parse(c.BaseURL.String() + path)
if err != nil {
return nil, err
}
// JSON encode the body
var buf io.ReadWriter
if body != nil {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(body)
if err != nil {
return nil, err
}
buf = b
// log.Println("Body is: " + b.String())
}
// create the request
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
// headers
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if authKeyType == APP {
req.Header.Add("Authorization", "Basic "+c.AppKey)
// log.Println("Authorization header is AppKey")
} else {
req.Header.Add("Authorization", "Basic "+c.UserKey)
// log.Println("Authorization header is UserKey")
}
return req, nil
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred.
func (c *Client) Do(r *http.Request, v interface{}) (*http.Response, error) {
// send the request
resp, err := c.Client.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = CheckResponse(resp)
if err != nil {
return resp, err
}
// // log body for debug
// b := new(bytes.Buffer)
// b.ReadFrom(resp.Body)
// log.Println("response body: ", b.String())
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&v)
if err != nil {
return resp, err
}
return resp, nil
}
// CheckResponse checks the API response for errors, and returns them if
// present. A response is considered an error if it has a status code outside
// the 200 range. API error responses are expected to have either no response
// body, or a JSON response body that maps to ErrorResponse. Any other
// response body will be silently ignored.
func CheckResponse(r *http.Response) error {
switch r.StatusCode {
case http.StatusOK:
return nil
case http.StatusInternalServerError:
return &ErrorResponse{
Messages: []string{"Internal Server Error"},
}
default:
var errResp ErrorResponse
dec := json.NewDecoder(r.Body)
err := dec.Decode(&errResp)
if err != nil {
errResp.Messages = []string{"Couldn't decode response body JSON"}
}
return &errResp
}
}