-
Notifications
You must be signed in to change notification settings - Fork 0
/
pine_api.lua
436 lines (350 loc) · 13.3 KB
/
pine_api.lua
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--- A simple library for communicating with the PineStore API.
local _expect = require "cc.expect"
local PINESTORE_ROOT = "https://pinestore.cc/api/"
local expect, field = _expect.expect, _expect.field
--- Parse a response from PineStore.
---@param handle BinaryResponse|Response? The response from PineStore.
---@param err string? The error message, if any.
---@param err_handle BinaryResponse|Response? The error handle, if any.
---@return boolean success Whether or not the response was successful.
---@return any response The response from PineStore, or the error message.
local function parse_response(handle, err, err_handle)
if not handle and not err_handle then
return false, "Failed to connect to PineStore: " .. err
end
local success, response = pcall(textutils.unserializeJSON, handle and handle.readAll() or err_handle and err_handle.readAll());
(handle or err_handle).close()
if not success or not response then
if err then
return false, "Failed to parse response from pinestore: " .. err
end
return false, "Failed to parse response from pinestore."
end
if response and not response.success then
return false, "Failed to get information from pinestore: " .. response.error
end
return true, response
end
--- Make a get request to PineStore.
---@param url string The endpoint to get.
---@param authorization string? The authorization token to use.
---@return boolean success Whether or not the response was successful.
---@return any response The response from PineStore, or the error message.
local function pine_get(url, authorization)
return parse_response(http.get(
PINESTORE_ROOT .. url,
{
---@diagnostic disable-next-line If it's nil it shrimply doesn't get appended.
authorization = authorization
}
))
---@FIXME actually implement the authorization part.
end
--- Make a post request PineStore
---@param url string The endpoint to get.
---@param data string The data to send.
---@param authorization string? The authorization token to use.
---@return boolean success Whether or not the response was successful.
---@return any response The response from PineStore, or the error message.
local function pine_post(url, data, authorization)
return parse_response(http.post(
PINESTORE_ROOT .. url,
data,
{
["Content-Type"] = "application/json",
---@diagnostic disable-next-line If it's nil it shrimply doesn't get appended.
authorization = authorization
}
))
---@FIXME Actually implement the authorization part.
end
--- PineStore API.
---@type pine_store-base
local pine_api = {
project = {},
projects = {},
user = {},
log = {},
auth = {
profile = {},
project = {},
media = {},
comment = {}
}
}
local auth_token = nil ---@type string?
---@diagnostic disable:duplicate-set-field
-- Does @meta do nothing?
-- ########################################################################## --
-- Non-authorized --
-- ########################################################################## --
-- ################################################################ --
-- Project --
-- ################################################################ --
function pine_api.project.info(id)
expect(1, id, "number")
return pine_get("project/" .. id)
end
function pine_api.project.comments(id)
expect(1, id, "number")
return pine_get("project/" .. id .. "/comments")
end
function pine_api.project.changelog(id)
expect(1, id, "number")
return pine_get("project/" .. id .. "/changelog")
end
function pine_api.project.changelogs(id)
expect(1, id, "number")
return pine_get("project/" .. id .. "/changelogs")
end
-- ################################################################ --
-- Projects --
-- ################################################################ --
function pine_api.projects.list()
return pine_get("projects")
end
function pine_api.projects.search(query)
expect(1, query, "string")
return pine_get("projects/search/?q=" .. textutils.urlEncode(query))
end
function pine_api.projects.named(name)
expect(1, name, "string")
return pine_get("projects/named/?name=" .. textutils.urlEncode(name))
end
-- ################################################################ --
-- User --
-- ################################################################ --
function pine_api.user.info(id)
expect(1, id, "string")
return pine_get("user/" .. id)
end
function pine_api.user.projects(id)
expect(1, id, "number")
return pine_get("user/" .. id .. "/projects")
end
-- ################################################################ --
-- Log --
-- ################################################################ --
function pine_api.log.view(id)
expect(1, id, "number")
return pine_post("log/view", textutils.serializeJSON {
projectId = id
})
end
function pine_api.log.download(id)
expect(1, id, "number")
return pine_post("log/download", textutils.serializeJSON {
projectId = id
})
end
-- ########################################################################## --
-- Authorized --
-- ########################################################################## --
-- ################################################################ --
-- profile --
-- ################################################################ --
function pine_api.auth.profile.info()
return pine_get("auth/profile", auth_token)
end
function pine_api.auth.profile.projects()
return pine_get("auth/profile/projects", auth_token)
end
function pine_api.auth.profile.update(data)
expect(1, data, "table")
-- Validation: Collect the error from `field`, then supply more information about the error.
local step, pos = "main", -1
local ok, err = pcall(function()
-- Validate the input table.
field(data, "allow_null", "boolean", "nil")
field(data, "name", "string", "nil")
field(data, "about", "string", "nil")
field(data, "about_markdown", "string", "nil")
field(data, "connections", "table", "nil")
-- Validate each connection.
if data.connections then
step = "connections"
for i, connection in ipairs(data.connections) do
pos = i
field(connection, "id", "string")
field(connection, "display", "string")
field(connection, "link", "string", "nil")
end
end
end)
if not ok then
if step == "main" then
-- Error with one of the fields in the main table. We shouldn't need a whole lot more information here.
-- Just supply the error and make sure it's known that its the caller's fault.
error(err, 2)
else
-- Error with one of the connections. We need to supply more information.
error(("Connection #%d: %s"):format(pos, err), 2)
end
end
return pine_post("auth/profile/update", textutils.serializeJSON(data), auth_token)
end
function pine_api.auth.profile.get_options()
return pine_get("auth/profile/options", auth_token)
end
function pine_api.auth.profile.set_options(options)
expect(1, options, "table")
-- Validation: Collect the error from `field`, then supply more information about the error.
local ok, err = pcall(function()
field(options, "user_discord", "string", "nil")
field(options, "discord_notifications", "boolean", "nil")
field(options, "diiscord_noti_comment", "boolean", "nil")
field(options, "discord_noti_reply", "boolean", "nil")
field(options, "discord_noti_newfollow_user", "boolean", "nil")
field(options, "discord_noti_newfollow_project", "boolean", "nil")
field(options, "discord_noti_following_newproject", "boolean", "nil")
field(options, "discord_noti_following_projectupdate", "boolean", "nil")
end)
if not ok then
-- We shouldn't need a whole lot more information here.
-- Just supply the error and make sure it's known that its the caller's fault.
error(err, 2)
end
return pine_post("auth/profile/options", textutils.serializeJSON(options), auth_token)
end
-- ################################################################ --
-- project --
-- ################################################################ --
function pine_api.auth.project.update(project_data)
expect(1, project_data, "table")
-- Validation: Collect the error from `field`, then supply more information about the error.
local ok, err = pcall(function()
field(project_data, "projectId", "number")
field(project_data, "allow_null", "boolean", "nil")
field(project_data, "projectname", "string", "nil")
field(project_data, "install_command", "string", "nil")
field(project_data, "download_url", "string", "nil")
field(project_data, "target_file", "string", "nil")
field(project_data, "tags", "table", "nil")
field(project_data, "repository", "string", "nil")
field(project_data, "description_short", "string", "nil")
field(project_data, "description", "string", "nil")
field(project_data, "description_markdown", "string", "nil")
field(project_data, "keywords", "table", "nil")
field(project_data, "visible", "boolean", "nil")
field(project_data, "date_release", "number", "nil")
local function validate_array(arr)
local max_n = 0
for k in pairs(arr) do
if type(k) ~= "number" then
error("Array contains non-integer key: " .. tostring(k), 0)
end
if k > max_n then
max_n = k
end
end
-- Now ensure we can count to `max_n` without any holes.
for i = 1, max_n do
if arr[i] == nil then
error("Array contains hole at index " .. i, 0)
end
end
end
-- Validate `tags` and `keywords` -- they should be string[]
if project_data.tags then
validate_array(project_data.tags)
local ok2, err2 = pcall(function()
for i, tag in ipairs(project_data.tags) do
if type(tag) ~= "string" then
error("Element at index " .. i .. " is not a string", 0)
end
end
end)
if not ok2 then
error("Field 'tags': " .. err2, 0)
end
validate_array(project_data.tags)
end
if project_data.keywords then
validate_array(project_data.keywords)
local ok2, err2 = pcall(function()
for i, keyword in ipairs(project_data.keywords) do
if type(keyword) ~= "string" then
error("Element at index " .. i .. " is not a string", 0)
end
end
end)
if not ok2 then
error("Field 'keywords': " .. err2, 0)
end
end
end)
if not ok then
-- We shouldn't need a whole lot more information here.
-- Just supply the error and make sure it's known that its the caller's fault.
-- For some reason `field` does not do this.
error(err, 2)
end
return pine_post("auth/project/update", textutils.serializeJSON(project_data), auth_token)
end
function pine_api.auth.project.new(name)
expect(1, name, "string")
return pine_post("auth/project/new", textutils.serializeJSON {
name = name
}, auth_token)
end
function pine_api.auth.project.delete(id)
expect(1, id, "number")
return pine_post("auth/project/delete", textutils.serializeJSON {
projectId = id
}, auth_token)
end
function pine_api.auth.project.publish_update(id, body)
expect(1, id, "number")
expect(2, body, "string")
return pine_post("auth/project/publishupdate", textutils.serializeJSON {
projectId = id,
body = body
}, auth_token)
end
-- ################################################################ --
-- media --
-- ################################################################ --
function pine_api.auth.media.new(id, image)
expect(1, id, "number")
expect(2, image, "string")
return pine_post("auth/media", textutils.serializeJSON {
projectId = id,
imageData = image
}, auth_token)
end
function pine_api.auth.media.remove(id, index)
expect(1, id, "number")
expect(2, index, "number")
return pine_post("auth/media/remove", textutils.serializeJSON {
projectId = id,
index = index
}, auth_token)
end
function pine_api.auth.media.set_thumbnail(id, image)
expect(1, id, "number")
expect(2, image, "string")
return pine_post("auth/media/thumbnail", textutils.serializeJSON {
projectId = id,
imageData = image
}, auth_token)
end
-- ################################################################ --
-- comment --
-- ################################################################ --
function pine_api.auth.comment.new(id, body, reply_id)
expect(1, id, "number")
expect(2, body, "string")
expect(3, reply_id, "number", "nil")
return pine_post("auth/comment", textutils.serializeJSON {
projectId = id,
body = body,
replyId = reply_id
}, auth_token)
end
function pine_api.auth.comment.delete(id)
expect(1, id, "number")
return pine_post("auth/comment/delete", textutils.serializeJSON {
commentId = id
}, auth_token)
end
return pine_api --[[@as pine_store-base]]