Skip to content

Commit

Permalink
add api util
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 6, 2021
1 parent c8bf3d0 commit be99201
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
26 changes: 6 additions & 20 deletions api/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,12 @@ local http, url = ...
-- @param create_schema the new schema as table
-- @return a promise with the result
function blockexchange.api.create_schema(token, create_schema)
return Promise.new(function(resolve, reject)
local json = minetest.write_json(create_schema);
http.fetch({
url = url .. "/api/schema",
extra_headers = {
"Content-Type: application/json",
"Authorization: " .. token
},
timeout = 10,
method = "POST",
data = json
}, function(res)
if res.succeeded and res.code == 200 then
local schema = minetest.parse_json(res.data)
resolve(schema)
else
reject(res.code or 0)
end
end)
end)
return blockexchange.api.json({
endpoint = "schema",
data = create_schema,
method = "POST",
token = token
})
end

--- updates the stats of an existing schema
Expand Down
36 changes: 36 additions & 0 deletions api/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---------
-- api utilities

local http, url = ...

function blockexchange.api.json(opts)
return Promise.new(function(resolve, reject)
local extra_headers = {
"Content-Type: application/json"
}

if opts.token then
table.insert(extra_headers, "Authorization: " .. opts.token)
end

http.fetch({
url = url .. "/api/" .. opts.endpoint,
extra_headers = extra_headers,
timeout = opts.timeout or 10,
method = opts.method or "GET",
data = opts.data and minetest.write_json(opts.data)
}, function(res)
if res.succeeded and res.code == 200 then
local obj = minetest.parse_json(res.data)
resolve(obj)
else
local obj
if res.data and res.data ~= "" then
-- TODO: data is empty if the status-code is 400 :/
obj = minetest.parse_json(res.data)
end
reject(res.code or 0, obj)
end
end)
end)
end
5 changes: 4 additions & 1 deletion commands/save.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ function blockexchange.save(playername, pos1, pos2, name, local_save)
-- start save worker with context
blockexchange.save_worker(ctx)
end):catch(function(http_code)
local msg = "[blockexchange] create schema failed with http code: " .. (http_code or "unknown")
local msg = "[blockexchange] create schema failed with http code: " ..
(http_code or "unknown") ..
", you might have exceeded the upload limits"

minetest.log("error", msg)
minetest.chat_send_player(playername, minetest.colorize("#ff0000", msg))
ctx.promise:reject(msg)
Expand Down
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ end

-- http api
if blockexchange.is_online then
loadfile(MP.."/api/util.lua")(http, blockexchange.url)
loadfile(MP.."/api/info.lua")(http, blockexchange.url)
loadfile(MP.."/api/schema.lua")(http, blockexchange.url)
loadfile(MP.."/api/schemapart.lua")(http, blockexchange.url)
Expand Down

0 comments on commit be99201

Please sign in to comment.