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-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-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-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-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-07-17 08:06:04 +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
|
|
|
|
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-07-17 08:06:04 +02:00
|
|
|
io.write("Status: " .. self.httpStatusCode .. " " .. self.httpStatusText .. "\r\n")
|
2013-07-24 16:30:23 +02:00
|
|
|
io.write ("Content-type: " .. self.contentType .. "\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
|