-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.gs
96 lines (93 loc) · 2.98 KB
/
api.gs
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
function doGet(request) {
return withAuthentication(request, () => {
const store = request.parameter["store"];
const id = request.parameter["id"];
if (store == null && id != null)
return makeResponse({
status: "fail",
data: {
parameters:
'URL parameter "store" cannot be missing if "id" is present',
},
});
let data = null;
if (store == null) data = uDB.getStores();
else if (id == null) data = new uDB(store).getAll();
else data = new uDB(store).get(id);
return makeResponse({
status: "success",
data: data,
});
});
}
function doPost(request) {
return withAuthentication(request, () => {
const store = request.parameter["store"];
if (store == null)
return makeResponse({
status: "fail",
data: { store: 'Missing URL parameter "store"' },
});
const id = request.parameter["id"];
if (
id != null &&
request.postData != null &&
request.postData.contents.trim().length > 0
)
return makeResponse({
status: "fail",
data: {
id: 'URL parameter "id" may not be given with document in body',
},
});
let data = null;
if (
id == null &&
(request.postData == null ||
request.postData.contents.trim().length == 0)
)
data = new uDB(store).clear();
else if (id != null) data = new uDB(store).delete(id);
else {
let doc = null;
try {
doc = JSON.parse(request.postData.contents);
} catch {
return makeResponse({
status: "fail",
data: { postData: "Request body is not valid JSON" },
});
}
data = new uDB(store).put(doc);
}
return makeResponse({
status: "success",
data: data,
});
});
}
function makeResponse(response) {
return ContentService.createTextOutput(
JSON.stringify(response)
).setMimeType(ContentService.MimeType.JSON);
}
function withAuthentication(request, func) {
const expectedApiKey =
PropertiesService.getScriptProperties().getProperty("api_key");
if (expectedApiKey == null) {
console.error('No script property "api_key" set up!');
return makeResponse({
status: "error",
message: 'No script property "api_key" set up',
});
}
const apiKey = request.parameter["api_key"];
if (apiKey == null || apiKey !== expectedApiKey)
return makeResponse({
status: "fail",
data: {
authentication: 'Invalid or missing URL parameter "api_key"',
},
});
return func();
}