diff options
author | 6543 <6543@obermui.de> | 2019-12-20 18:07:12 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-12-20 19:07:12 +0200 |
commit | 2848c5eb8f7333b6791afd296b12d21751d0516b (patch) | |
tree | 67ff6244026174116edbff1b4c4cdb5934401968 /routers/api/v1/user/key.go | |
parent | 050a8af4243d7f5fff0a2f492b9166f4dfdf0ecf (diff) | |
download | gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.tar.gz gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.zip |
Swagger info corrections (#9441)
* use numbers and not http.Status___ enum
* fix test
* add many missing swagger responses
* code format
* Deletion Sould return 204 ...
* error handling improvements
* if special error type ... then add it to swagger too
* one smal nit
* invalidTopicsError is []string
* valid swagger specification 2.0
- if you add responses swagger can tell you if you do it right :+1:
* use ctx.InternalServerError
* Revert "use numbers and not http.Status___ enum"
This reverts commit b1ff386e2418ed6a7f183e756b13277d701278ef.
* use http.Status* enum everywhere
Diffstat (limited to 'routers/api/v1/user/key.go')
-rw-r--r-- | routers/api/v1/user/key.go | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index a812edfcc7..7cf6fa383d 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -43,7 +45,7 @@ func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { if models.IsErrUserNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetUserByName", err) + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } return nil } @@ -81,7 +83,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { } if err != nil { - ctx.Error(500, "ListPublicKeys", err) + ctx.Error(http.StatusInternalServerError, "ListPublicKeys", err) return } @@ -94,7 +96,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { } } - ctx.JSON(200, &apiKeys) + ctx.JSON(http.StatusOK, &apiKeys) } // ListMyPublicKeys list all of the authenticated user's public keys @@ -112,6 +114,7 @@ func ListMyPublicKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/PublicKeyList" + listPublicKeys(ctx, ctx.User) } @@ -135,6 +138,7 @@ func ListPublicKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/PublicKeyList" + user := GetUserByParams(ctx) if ctx.Written() { return @@ -161,12 +165,13 @@ func GetPublicKey(ctx *context.APIContext) { // "$ref": "#/responses/PublicKey" // "404": // "$ref": "#/responses/notFound" + key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrKeyNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetPublicKeyByID", err) + ctx.Error(http.StatusInternalServerError, "GetPublicKeyByID", err) } return } @@ -176,7 +181,7 @@ func GetPublicKey(ctx *context.APIContext) { if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID { apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User) } - ctx.JSON(200, apiKey) + ctx.JSON(http.StatusOK, apiKey) } // CreateUserPublicKey creates new public key to given user by ID. @@ -197,7 +202,7 @@ func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID { apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User) } - ctx.JSON(201, apiKey) + ctx.JSON(http.StatusCreated, apiKey) } // CreatePublicKey create one public key for me @@ -219,6 +224,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { // "$ref": "#/responses/PublicKey" // "422": // "$ref": "#/responses/validationError" + CreateUserPublicKey(ctx, form, ctx.User.ID) } @@ -243,16 +249,17 @@ func DeletePublicKey(ctx *context.APIContext) { // "$ref": "#/responses/forbidden" // "404": // "$ref": "#/responses/notFound" + if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { if models.IsErrKeyNotExist(err) { ctx.NotFound() } else if models.IsErrKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") + ctx.Error(http.StatusForbidden, "", "You do not have access to this key") } else { - ctx.Error(500, "DeletePublicKey", err) + ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err) } return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } |