Skip to content

Commit

Permalink
Add nic format option for json response (#17)
Browse files Browse the repository at this point in the history
* add nic option config for json response and headers

* remove header stuff

Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
  • Loading branch information
BuckarooBanzay and BuckarooBanzay committed Jun 6, 2021
1 parent ce8f330 commit 126aa82
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
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

0 comments on commit 126aa82

Please sign in to comment.