Refactoring some api handlers to use the new Response return object

This commit is contained in:
Torkel Ödegaard 2015-05-18 21:23:40 +02:00
parent fbc6bb2112
commit f81bde5643
3 changed files with 17 additions and 19 deletions

View File

@ -84,9 +84,9 @@ func Register(r *macaron.Macaron) {
// auth api keys
r.Group("/auth/keys", func() {
r.Get("/", GetApiKeys)
r.Post("/", bind(m.AddApiKeyCommand{}), AddApiKey)
r.Delete("/:id", DeleteApiKey)
r.Get("/", wrap(GetApiKeys))
r.Post("/", bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
r.Delete("/:id", wrap(DeleteApiKey))
}, reqAccountAdmin)
// Data sources

View File

@ -8,12 +8,11 @@ import (
m "github.com/grafana/grafana/pkg/models"
)
func GetApiKeys(c *middleware.Context) {
func GetApiKeys(c *middleware.Context) Response {
query := m.GetApiKeysQuery{OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
c.JsonApiErr(500, "Failed to list api keys", err)
return
return ApiError(500, "Failed to list api keys", err)
}
result := make([]*m.ApiKeyDTO, len(query.Result))
@ -24,27 +23,26 @@ func GetApiKeys(c *middleware.Context) {
Role: t.Role,
}
}
c.JSON(200, result)
return Json(200, result)
}
func DeleteApiKey(c *middleware.Context) {
func DeleteApiKey(c *middleware.Context) Response {
id := c.ParamsInt64(":id")
cmd := &m.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId}
err := bus.Dispatch(cmd)
if err != nil {
c.JsonApiErr(500, "Failed to delete API key", err)
return
return ApiError(500, "Failed to delete API key", err)
}
c.JsonOK("API key deleted")
return ApiSuccess("API key deleted")
}
func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) Response {
if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
return
return ApiError(400, "Invalid role specified", nil)
}
cmd.OrgId = c.OrgId
@ -53,14 +51,12 @@ func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
cmd.Key = newKeyInfo.HashedKey
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to add API key", err)
return
return ApiError(500, "Failed to add API key", err)
}
result := &dtos.NewApiKeyResult{
Name: cmd.Result.Name,
Key: newKeyInfo.ClientSecret,
}
Key: newKeyInfo.ClientSecret}
c.JSON(200, result)
return Json(200, result)
}

View File

@ -35,6 +35,8 @@ function (angular) {
src: './app/features/org/partials/apikeyModal.html',
scope: modalScope
});
$scope.getTokens();
});
};