-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpebble.go
163 lines (131 loc) · 4.08 KB
/
pebble.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
package pebble
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)
const (
userPinURL = "https://timeline-api.getpebble.com/v1/user/pins/"
sharedPinURL = "https://timeline-api.getpebble.com/v1/shared/pins/"
)
type Layout struct {
Type string `json:"type"`
Title string `json:"title"`
TinyIcon string `json:"tinyIcon"`
Body string `json:"body,omitempty"`
PrimaryColor string `json:"primaryColor,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
BackgroundColor string `json:"backgroundColor,omitempty"`
Headings []string `json:"headings,omitempty"`
Paragraphs []string `json:"paragraphs,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
}
type Notification struct {
Layout *Layout `json:"layout,omitempty"`
Time string `json:"time,omitempty"`
}
type Reminder struct {
Time string `json:"time"`
Layout *Layout `json:"layout"`
}
type Reminders []Reminder
type Action struct {
Title string `json:"title"`
Type string `json:"type"`
LaunchCode int `json:"launchCode"`
}
type Actions []Action
type Pin struct {
Id string `json:"id"`
Time string `json:"time"`
Layout *Layout `json:"layout"`
Duration int `json:"duration,omitempty"`
CreateNotification *Notification `json:"createNotification,omitempty"`
UpdateNotification *Notification `json:"updateNotification,omitempty"`
Reminders *Reminders `json:"reminders,omitempty"`
Actions *Actions `json:"actions,omitempty"`
}
type Error struct {
ErrorCode string `json:"errorCode"`
}
type UserPin struct {
Pin
Token string
}
type SharedPin struct {
Pin
APIKey string
Topics string
}
func (pin *Pin) String() string {
buf, _ := json.Marshal(pin)
return string(buf)
}
func (pin *Pin) doRequest(client *http.Client, req *http.Request) error {
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Check the response:
if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// What's the error sent from Pebble:
var e Error
err = json.NewDecoder(strings.NewReader(string(body))).Decode(&e)
if err != nil {
return err
}
err = errors.New("Error from Pebble Server: " + e.ErrorCode)
return err
}
return nil
}
func (uPin *UserPin) address() string {
return userPinURL + uPin.Id
}
func (sPin *SharedPin) address() string {
return sharedPinURL + sPin.Id
}
func (uPin *UserPin) Put(client *http.Client) error {
req, err := http.NewRequest("PUT", uPin.address(), strings.NewReader(uPin.String()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-User-Token", uPin.Token)
return uPin.doRequest(client, req)
}
func (uPin *UserPin) Delete(client *http.Client) error {
req, err := http.NewRequest("DELETE", uPin.address(), nil)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-User-Token", uPin.Token)
return uPin.doRequest(client, req)
}
func (sPin *SharedPin) Put(client *http.Client) error {
req, err := http.NewRequest("PUT", sPin.address(), strings.NewReader(sPin.String()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-Key", sPin.APIKey)
req.Header.Add("X-Pin-Topics", sPin.Topics)
return sPin.doRequest(client, req)
}
func (sPin *SharedPin) Delete(client *http.Client) error {
req, err := http.NewRequest("DELETE", sPin.address(), nil)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-Key", sPin.APIKey)
return sPin.doRequest(client, req)
}