-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_push.go
192 lines (168 loc) · 4.82 KB
/
bt_push.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
package btpush
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"path"
"time"
)
const (
baseURL = "https://brushedtype-push.herokuapp.com/v0.1"
)
// Client this is a client to work with the BT Push API
type Client struct {
BaseURL string
HTTPClient *http.Client
Config Config
Debug bool
}
// Config Represents a Client configuration
type Config struct {
Token string
}
const (
TimeSensitiveInturruptionLevel = "time-sensitive"
ActiveInterruptionLevel = "active"
PassiveInterruptionLevel = "passive"
)
// Content represents the data in a notification request
type Content struct {
Data interface{} `json:"data,omitempty"`
ContentAvailable int `json:"content_available,omitempty"`
InterruptionLevel string `json:"interruption-level,omitempty"`
Badge int `json:"badge,omitempty"`
Category string `json:"category,omitempty"`
MutableContent int `json:"mutable_content,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
Sound interface{} `json:"sound,omitempty"`
Expiration time.Time `json:"expiration,omitempty"`
// Alert
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
TitleLocArgs []string `json:"title_loc_args,omitempty"`
TitleLocKey string `json:"title_loc_key,omitempty"`
LocArgs []string `json:"loc_args,omitempty"`
LocKey string `json:"loc_key,omitempty"`
}
// Response a response from the server
type Response struct {
Status bool `json:"status"`
Message string `json:"message"`
Error *Error `json:"error"`
}
// ClientError represents an error in the client or response
type ClientError struct {
message string
Type ErrorType
}
func (r ClientError) Error() string {
return r.message
}
// SendAlertNotificationsUser send alert notifications to a user
func (c *Client) SendAlertNotificationsUser(userID string, content Content) (Response, *ClientError) {
return c.POST("/alert", map[string]interface{}{
"user": userID,
"content": content,
})
}
// SendAlertNotificationsDevices send alert notifications to specific devices
func (c *Client) SendAlertNotificationsDevices(userID string, devices []string, content Content) (Response, *ClientError) {
return c.POST("/alert", map[string]interface{}{
"user": userID,
"devices": devices,
"content": content,
})
}
// SendSilentNotificationsUser send silent notifications to a user
func (c *Client) SendSilentNotificationsUser(userID string, content Content) (Response, *ClientError) {
return c.POST("/silent", map[string]interface{}{
"user": userID,
"content": content,
})
}
// SendSilentNotificationsDevices send silent notifications to specific devices
func (c *Client) SendSilentNotificationsDevices(userID string, devices []string, content Content) (Response, *ClientError) {
return c.POST("/silent", map[string]interface{}{
"user": userID,
"devices": devices,
"content": content,
})
}
// POST Make a POST request
func (c *Client) POST(route string, payload interface{}) (Response, *ClientError) {
var r Response
var url *url.URL
var err error
url, _ = url.Parse(c.BaseURL)
url.Path = path.Join(url.Path, route)
// JSON encoding
var jsonBytes []byte
jsonBytes, err = json.Marshal(payload)
if err != nil {
return r, &ClientError{
Type: ErrorTypeOther,
message: err.Error(),
}
}
if c.Debug {
log.Printf("POST '%s' with %d-byte payload: %+v\n", url.String(), len(jsonBytes), string(jsonBytes))
}
// Request creation
req, err := http.NewRequest("POST", url.String(), bytes.NewBuffer(jsonBytes))
if err != nil {
return r, &ClientError{
Type: ErrorTypeOther,
message: err.Error(),
}
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Config.Token))
// Response
resp, err := c.HTTPClient.Do(req)
if err != nil {
return r, &ClientError{
Type: ErrorTypeOther,
message: err.Error(),
}
}
defer resp.Body.Close()
if c.Debug {
log.Printf("HTTP %s\n", resp.Status)
}
// JSON decoding
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return r, &ClientError{
Type: ErrorTypeOther,
message: err.Error(),
}
} else if r.Error != nil {
return r, &ClientError{
Type: r.Error.Type,
message: r.Error.Message,
}
}
return r, nil
}
// New create an API client with usual defaults
func New(config Config) *Client {
return &Client{
BaseURL: baseURL,
HTTPClient: http.DefaultClient,
Config: config,
Debug: false,
}
}
// NewDebug create an API client with usual defaults and debugging turned on
func NewDebug(config Config) *Client {
return &Client{
BaseURL: baseURL,
HTTPClient: http.DefaultClient,
Config: config,
Debug: true,
}
}