forked from loafle/openapi-generator-original
* 1st commit of lua generator * add petstore sample * various fixes based on feedback * better return handling * check status code 1st letter * add support for query parameters, auth, body param * more fixes based on daurnimator feedback * more fixes based on daurnimator feedback * fix a few more issues found by luac -p * use lower case for http header key name * more fixes based on feedback * update lua sample * skip auto-generated readme, doc, test files * use tab instead of 2-space * fix container return type * fix cast method call * fix cast function call
355 lines
9.6 KiB
Lua
355 lines
9.6 KiB
Lua
--[[
|
|
Swagger Petstore
|
|
|
|
This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
|
|
|
|
OpenAPI spec version: 1.0.0
|
|
Contact: apiteam@swagger.io
|
|
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
|
]]
|
|
|
|
--package petstore
|
|
|
|
local http_request = require "http.request"
|
|
local http_util = require "http.util"
|
|
local dkjson = require "dkjson"
|
|
local basexx = require "basexx"
|
|
|
|
-- model import
|
|
local petstore_user_api = require "petstore.api.user_api"
|
|
|
|
local petstore= {}
|
|
local petstore_mt = {
|
|
__name = "user_api";
|
|
__index = petstore;
|
|
}
|
|
|
|
local function new_user_api(host, basePath, schemes)
|
|
local schemes_map = {}
|
|
for _,v in ipairs(schemes) do
|
|
schemes_map[v] = v
|
|
end
|
|
local default_scheme = schemes_map.https or schemes_map.http
|
|
return setmetatable({
|
|
host = host;
|
|
basePath = basePath or "http://petstore.swagger.io/v2";
|
|
schemes = schemes_map;
|
|
default_scheme = default_scheme;
|
|
http_username = nil;
|
|
http_password = nil;
|
|
api_key = {};
|
|
access_token = nil;
|
|
}, petstore_mt)
|
|
end
|
|
|
|
function user_api:create_user(body)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user",
|
|
self.basePath);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "POST")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
req:set_body(dkjson.encode(body))
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:create_users_with_array_input(body)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/createWithArray",
|
|
self.basePath);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "POST")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
req:set_body(dkjson.encode(body))
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:create_users_with_list_input(body)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/createWithList",
|
|
self.basePath);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "POST")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
req:set_body(dkjson.encode(body))
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:delete_user(username)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/%s",
|
|
self.basePath, username);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "DELETE")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:get_user_by_name(username)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/%s",
|
|
self.basePath, username);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "GET")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
-- exception when getting the HTTP body
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
local result, _, err3 = dkjson.decode(body)
|
|
-- exception when decoding the HTTP body
|
|
if result == nil then
|
|
return nil, err3
|
|
end
|
|
return cast_user(result), headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:login_user(username, password)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/login?username=%s&password=%s",
|
|
self.basePath, http_util.encodeURIComponent(username), http_util.encodeURIComponent(password));
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "GET")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
-- exception when getting the HTTP body
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
local result, _, err3 = dkjson.decode(body)
|
|
-- exception when decoding the HTTP body
|
|
if result == nil then
|
|
return nil, err3
|
|
end
|
|
return result, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:logout_user()
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/logout",
|
|
self.basePath);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "GET")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|
|
function user_api:update_user(username, body)
|
|
local req = http_request.new_from_uri({
|
|
scheme = self.default_scheme;
|
|
host = self.host;
|
|
path = string.format("%s/user/%s",
|
|
self.basePath, username);
|
|
})
|
|
|
|
-- set HTTP verb
|
|
req.headers:upsert(":method", "PUT")
|
|
-- TODO: create a function to select proper content-type
|
|
-- ref: https://github.com/swagger-api/swagger-codegen/pull/6252#issuecomment-321199879
|
|
--local var_accept = { "application/xml", "application/json" }
|
|
req.headers:upsert("content-type", "application/xml")
|
|
|
|
req:set_body(dkjson.encode(body))
|
|
|
|
|
|
-- make the HTTP call
|
|
local headers, stream, errno = req:go()
|
|
if not headers then
|
|
return nil, stream, errno
|
|
end
|
|
local http_status = headers:get(":status")
|
|
if http_status:sub(1,1) == "2" then
|
|
return nil, headers
|
|
else
|
|
local body, err, errno2 = stream:get_body_as_string()
|
|
if not body then
|
|
return nil, err, errno2
|
|
end
|
|
stream:shutdown()
|
|
-- return the error message (http body)
|
|
return nil, http_status, body
|
|
end
|
|
end
|
|
|