From f81bde5643722ebb3e8290e8208347d288f4d935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 18 May 2015 21:23:40 +0200 Subject: [PATCH] Refactoring some api handlers to use the new Response return object --- pkg/api/api.go | 6 ++--- pkg/api/apikey.go | 28 ++++++++++------------- public/app/features/org/orgApiKeysCtrl.js | 2 ++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index a84c753c7ba..0503ac61a3c 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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 diff --git a/pkg/api/apikey.go b/pkg/api/apikey.go index 237fdc48ab0..b2097104aba 100644 --- a/pkg/api/apikey.go +++ b/pkg/api/apikey.go @@ -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) } diff --git a/public/app/features/org/orgApiKeysCtrl.js b/public/app/features/org/orgApiKeysCtrl.js index a8b05155401..918f57b42e8 100644 --- a/public/app/features/org/orgApiKeysCtrl.js +++ b/public/app/features/org/orgApiKeysCtrl.js @@ -35,6 +35,8 @@ function (angular) { src: './app/features/org/partials/apikeyModal.html', scope: modalScope }); + + $scope.getTokens(); }); };