From 258770675e02dcdc04d0cd432b9d618554c80116 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 24 Jul 2013 16:30:23 +0200 Subject: [PATCH] Fix quoting style in response.lua, add fucntion to set HTTP content type. --- src/rest/response.lua | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/rest/response.lua b/src/rest/response.lua index 6c17012..5ca887a 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -1,12 +1,12 @@ -local JSON = require("util/JSON") -local s = require("util.settings") +local JSON = require('util/JSON') +local s = require('util.settings') local M = {} M.__index = M -local REQUEST_ID_ARGUMENT = "rq_id" +local REQUEST_ID_ARGUMENT = 'rq_id' -M.httpStatusCode, M.httpStatusText = nil, nil +M.httpStatusCode, M.httpStatusText, M.contentType = nil, nil, nil setmetatable(M, { @@ -20,15 +20,17 @@ function M.new(requestObject) local self = setmetatable({}, M) self.body = { status = nil, data = {} } - self:setHttpStatus(200, "OK") + self:setHttpStatus(200, 'OK') + self:setContentType('text/plain;charset=UTF-8') + --self:setContentType('application/json;charset=UTF-8') if requestObject ~= nil then local rqId = requestObject:get(REQUEST_ID_ARGUMENT) if rqId ~= nil then self.body[REQUEST_ID_ARGUMENT] = rqId end if s.API_INCLUDE_ENDPOINT_INFO == true then - self.body["module"] = requestObject:getRequestedApiModule() - self.body["function"] = requestObject:getRealApiFunctionName() or "" + self.body['module'] = requestObject:getRequestedApiModule() + self.body['function'] = requestObject:getRealApiFunctionName() or '' end end @@ -40,25 +42,29 @@ function M:setHttpStatus(code, text) if text ~= nil then self.httpStatusText = text end end +function M:setContentType(contentType) + if contentType ~= nil then self.contentType = contentType end +end + function M:setSuccess(msg) - self.body.status = "success" - if msg ~= "" then self.body.msg = msg end + self.body.status = 'success' + if msg ~= '' then self.body.msg = msg end end function M:setFail(msg) - self.body.status = "fail" - if msg ~= "" then self.body.msg = msg end + self.body.status = 'fail' + if msg ~= '' then self.body.msg = msg end end function M:setError(msg) - self.body.status = "error" - if msg ~= "" then self.body.msg = msg end + self.body.status = 'error' + if msg ~= '' then self.body.msg = msg end - self:addData("more_info", "http://doodle3d.nl/wiki/wiki/communication-api") + self:addData('more_info', 'http://doodle3d.nl/wiki/wiki/communication-api') 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"})) +--(e.g.: response:addData('data', {f1=3, f2='x'})) function M:addData(k, v) self.body.data[k] = v end @@ -69,7 +75,7 @@ end function M:send() io.write("Status: " .. self.httpStatusCode .. " " .. self.httpStatusText .. "\r\n") - io.write ("Content-type: text/plain\r\n\r\n") + io.write ("Content-type: " .. self.contentType .. "\r\n\r\n") print(self:serializeAsJson()) end