Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nic option config for json response #17

Merged
merged 2 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/nic.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
How to use the NIC:
Send a digilines signal with the URL you want to download. The HTTPRequestResult table will be sent back on the same channel.

# Examples

## GET request with a plain url
```lua
-- request
digiline_send("nic", "http://example.com")
-- response
event.msg = {
code = 200,
succeeded = true,
data = "<html></html>"
}
```

## GET request with parsed json response
```lua
-- request
digiline_send("nic", {
url = "http://example.com",
parse_json = true
})
-- response
event.msg = {
code = 200,
succeeded = true,
data = {
my = "data"
}
}
```
25 changes: 21 additions & 4 deletions nic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,30 @@ minetest.register_node("digistuff:nic", {
action = function(pos,node,channel,msg)
local meta = minetest.get_meta(pos)
if meta:get_string("channel") ~= channel then return end
if type(msg) ~= "string" then return end
local url
local parse_json = false
-- parse message
if type(msg) == "string" then
-- simple string data
url = msg
elseif type(msg) == "table" and type(msg.url) == "string" then
-- config object
url = msg.url
parse_json = msg.parse_json
else
-- not supported
return
end
http.fetch({
url = msg,
timeout = 5,
user_agent = "Minetest Digilines Modem",
url = url,
timeout = 5,
user_agent = "Minetest Digilines Modem"
},
function(res)
if type(res.data) == "string" and parse_json then
-- parse json data and replace payload
res.data = minetest.parse_json(res.data)
end
digilines.receptor_send(pos, digilines.rules.default, channel, res)
end)
end
Expand Down