-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8bf3d0
commit be99201
Showing
4 changed files
with
47 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters