Skip to content

Commit

Permalink
add Promise.http
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Apr 11, 2023
1 parent a071637 commit 4950c34
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
40 changes: 40 additions & 0 deletions http.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

function Promise.http(http, url, opts)
assert(http, "http instance is nil")
assert(url, "no url given")

-- defaults
opts = opts or {}

return Promise.new(function(resolve, reject)
local extra_headers = {}

local data = opts.post_data
if type(data) == "table" then
-- serialize as json
data = minetest.write_json(data)
table.insert(extra_headers, "Content-Type: application/json")
end

http.fetch({
url = url,
extra_headers = extra_headers,
timeout = opts.timeout or 10,
method = opts.method or "GET",
data = data
}, function(res)
if res.succeeded and res.code == 200 then
if opts.json then
resolve(minetest.parse_json(res.data))
else
resolve(res.data)
end
else
reject({
code = res.code or 0,
data = res.data
})
end
end)
end)
end
8 changes: 8 additions & 0 deletions http.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local http = ...

mtt.register("Promise.http", function(callback)
Promise.http(http, "https://api.chucknorris.io/jokes/random", { json = true }):next(function(joke)
assert(type(joke.value) == "string")
callback()
end)
end)
4 changes: 4 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ local MP = minetest.get_modpath("promise")

dofile(MP.."/promise.lua")
dofile(MP.."/util.lua")
dofile(MP.."/http.lua")
dofile(MP.."/formspec.lua")

if minetest.get_modpath("mtt") and mtt.enabled then
local http = assert(minetest.request_http_api())

dofile(MP .. "/promise.spec.lua")
dofile(MP .. "/util.spec.lua")
loadfile(MP .. "/http.spec.lua")(http)
end
21 changes: 20 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@ TODO

```lua
Promise.formspec(player, "size[2,2]button[0,0;2,2;mybutton;label]")
:then(function(data)
:next(function(data)
assert(data.player:get_player_name())
assert(data.fields.mybutton == true)
end)
```

```lua
local http = minetest.request_http_api()

-- call chuck norris api: https://api.chucknorris.io/ and expect json-response
Promise.http(http, "https://api.chucknorris.io/jokes/random", { json = true })
:next(function(joke)
assert(type(joke.value) == "string")
end)

-- post json-payload with 10 second timeout and expect raw string-response (or error)
Promise.http(http, "http://localhost/stuff", { method = "POST", timeout = 10, data = { x=123 } })
:next(function(result)
assert(result)
end):catch(function(result)
assert(result.code == 500)
assert(result.data == "Server error")
end)
```

# License

* Code: MIT
3 changes: 2 additions & 1 deletion test/minetest.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mg_name = v7
mtt_enable = true
mtt_filter = promise
mtt_enable_coverage = true
secure.trusted_mods = mtt
secure.trusted_mods = mtt
secure.http_mods = promise

0 comments on commit 4950c34

Please sign in to comment.