2013-07-17 08:06:04 +02:00
|
|
|
local JSON = require("util/JSON")
|
2013-07-17 22:55:27 +02:00
|
|
|
local s = require("util.settings")
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
M.__index = M
|
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
local REQUEST_ID_ARGUMENT = "rq_id"
|
|
|
|
|
2013-07-17 08:06:04 +02:00
|
|
|
M.httpStatusCode, M.httpStatusText = nil, nil
|
|
|
|
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
setmetatable(M, {
|
|
|
|
__call = function(cls, ...)
|
|
|
|
return cls.new(...)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
--requestObject should always be passed (except on init failure, when it is not yet available)
|
|
|
|
function M.new(requestObject)
|
2013-07-08 13:34:27 +02:00
|
|
|
local self = setmetatable({}, M)
|
|
|
|
|
2013-07-17 08:06:04 +02:00
|
|
|
self.body = { status = nil, data = {} }
|
|
|
|
self:setHttpStatus(200, "OK")
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
if requestObject ~= nil then
|
|
|
|
local rqId = requestObject:get(REQUEST_ID_ARGUMENT)
|
|
|
|
if rqId ~= nil then self.body[REQUEST_ID_ARGUMENT] = rqId end
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
if s.API_INCLUDE_ENDPOINT_INFO == true then
|
2013-07-10 00:32:43 +02:00
|
|
|
self.body["module"] = requestObject:getRequestedApiModule()
|
|
|
|
self.body["function"] = requestObject:getRealApiFunctionName() or ""
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2013-07-17 08:06:04 +02:00
|
|
|
function M:setHttpStatus(code, text)
|
|
|
|
if code ~= nil then self.httpStatusCode = code end
|
|
|
|
if text ~= nil then self.httpStatusText = text end
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:setSuccess(msg)
|
|
|
|
self.body.status = "success"
|
2013-07-08 16:53:45 +02:00
|
|
|
if msg ~= "" then self.body.msg = msg end
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 19:02:20 +02:00
|
|
|
function M:setFail(msg)
|
|
|
|
self.body.status = "fail"
|
|
|
|
if msg ~= "" then self.body.msg = msg end
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:setError(msg)
|
|
|
|
self.body.status = "error"
|
2013-07-08 16:53:45 +02:00
|
|
|
if msg ~= "" then self.body.msg = msg end
|
2013-07-17 08:06:04 +02:00
|
|
|
|
|
|
|
self:addData("more_info", "http://doodle3d.nl/wiki/wiki/communication-api")
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--NOTE: with this method, to add nested data, it is necessary to precreate the table and add it with its root key
|
|
|
|
--(e.g.: response:addData("data", {f1=3, f2="x"}))
|
|
|
|
function M:addData(k, v)
|
|
|
|
self.body.data[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:serializeAsJson()
|
|
|
|
return JSON:encode(self.body)
|
|
|
|
end
|
|
|
|
|
2013-07-09 01:49:56 +02:00
|
|
|
function M:send()
|
2013-07-17 08:06:04 +02:00
|
|
|
io.write("Status: " .. self.httpStatusCode .. " " .. self.httpStatusText .. "\r\n")
|
|
|
|
io.write ("Content-type: text/plain\r\n\r\n")
|
2013-07-09 01:49:56 +02:00
|
|
|
print(self:serializeAsJson())
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
return M
|