diff --git a/api/schema.lua b/api/schema.lua index 6566bdc..82c45eb 100644 --- a/api/schema.lua +++ b/api/schema.lua @@ -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 diff --git a/api/util.lua b/api/util.lua new file mode 100644 index 0000000..d3bb90c --- /dev/null +++ b/api/util.lua @@ -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 \ No newline at end of file diff --git a/commands/save.lua b/commands/save.lua index 878c086..506e7ab 100644 --- a/commands/save.lua +++ b/commands/save.lua @@ -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) diff --git a/init.lua b/init.lua index 80fb64b..752ede3 100644 --- a/init.lua +++ b/init.lua @@ -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)