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 // auth api keys
r.Group("/auth/keys", func() { r.Group("/auth/keys", func() {
r.Get("/", GetApiKeys) r.Get("/", wrap(GetApiKeys))
r.Post("/", bind(m.AddApiKeyCommand{}), AddApiKey) r.Post("/", bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
r.Delete("/:id", DeleteApiKey) r.Delete("/:id", wrap(DeleteApiKey))
}, reqAccountAdmin) }, reqAccountAdmin)
// Data sources // Data sources

View File

@ -8,12 +8,11 @@ import (
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
) )
func GetApiKeys(c *middleware.Context) { func GetApiKeys(c *middleware.Context) Response {
query := m.GetApiKeysQuery{OrgId: c.OrgId} query := m.GetApiKeysQuery{OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil { if err := bus.Dispatch(&query); err != nil {
c.JsonApiErr(500, "Failed to list api keys", err) return ApiError(500, "Failed to list api keys", err)
return
} }
result := make([]*m.ApiKeyDTO, len(query.Result)) result := make([]*m.ApiKeyDTO, len(query.Result))
@ -24,27 +23,26 @@ func GetApiKeys(c *middleware.Context) {
Role: t.Role, 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") id := c.ParamsInt64(":id")
cmd := &m.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId} cmd := &m.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId}
err := bus.Dispatch(cmd) err := bus.Dispatch(cmd)
if err != nil { if err != nil {
c.JsonApiErr(500, "Failed to delete API key", err) return ApiError(500, "Failed to delete API key", err)
return
} }
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() { if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil) return ApiError(400, "Invalid role specified", nil)
return
} }
cmd.OrgId = c.OrgId cmd.OrgId = c.OrgId
@ -53,14 +51,12 @@ func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
cmd.Key = newKeyInfo.HashedKey cmd.Key = newKeyInfo.HashedKey
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to add API key", err) return ApiError(500, "Failed to add API key", err)
return
} }
result := &dtos.NewApiKeyResult{ result := &dtos.NewApiKeyResult{
Name: cmd.Result.Name, 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', src: './app/features/org/partials/apikeyModal.html',
scope: modalScope scope: modalScope
}); });
$scope.getTokens();
}); });
}; };