-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushNotifier.h
286 lines (229 loc) · 6.71 KB
/
PushNotifier.h
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
#ifndef H_PUSHNOTIFIER
#define H_PUSHNOTIFIER
#include <string>
#include <vector>
#include <ctime>
#include <curl/curl.h>
#include "json/src/json.hpp"
// Uncomment and change these lines to your API-Token and Package
// #define API_TOKEN "replaceme"
// #define APP_PACKAGE "replaceme"
// URLs
#define URL_PN_LOGIN "https://api.pushnotifier.de/v2/user/login"
#define URL_PN_DEVICES "https://api.pushnotifier.de/v2/devices"
#define URL_PN_REFRESH "https://api.pushnotifier.de/v2/user/refresh"
#define URL_PN_TXT "https://api.pushnotifier.de/v2/notifications/text"
#define URL_PN_URL "https://api.pushnotifier.de/v2/notifications/url"
#define URL_PN_NOTIFICATION "https://api.pushnotifier.de/v2/notifications/notification"
/**
* @brief PushNotifier SDK Service
*
* A bridge for using PushNotifier
*
* @author hackherz
* @version 1.0 <January 2018>
*/
class PushNotifier
{
public:
/**
* @brief Initialize the PushNotifier instance
*/
PushNotifier();
/**
* @brief Initialize the PushNotifier instance
*
* @param username Username of the user
* @param appToken App-Token to user
* @param expires_at Timestamp of the expire date of the token. Set to NULL if you want to disable automatical renewal
*/
PushNotifier(std::string username, std::string appToken, std::time_t expires_at);
/**
* @brief Take care of CURL
*/
~PushNotifier();
/**
* @brief A PushNotifier device
*/
typedef struct {
/// Title of the Device
std::string title;
/// ID of the Device
std::string id;
/// Model of the Device
std::string model;
/// URL to Device-image
std::string image;
/// String representation of the device (without image)
std::string toString() {
return "Title: " + title + " ID: " + id + " Model: " + model;
}
} Device;
/**
* @brief Representation of an AppToken
*/
typedef struct {
/// The token itself
std::string token;
/// Timestamp of the expiration date
std::time_t expires_at;
/// Checks if the token expires within the next 48 hours
bool isAboutToExpire() {
return std::difftime(expires_at, std::time(nullptr)) < 48.0 * 3600;
}
} AppToken;
/**
* @brief Log in as a user
*
* @param username Username to login with
* @param password Passwordof the user
* @param apply Automatically apply the AppToken or just return it
*
* @return AppToken
*/
AppToken login(std::string username, std::string password, bool apply = true);
/**
* @brief Refresh the current AppToken
*
* This can be done any time but shouldn't be done until 48 hours before it expires
*
* @param apply Automatically apply the AppToken to this instance
*
* @return AppToken
*
* @throws runtime_error If an unknown error happened
*/
AppToken refreshToken(bool apply = true);
/**
* @brief Get all devices the current user has
*
* @return List of Device
*
* @throws runtime_error If an unknown error happened
*/
std::vector<Device> getDevices();
/**
* @brief Set the AppToken that should be used
*
* @param appToken AppToken
* @param expires_at Expiration Date of the AppToken, set null to disable auto-renewal
*
* @return PushNotifier
*/
void setApptoken(std::string appToken, std::time_t expires_at);
/**
* @brief Get the current AppToken
*
* Renews itself automatically in case it is about to expire
*
* @return string representation of the token
*/
std::string getAppToken();
/**
* @brief Send a text-only message
*
* @param deviceIDs List of Device-IDs to send to
* @param content Message to send
* @param silent If true, no sound is played
*
* @return Whether the message has been successfully delivered to all devices or not
*
* @throws runtime_error If content is empty
*/
bool sendMessage(std::vector<std::string> deviceIDs, std::string content, bool silent = false);
/**
* @brief Send a message
*
* @param deviceID Device-ID to send to
* @param content Message to send
* @param silent If true, no sound is played
*
* @return Whether the message has been successfully delivered to all devices or not
*
* @throws runtime_error If content is empty
*/
bool sendMessage(std::string deviceID, std::string content, bool silent = false);
/**
* @brief Send a link
*
* @param deviceIDs List of Device-IDs to send to
* @param url URL to send
* @param silent If true, no sound is played
*
* @return Whether the url has been successfully delivered to all devices or not
*
* @throws runtime_error If url is empty
*/
bool sendURL(std::vector<std::string> deviceIDs, std::string url, bool silent = false);
/**
* @brief Send a link
*
* @param deviceID Device-ID to send to
* @param url URL to send
* @param silent If true, no sound is played
*
* @return Whether the url has been successfully delivered to all devices or not
*
* @throws runtime_error If url is empty
*/
bool sendURL(std::string deviceID, std::string url, bool silent = false);
/**
* @brief Send a notification with content and a URL
*
* The user will be taken to the URL after tapping on the notification
*
* @param deviceIDs List of Device-IDs to send to
* @param content Content to send
* @param url URL to send
* @param silent If true, no sound is played
*
* @return Whether the notification has been successfully delivered to all devices or not
*
* @throws runtime_error If url or content are empty
*/
bool sendNotification(std::vector<std::string> deviceIDs, std::string content, std::string url, bool silent = false);
/**
* @brief Send a notification with content and a URL
*
* The user will be taken to the URL after tapping on the notification
*
* @param deviceID Device-ID to send to
* @param content Content to send
* @param url URL to send
* @param silent If true, no sound is played
*
* @return Whether the notification has been successfully delivered to all devices or not
*
* @throws runtime_error If url or content are empty
*/
bool sendNotification(std::string deviceID, std::string content, std::string url, bool silent = false);
private:
/**
* Enum Class to specify the Http method for call
*/
typedef enum {
PUT,
GET,
POST
} http_method;
/**
* @brief CURL wrapper function
*
* @param method Http Method to use
* @param url The Url to invoke
* @param xapptoken X-AppToken for Api Authentication
* @param body JSON-Request data
*
* @return JSON-Object of the response
*
* @throws runtime_error If the Api response indicates an error or CURL failed
*/
nlohmann::json call(http_method method, std::string url, const char* xapptoken, nlohmann::json body);
/// Currently used Username
std::string username;
/// Currently used AppToken
AppToken appToken;
/// Authorization Header Value
std::string authorization;
};
#endif