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"
@ -14,7 +16,7 @@ import (
func listGPGKeys(ctx *context.APIContext, uid int64) {
keys, err := models.ListGPGKeys(uid)
if err != nil {
ctx.Error(500, "ListGPGKeys", err)
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
}
@ -23,7 +25,7 @@ func listGPGKeys(ctx *context.APIContext, uid int64) {
apiKeys[i] = convert.ToGPGKey(keys[i])
}
ctx.JSON(200, &apiKeys)
ctx.JSON(http.StatusOK, &apiKeys)
}
//ListGPGKeys get the GPG key list of a user
@ -42,6 +44,7 @@ func ListGPGKeys(ctx *context.APIContext) {
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
user := GetUserByParams(ctx)
if ctx.Written() {
return
@ -59,6 +62,7 @@ func ListMyGPGKeys(ctx *context.APIContext) {
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
listGPGKeys(ctx, ctx.User.ID)
}
@ -81,16 +85,17 @@ func GetGPGKey(ctx *context.APIContext) {
// "$ref": "#/responses/GPGKey"
// "404":
// "$ref": "#/responses/notFound"
key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
if err != nil {
if models.IsErrGPGKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetGPGKeyByID", err)
ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
}
return
}
ctx.JSON(200, convert.ToGPGKey(key))
ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
}
// CreateUserGPGKey creates new GPG key to given user by ID.
@ -100,7 +105,7 @@ func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid
HandleAddGPGKeyError(ctx, err)
return
}
ctx.JSON(201, convert.ToGPGKey(key))
ctx.JSON(http.StatusCreated, convert.ToGPGKey(key))
}
// swagger:parameters userCurrentPostGPGKey
@ -123,6 +128,7 @@ func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
// "$ref": "#/responses/GPGKey"
// "422":
// "$ref": "#/responses/validationError"
CreateUserGPGKey(ctx, form, ctx.User.ID)
}
@ -145,26 +151,27 @@ func DeleteGPGKey(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"
if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
if models.IsErrGPGKeyAccessDenied(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, "DeleteGPGKey", err)
ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
}
return
}
ctx.Status(204)
ctx.Status(http.StatusNoContent)
}
// HandleAddGPGKeyError handle add GPGKey error
func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
switch {
case models.IsErrGPGKeyAccessDenied(err):
ctx.Error(422, "", "You do not have access to this GPG key")
ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key")
case models.IsErrGPGKeyIDAlreadyUsed(err):
ctx.Error(422, "", "A key with the same id already exists")
ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists")
default:
ctx.Error(500, "AddGPGKey", err)
ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
}
}