-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
111 lines (102 loc) · 3.22 KB
/
Copy pathserver.lua
File metadata and controls
111 lines (102 loc) · 3.22 KB
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
--[[
FiveHub API client - server side.
Exports: ListAssets, GetAsset, GetAssetUrl, DeleteAsset
Usage from another resource:
exports.fivehub:ListAssets(function(err, data) ... end)
exports.fivehub:GetAsset(assetId, function(err, asset) ... end)
exports.fivehub:GetAssetUrl(assetId, expiresIn, function(err, data) ... end)
exports.fivehub:DeleteAsset(assetId, function(err) ... end)
]]
local function buildHeaders(includeJsonContentType)
local headers = {
["X-API-KEY"] = Config.ApiKey,
}
if Config.OrganizationId and Config.OrganizationId ~= "" then
headers["X-Organization-Id"] = Config.OrganizationId
end
if includeJsonContentType then
headers["Content-Type"] = "application/json"
end
return headers
end
local function parseJsonBody(body)
if not body or body == "" then
return nil
end
local ok, decoded = pcall(json.decode, body)
if not ok or not decoded then
return nil
end
return decoded
end
local function request(method, path, body, contentType, cb)
local url = Config.BaseUrl:gsub("/$", "") .. path
local headers = buildHeaders(not body or contentType == "application/json")
if contentType and contentType ~= "application/json" then
headers["Content-Type"] = contentType
end
PerformHttpRequest(url, {
method = method,
headers = headers,
data = body,
}, function(statusCode, responseBody, responseHeaders)
if statusCode >= 200 and statusCode < 300 then
local data = parseJsonBody(responseBody)
cb(nil, data)
else
local data = parseJsonBody(responseBody)
local msg = (data and data.error) or ("HTTP " .. tostring(statusCode))
cb(msg, nil)
end
end)
end
local function encodeQuery(t)
local parts = {}
for k, v in pairs(t) do
table.insert(parts, string.format("%s=%s", k, v))
end
return table.concat(parts, "&")
end
-- List assets (limit, offset)
exports("ListAssets", function(options, cb)
if type(options) == "function" then
cb = options
options = {}
end
options = options or {}
local limit = math.min(tonumber(options.limit) or 50, 100)
local offset = tonumber(options.offset) or 0
local path = "/api/assets?" .. encodeQuery({ limit = limit, offset = offset })
request("GET", path, nil, nil, cb)
end)
-- Get single asset by ID
exports("GetAsset", function(assetId, cb)
if type(assetId) ~= "string" or assetId == "" then
return cb("assetId is required", nil)
end
local path = "/api/assets/" .. assetId
request("GET", path, nil, nil, cb)
end)
-- Get signed URL for asset (expiresIn in seconds, default 3600)
exports("GetAssetUrl", function(assetId, expiresIn, cb)
if type(assetId) ~= "string" or assetId == "" then
return cb("assetId is required", nil)
end
if type(expiresIn) == "function" then
cb = expiresIn
expiresIn = 3600
end
expiresIn = tonumber(expiresIn) or 3600
local path = "/api/assets/" .. assetId .. "/url?expiresIn=" .. tostring(expiresIn)
request("GET", path, nil, nil, cb)
end)
-- Delete asset by ID
exports("DeleteAsset", function(assetId, cb)
if type(assetId) ~= "string" or assetId == "" then
return cb("assetId is required")
end
local path = "/api/assets/" .. assetId
request("DELETE", path, nil, nil, function(err, _)
cb(err)
end)
end)