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 👍

* use ctx.InternalServerError

* Revert "use numbers and not http.Status___ enum"

This reverts commit b1ff386e24.

* use http.Status* enum everywhere
This commit is contained in:
6543 2019-12-20 18:07:12 +01:00 committed by Lauris BH
parent 050a8af424
commit 2848c5eb8f
52 changed files with 1262 additions and 648 deletions

View file

@ -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)
}