浏览代码

Refactoring some api handlers to use the new Response return object

Torkel Ödegaard 10 年之前
父节点
当前提交
f81bde5643
共有 3 个文件被更改,包括 17 次插入19 次删除
  1. 3 3
      pkg/api/api.go
  2. 12 16
      pkg/api/apikey.go
  3. 2 0
      public/app/features/org/orgApiKeysCtrl.js

+ 3 - 3
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

+ 12 - 16
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)
 }

+ 2 - 0
public/app/features/org/orgApiKeysCtrl.js

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