From 5d14f3f733dd3af31acbb470e71bd684ea2b1462 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 8 Aug 2017 13:35:17 +1000 Subject: [PATCH] samples/client/petstore/lua: Add lua sample (#6244) * samples/client/petstore/lua: Add basic lua GET request * samples/client/petstore/lua: Add classes --- samples/client/petstore/lua/README.md | 1 + samples/client/petstore/lua/main.lua | 12 ++ .../petstore/lua/petstore_client/init.lua | 118 ++++++++++++++++++ .../petstore/lua/petstore_client/pet.lua | 23 ++++ 4 files changed, 154 insertions(+) create mode 100644 samples/client/petstore/lua/README.md create mode 100644 samples/client/petstore/lua/main.lua create mode 100644 samples/client/petstore/lua/petstore_client/init.lua create mode 100644 samples/client/petstore/lua/petstore_client/pet.lua diff --git a/samples/client/petstore/lua/README.md b/samples/client/petstore/lua/README.md new file mode 100644 index 000000000..95104d03e --- /dev/null +++ b/samples/client/petstore/lua/README.md @@ -0,0 +1 @@ +https://github.com/swagger-api/swagger-codegen/issues/4794#issuecomment-320245838 diff --git a/samples/client/petstore/lua/main.lua b/samples/client/petstore/lua/main.lua new file mode 100644 index 000000000..8997347ed --- /dev/null +++ b/samples/client/petstore/lua/main.lua @@ -0,0 +1,12 @@ +local petstore_client = require "petstore_client" +local petstore_client_pet = require "petstore_client.pet" + +local my_pet_http_api = petstore_client.new("petstore.swagger.io", "/v2", {"http"}) + +local my_pet = assert(my_pet_http_api:getPetById(4)) +for k,v in pairs(my_pet) do + print(k,v) +end + +local my_new_pet = petstore_client_pet.new("Mr. Barks") +assert(my_pet_http_api:addPet(my_new_pet)) diff --git a/samples/client/petstore/lua/petstore_client/init.lua b/samples/client/petstore/lua/petstore_client/init.lua new file mode 100644 index 000000000..f9f810659 --- /dev/null +++ b/samples/client/petstore/lua/petstore_client/init.lua @@ -0,0 +1,118 @@ +local http_request = require "http.request" +local http_util = require "http.util" +local dkjson = require "dkjson" + +local petstore_client_pet = require "petstore_client.pet" + +-- API class +local pet_http_api = {} +local pet_http_api_mt = { + __name = "pet-http-api"; + __index = pet_http_api; +} + +local function new_pet_http_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; + schemes = schemes_map; + default_scheme = default_scheme; + }, pet_http_api_mt) +end + +function pet_http_api:getPetById(petId) + local req = http_request.new_from_uri({ + scheme = self.default_scheme; + host = self.host; + path = string.format("%s/pet/%d", self.basePath, petId); + }) + req.headers:upsert("accept", "application/json") + local headers, stream, errno = req:go() + if not headers then + return nil, stream, errno + end + local http_status = headers:get(":status") + if http_status == "200" then + local body, err, errno2 = stream:get_body_as_string() + if not body then + return nil, err, errno2 + end + stream:shutdown() + local result, _, err3 = dkjson.decode(body) + if result == nil then + return nil, err3 + end + return petstore_client_pet.cast(result) + elseif http_status == "400" then + stream:shutdown() + return nil, "Invalid ID supplied" + elseif http_status == "404" then + stream:shutdown() + return nil, "Pet not found" + else + stream:shutdown() + return nil, "Unexpected response status code" + end +end + +function pet_http_api:updatePetWithForm(petId, name, status) + local req = http_request.new_from_uri({ + scheme = self.default_scheme; + host = self.host; + path = string.format("%s/pet/%d", self.basePath, petId); + }) + req.headers:upsert(":method", "POST") + req.headers:upsert("content-type", "application/x-www-form-urlencoded") + req:set_body(http_util.dict_to_query({ + name = name; + status = status; + })) + req.headers:upsert("accept", "application/json") + local headers, stream, errno = req:go() + if not headers then + return nil, stream, errno + end + local http_status = headers:get(":status") + if http_status == "405" then + stream:shutdown() + return nil, "Invalid input" + else + -- TODO: this should handle 200... but it's not in the spec? + stream:shutdown() + return nil, "Unexpected response status code" + end +end + +function pet_http_api:addPet(pet) + local req = http_request.new_from_uri({ + scheme = self.default_scheme; + host = self.host; + path = string.format("%s/pet", self.basePath); + }) + req.headers:upsert(":method", "POST") + req.headers:upsert("content-type", "application/json") + req:set_body(dkjson.encode(pet)) + req.headers:upsert("accept", "application/json") + local headers, stream, errno = req:go() + if not headers then + return nil, stream, errno + end + local http_status = headers:get(":status") + if http_status == "405" then + stream:shutdown() + return nil, "Invalid input" + else + -- TODO: this should handle 200... but it's not in the spec? + stream:shutdown() + return nil, "Unexpected response status code" + end +end + +return { + new = new_pet_http_api; +} diff --git a/samples/client/petstore/lua/petstore_client/pet.lua b/samples/client/petstore/lua/petstore_client/pet.lua new file mode 100644 index 000000000..136bd2454 --- /dev/null +++ b/samples/client/petstore/lua/petstore_client/pet.lua @@ -0,0 +1,23 @@ +-- Pet class +local pet = {} +local pet_mt = { + __name = "pet"; + __index = pet; +} + +local function cast_pet(t) + return setmetatable(t, pet_mt) +end + +local function new_pet(name) + return cast_pet({ + name = name; + photoUrls = {}; + tags = {}; + }) +end + +return { + cast = cast_pet; + new = new_pet; +}