--[[
GameBrain API
GameBrain API
The version of the OpenAPI document: 1.0.1
Contact: mail@gamebrain.co
Generated by: https://openapi-generator.tech
]]
--package gamebrain
local http_request = require "http.request"
local http_util = require "http.util"
local dkjson = require "dkjson"
local basexx = require "basexx"
-- model import
local gamebrain_game_news_response = require "gamebrain.model.game_news_response"
local gamebrain_game_response = require "gamebrain.model.game_response"
local gamebrain_search_response = require "gamebrain.model.search_response"
local gamebrain_search_suggestion_response = require "gamebrain.model.search_suggestion_response"
local gamebrain_similar_games_response = require "gamebrain.model.similar_games_response"
local default_api = {}
local default_api_mt = {
__name = "default_api";
__index = default_api;
}
local function new_default_api(authority, 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
local host, port = http_util.split_authority(authority, default_scheme)
return setmetatable({
host = host;
port = port;
basePath = basePath or "https://api.gamebrain.co/v1";
schemes = schemes_map;
default_scheme = default_scheme;
http_username = nil;
http_password = nil;
api_key = {};
access_token = nil;
}, default_api_mt)
end
function default_api:detail(id, api_key)
local req = http_request.new_from_uri({
scheme = self.default_scheme;
host = self.host;
port = self.port;
path = string.format("%s/games/%s?api-key=%s",
self.basePath, id, http_util.encodeURIComponent(api_key));
})
-- set HTTP verb
req.headers:upsert(":method", "GET")
-- TODO: create a function to select proper content-type
--local var_accept = { "application/json" }
req.headers:upsert("content-type", "application/json")
-- TODO: api key in query 'api-key'
-- api key in headers 'x-api-key'
if self.api_key['x-api-key'] then
req.headers:upsert("headerApiKey", self.api_key['x-api-key'])
end
-- 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 gamebrain_game_response.cast(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 default_api:news(id, offset, limit, api_key)
local req = http_request.new_from_uri({
scheme = self.default_scheme;
host = self.host;
port = self.port;
path = string.format("%s/games/%s/news?offset=%s&limit=%s&api-key=%s",
self.basePath, id, http_util.encodeURIComponent(offset), http_util.encodeURIComponent(limit), http_util.encodeURIComponent(api_key));
})
-- set HTTP verb
req.headers:upsert(":method", "GET")
-- TODO: create a function to select proper content-type
--local var_accept = { "application/json" }
req.headers:upsert("content-type", "application/json")
-- TODO: api key in query 'api-key'
-- api key in headers 'x-api-key'
if self.api_key['x-api-key'] then
req.headers:upsert("headerApiKey", self.api_key['x-api-key'])
end
-- 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 gamebrain_game_news_response.cast(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 default_api:search(query, offset, limit, filters, sort, sort_order, generate_filter_options, api_key)
local req = http_request.new_from_uri({
scheme = self.default_scheme;
host = self.host;
port = self.port;
path = string.format("%s/games?query=%s&offset=%s&limit=%s&filters=%s&sort=%s&sort-order=%s&generate-filter-options=%s&api-key=%s",
self.basePath, http_util.encodeURIComponent(query), http_util.encodeURIComponent(offset), http_util.encodeURIComponent(limit), http_util.encodeURIComponent(filters), http_util.encodeURIComponent(sort), http_util.encodeURIComponent(sort_order), http_util.encodeURIComponent(generate_filter_options), http_util.encodeURIComponent(api_key));
})
-- set HTTP verb
req.headers:upsert(":method", "GET")
-- TODO: create a function to select proper content-type
--local var_accept = { "application/json" }
req.headers:upsert("content-type", "application/json")
-- TODO: api key in query 'api-key'
-- api key in headers 'x-api-key'
if self.api_key['x-api-key'] then
req.headers:upsert("headerApiKey", self.api_key['x-api-key'])
end
-- 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 gamebrain_search_response.cast(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 default_api:similar(id, limit, api_key)
local req = http_request.new_from_uri({
scheme = self.default_scheme;
host = self.host;
port = self.port;
path = string.format("%s/games/%s/similar?limit=%s&api-key=%s",
self.basePath, id, http_util.encodeURIComponent(limit), http_util.encodeURIComponent(api_key));
})
-- set HTTP verb
req.headers:upsert(":method", "GET")
-- TODO: create a function to select proper content-type
--local var_accept = { "application/json" }
req.headers:upsert("content-type", "application/json")
-- TODO: api key in query 'api-key'
-- api key in headers 'x-api-key'
if self.api_key['x-api-key'] then
req.headers:upsert("headerApiKey", self.api_key['x-api-key'])
end
-- 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 gamebrain_similar_games_response.cast(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 default_api:suggest(query, limit, api_key)
local req = http_request.new_from_uri({
scheme = self.default_scheme;
host = self.host;
port = self.port;
path = string.format("%s/games/suggestions?query=%s&limit=%s&api-key=%s",
self.basePath, http_util.encodeURIComponent(query), http_util.encodeURIComponent(limit), http_util.encodeURIComponent(api_key));
})
-- set HTTP verb
req.headers:upsert(":method", "GET")
-- TODO: create a function to select proper content-type
--local var_accept = { "application/json" }
req.headers:upsert("content-type", "application/json")
-- TODO: api key in query 'api-key'
-- api key in headers 'x-api-key'
if self.api_key['x-api-key'] then
req.headers:upsert("headerApiKey", self.api_key['x-api-key'])
end
-- 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 gamebrain_search_suggestion_response.cast(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
return {
new = new_default_api;
}