-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from Mashape/feature/restful-api
[feature] RESTful Admin API
- Loading branch information
Showing
28 changed files
with
1,611 additions
and
335 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
database/migrations/cassandra/2015-05-22-235608_plugins_fix.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
local Migration = { | ||
name = "2015-05-22-235608_plugins_fix", | ||
|
||
up = function(options) | ||
return [[ | ||
CREATE INDEX IF NOT EXISTS ON keyauth_credentials(consumer_id); | ||
CREATE INDEX IF NOT EXISTS ON basicauth_credentials(consumer_id); | ||
]] | ||
end, | ||
|
||
down = function(options) | ||
return [[ | ||
DROP INDEX keyauth_credentials_consumer_id_idx; | ||
DROP INDEX basicauth_credentials_consumer_id_idx; | ||
]] | ||
end | ||
} | ||
|
||
return Migration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
local responses = require "kong.tools.responses" | ||
local validations = require "kong.dao.schemas_validation" | ||
local app_helpers = require "lapis.application" | ||
|
||
local _M = {} | ||
|
||
function _M.find_api_by_name_or_id(self, dao_factory, helpers) | ||
local fetch_keys = { | ||
[validations.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id | ||
} | ||
self.params.name_or_id = nil | ||
|
||
-- TODO: make the base_dao more flexible so we can query find_one with key/values | ||
-- https://github.com/Mashape/kong/issues/103 | ||
local data, err = dao_factory.apis:find_by_keys(fetch_keys) | ||
if err then | ||
return helpers.yield_error(err) | ||
end | ||
|
||
self.api = data[1] | ||
if not self.api then | ||
return helpers.responses.send_HTTP_NOT_FOUND() | ||
end | ||
end | ||
|
||
function _M.find_consumer_by_username_or_id(self, dao_factory, helpers) | ||
local fetch_keys = { | ||
[validations.is_valid_uuid(self.params.username_or_id) and "id" or "username"] = self.params.username_or_id | ||
} | ||
self.params.username_or_id = nil | ||
|
||
local data, err = dao_factory.consumers:find_by_keys(fetch_keys) | ||
if err then | ||
return helpers.yield_error(err) | ||
end | ||
|
||
self.consumer = data[1] | ||
if not self.consumer then | ||
return helpers.responses.send_HTTP_NOT_FOUND() | ||
end | ||
end | ||
|
||
function _M.paginated_set(self, dao_collection) | ||
local size = self.params.size and tonumber(self.params.size) or 100 | ||
local offset = self.params.offset and ngx.decode_base64(self.params.offset) or nil | ||
|
||
self.params.size = nil | ||
self.params.offset = nil | ||
|
||
local data, err = dao_collection:find_by_keys(self.params, size, offset) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
end | ||
|
||
local next_url | ||
if data.next_page then | ||
next_url = self:build_url(self.req.parsed_url.path, { | ||
port = self.req.parsed_url.port, | ||
query = ngx.encode_args({ | ||
offset = ngx.encode_base64(data.next_page), | ||
size = size | ||
}) | ||
}) | ||
data.next_page = nil | ||
end | ||
|
||
-- This check is required otherwise the response is going to be a | ||
-- JSON Object and not a JSON array. The reason is because an empty Lua array `{}` | ||
-- will not be translated as an empty array by cjson, but as an empty object. | ||
local result = #data == 0 and "{\"data\":[]}" or {data=data, ["next"]=next_url} | ||
|
||
return responses.send_HTTP_OK(result, type(result) ~= "table") | ||
end | ||
|
||
function _M.put(params, dao_collection) | ||
local new_entity, err | ||
if params.id then | ||
new_entity, err = dao_collection:update(params) | ||
if not err and new_entity then | ||
return responses.send_HTTP_OK(new_entity) | ||
elseif not new_entity then | ||
return responses.send_HTTP_NOT_FOUND() | ||
end | ||
else | ||
new_entity, err = dao_collection:insert(params) | ||
if not err then | ||
return responses.send_HTTP_CREATED(new_entity) | ||
end | ||
end | ||
|
||
if err then | ||
return app_helpers.yield_error(err) | ||
end | ||
end | ||
|
||
function _M.post(params, dao_collection) | ||
local data, err = dao_collection:insert(params) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_CREATED(data) | ||
end | ||
end | ||
|
||
function _M.patch(params, dao_collection) | ||
local new_entity, err = dao_collection:update(params) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_OK(new_entity) | ||
end | ||
end | ||
|
||
function _M.delete(entity_id, dao_collection) | ||
local ok, err = dao_collection:delete(entity_id) | ||
if not ok then | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_NOT_FOUND() | ||
end | ||
else | ||
return responses.send_HTTP_NO_CONTENT() | ||
end | ||
end | ||
|
||
return _M |
Oops, something went wrong.