From 8dabcb8d6b85b7395fbb147213667bf9588c5c1b Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Thu, 4 Mar 2021 17:32:02 +0100 Subject: [PATCH 1/2] add nic option config for json response and headers --- docs/nic.txt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ nic.lua | 28 ++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/docs/nic.txt b/docs/nic.txt index 1bb9f7e..8d188c1 100644 --- a/docs/nic.txt +++ b/docs/nic.txt @@ -1,2 +1,48 @@ 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 = "" +} +``` + +## GET request with headers +```lua +-- request +digiline_send("nic", { + url = "http://example.com", + headers = {"Accept: text/plain"} +}) +-- response +event.msg = { + code = 200, + succeeded = true, + data = "blah" +} +``` + +## 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" + } +} +``` diff --git a/nic.lua b/nic.lua index da1e70d..bd8da2c 100644 --- a/nic.lua +++ b/nic.lua @@ -48,13 +48,33 @@ 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 headers + 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 + headers = msg.headers + 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", + extra_headers = headers }, 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 From 0d2d670c9e32425478f16006637958d97a51789c Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 1 Jun 2021 16:55:27 +0200 Subject: [PATCH 2/2] remove header stuff --- docs/nic.txt | 15 --------------- nic.lua | 5 +---- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/docs/nic.txt b/docs/nic.txt index 8d188c1..453926a 100644 --- a/docs/nic.txt +++ b/docs/nic.txt @@ -15,21 +15,6 @@ event.msg = { } ``` -## GET request with headers -```lua --- request -digiline_send("nic", { - url = "http://example.com", - headers = {"Accept: text/plain"} -}) --- response -event.msg = { - code = 200, - succeeded = true, - data = "blah" -} -``` - ## GET request with parsed json response ```lua -- request diff --git a/nic.lua b/nic.lua index bd8da2c..c717fb2 100644 --- a/nic.lua +++ b/nic.lua @@ -49,7 +49,6 @@ minetest.register_node("digistuff:nic", { local meta = minetest.get_meta(pos) if meta:get_string("channel") ~= channel then return end local url - local headers local parse_json = false -- parse message if type(msg) == "string" then @@ -58,7 +57,6 @@ minetest.register_node("digistuff:nic", { elseif type(msg) == "table" and type(msg.url) == "string" then -- config object url = msg.url - headers = msg.headers parse_json = msg.parse_json else -- not supported @@ -67,8 +65,7 @@ minetest.register_node("digistuff:nic", { http.fetch({ url = url, timeout = 5, - user_agent = "Minetest Digilines Modem", - extra_headers = headers + user_agent = "Minetest Digilines Modem" }, function(res) if type(res.data) == "string" and parse_json then