User Tools

Site Tools


action_http

This is an old revision of the document!


http.lua
--
-- Copyright (C) 2018 CurlyMo & Niek
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--
 
local M = {}
 
function M.check(parameters)
	local nr1 = -1;
	local nr2 = -1;
	local nr3 = -1;
 
	if parameters['GET'] == nil and parameters['POST'] == nil then
		error("http action is missing a \"GET\" or \"POST\" statement");
	end
 
	if parameters['GET'] ~= nil and parameters['POST'] ~= nil then
		error("http action cannot have \"GET\" and \"POST\" statements at the same time");
	end
 
	if parameters['GET'] ~= nil then
		if (#parameters['GET']['value'] ~= 1 or parameters['GET']['value'][2] ~= nil) then
			error("http action \"GET\" only takes one argument");
		end
 
		if parameters['MIMETYPE'] ~= nil
			error("http action \"GET\" does not take a \"MIMETYPE\" statement");
		end
 
		if parameters['DATA'] ~= nil
			error("http action \"GET\" does not take a \"MIMETYPE\" statement");
		end
	end
 
	if parameters['POST'] ~= nil then
		if (#parameters['POST']['value'] ~= 1 or parameters['POST']['value'][2] ~= nil) then
			error("http action \"POST\" only takes one argument");
		end
 
		if parameters['DATA'] == nil then
			error("http action \"POST\" request requires a \"DATA\" statement");
		end
 
		nr1 = parameters['POST']['order'];
		nr2 = parameters['DATA']['order'];
 
		if nr2 < nr1 then
			error("http actions are formatted as \"http POST ... DATA ...\"");
			return -1;
		end
	end
 
	if parameters['MIMETYPE'] ~= nil then
		if(#parameters['MIMETYPE']['value'] ~= 1 or parameters['MIMETYPE']['value'][2] ~= nil) then
			error("http action \"MIMETYPE\" only takes one argument");
		end
 
		nr3 = parameters['MIMETYPE']['order'];
 
		if nr3 < nr2 or nr3 < nr1 then
			error("http actions are formatted as \"http POST ... DATA ... MIMETYPE ...\"");
			return -1;
		end
 
	end
 
	if parameters['DATA'] ~= nil and (#parameters['DATA']['value'] ~= 1 or parameters['DATA']['value'][2] ~= nil) then
		error("http action \"DATA\" only takes one argument");
	end
 
	return 1;
end
 
function M.callback(http)
	if http.getCode() == 200 then
		error("http action calling ".. http.getUrl() .. " succeeded");
	else
		error("http action calling ".. http.getUrl() .. " failed with code " .. tostring(http.getCode()));
	end
end
 
function M.run(parameters)
 
	local httpobj = pilight.network.http();
	httpobj.setCallback("callback");
 
	if parameters['GET'] ~= nil then
		if parameters['DATA'] ~=nil then
			httpobj.setUrl(parameters['GET']['value'][1] .. "?" .. parameters['DATA']['value'][1]);
		else
			httpobj.setUrl(parameters['GET']['value'][1]);
		end
		httpobj.setMimetype("");
		httpobj.get();
	else
		httpobj.setUrl(parameters['POST']['value'][1]);
		httpobj.setMimetype(parameters['MIMETYPE']['value'][1]);
		httpobj.setData(parameters['DATA']['value'][1]);
		httpobj.post();
	end
 
	return 1;
end
 
function M.parameters()
	return "POST", "GET", "DEVICE", "MIMETYPE", "DATA";
end
 
function M.info()
	return {
		name = "http",
		version = "0.1",
		reqversion = "8.1.1",
		reqcommit = "0"
	}
end
 
return M;
action_http.1531730670.txt.gz · Last modified: 2018/07/16 08:44 by curlymo