2013-07-24 16:30:23 +02:00
|
|
|
local JSON = require('util/JSON')
|
2013-07-26 10:18:55 +02:00
|
|
|
local settings = require('util.settings')
|
|
|
|
local defaults = require('conf_defaults')
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
M.__index = M
|
|
|
|
|
2013-07-24 16:30:23 +02:00
|
|
|
local REQUEST_ID_ARGUMENT = 'rq_id'
|
2013-07-08 16:53:45 +02:00
|
|
|
|
2013-07-24 16:30:23 +02:00
|
|
|
M.httpStatusCode, M.httpStatusText, M.contentType = nil, nil, nil
|
2013-08-16 20:03:53 +02:00
|
|
|
M.binaryData, M.binarySavename = nil, nil
|
|
|
|
|
|
|
|
local function printHeaderLine(headerType, headerValue)
|
|
|
|
io.write(headerType .. ": " .. headerValue .. "\r\n")
|
|
|
|
end
|
2013-07-17 08:06:04 +02:00
|
|
|
|
|
|
|
|
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 = {} }
|
2013-07-24 16:30:23 +02:00
|
|
|
self:setHttpStatus(200, 'OK')
|
|
|
|
self:setContentType('text/plain;charset=UTF-8')
|
|
|
|
--self:setContentType('application/json;charset=UTF-8')
|
2013-09-02 15:01:16 +02:00
|
|
|
|
|
|
|
-- a queue for functions to be executed when the response has bin given
|
|
|
|
-- needed for api calls like network/associate, which requires a restart of the webserver
|
|
|
|
self.postResponseQueue = {}
|
|
|
|
|
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-09-02 15:01:16 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if settings.API_INCLUDE_ENDPOINT_INFO == true then
|
2013-07-24 16:30:23 +02:00
|
|
|
self.body['module'] = requestObject:getRequestedApiModule()
|
|
|
|
self.body['function'] = requestObject:getRealApiFunctionName() or ''
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
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-24 16:30:23 +02:00
|
|
|
function M:setContentType(contentType)
|
|
|
|
if contentType ~= nil then self.contentType = contentType end
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:setSuccess(msg)
|
2013-07-24 16:30:23 +02:00
|
|
|
self.body.status = 'success'
|
|
|
|
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)
|
2013-07-24 16:30:23 +02:00
|
|
|
self.body.status = 'fail'
|
|
|
|
if msg ~= '' then self.body.msg = msg end
|
2013-07-08 19:02:20 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:setError(msg)
|
2013-07-24 16:30:23 +02:00
|
|
|
self.body.status = 'error'
|
|
|
|
if msg ~= '' then self.body.msg = msg end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
self:addData('more_info', 'http://' .. defaults.API_BASE_URL_PATH .. '/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
|
2013-07-24 16:30:23 +02:00
|
|
|
--(e.g.: response:addData('data', {f1=3, f2='x'}))
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:addData(k, v)
|
|
|
|
self.body.data[k] = v
|
2013-08-16 20:03:53 +02:00
|
|
|
self.binaryData = nil
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
2013-09-02 15:01:16 +02:00
|
|
|
function M:addPostResponseFunction(fn)
|
|
|
|
local utils = require('util.utils')
|
|
|
|
local log = require('util.logger')
|
|
|
|
log:info("Response:addPostResponseFunction: " .. utils.dump(fn))
|
|
|
|
table.insert(self.postResponseQueue, fn)
|
|
|
|
log:info(" self.postResponseQueue: " .. utils.dump(self.postResponseQueue))
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:executePostResponseQueue()
|
|
|
|
local utils = require('util.utils')
|
|
|
|
local log = require('util.logger')
|
|
|
|
log:info("Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue))
|
|
|
|
|
|
|
|
for i,fn in ipairs(self.postResponseQueue) do fn() end
|
|
|
|
end
|
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
function M:apiURL(mod, func)
|
|
|
|
if not mod then return nil end
|
|
|
|
if func then func = '/' .. func else func = "" end
|
|
|
|
return 'http://' .. defaults.API_BASE_URL_PATH .. '/cgi-bin/d3dapi/' .. mod .. func
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
function M:serializeAsJson()
|
|
|
|
return JSON:encode(self.body)
|
|
|
|
end
|
|
|
|
|
2013-07-09 01:49:56 +02:00
|
|
|
function M:send()
|
2013-08-16 20:03:53 +02:00
|
|
|
printHeaderLine("Status", self.httpStatusCode .. " " .. self.httpStatusText)
|
2013-08-21 13:39:45 +02:00
|
|
|
printHeaderLine("Content-Type", self.contentType)
|
2013-08-16 20:03:53 +02:00
|
|
|
printHeaderLine("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
if self.binaryData == nil then
|
|
|
|
io.write("\r\n")
|
|
|
|
print(self:serializeAsJson())
|
|
|
|
else
|
2013-08-21 13:39:45 +02:00
|
|
|
printHeaderLine("Content-Disposition", "attachment; filename=" .. self.binarySavename)
|
2013-08-16 20:03:53 +02:00
|
|
|
io.write("\r\n")
|
|
|
|
io.write(self.binaryData)
|
|
|
|
end
|
|
|
|
end
|
2013-08-07 11:15:55 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
function M:setBinaryFileData(rFile, saveName, contentType)
|
|
|
|
if type(rFile) ~= 'string' or rFile:len() == 0 then return false end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
local f,msg = io.open(rFile, "rb")
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
if not f then return nil,msg end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
self.binaryData = f:read("*all")
|
|
|
|
f:close()
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
self.binarySavename = saveName
|
|
|
|
self:setContentType(contentType)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
return true
|
2013-07-09 01:49:56 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
return M
|