Déplacer déclarations de route à pave pour API spec autogénérée #176

Merged
vlbeaudoin merged 17 commits from feature/pave into main 2023-11-20 17:05:41 -05:00
4 changed files with 83 additions and 16 deletions
Showing only changes of commit 86f1284e12 - Show all commits

View file

@ -141,12 +141,15 @@ func (h *V1Handler) V1DocumentPUT(c echo.Context) error {
// V1DocumentDELETE permet de supprimer un object // V1DocumentDELETE permet de supprimer un object
func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { func (h *V1Handler) V1DocumentDELETE(c echo.Context) error {
bucket := c.Param("bucket") var request apirequest.V1DocumentDELETE
document := c.Param("document") var response apiresponse.V1DocumentDELETE
request.Params.Bucket = c.Param("bucket")
request.Params.Document = c.Param("document")
allowed := false allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets { for bucket_allowed := range h.Config.Server.Documents.Buckets {
if bucket == bucket_allowed { if request.Params.Bucket == bucket_allowed {
allowed = true allowed = true
} }
} }
@ -155,11 +158,18 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error {
return c.JSON(apiresponse.NotFoundResponse()) return c.JSON(apiresponse.NotFoundResponse())
} }
if !request.Complete() {
response.Message = "Incomplete V1DocumentDELETE request received"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket) bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
} }
@ -168,29 +178,34 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error {
return c.JSON(apiresponse.NotFoundResponse()) return c.JSON(apiresponse.NotFoundResponse())
} }
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) document_info, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{})
if err != nil { if err != nil {
if err.Error() == "The specified key does not exist." { if err.Error() == "The specified key does not exist." {
return c.JSON(apiresponse.NotFoundResponse()) return c.JSON(apiresponse.NotFoundResponse())
} }
return c.JSON(http.StatusInternalServerError, map[string]interface{}{ //response.Error = err.Error()
"message": "Error during minio#StatObject", response.Message = "Error during minio#StatObject"
}) response.StatusCode = http.StatusInternalServerError
return c.JSON(response.StatusCode, response)
} }
//TODO Add error validation //TODO Add error validation
_ = document_info _ = document_info
err = h.MediaClient.MinioClient.RemoveObject(ctx, bucket, document, minio.RemoveObjectOptions{}) err = h.MediaClient.MinioClient.RemoveObject(ctx, request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{})
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ //response.Error = err.Error()
"message": "Error during minio#RemoveObject", response.Message = "Error during minio#RemoveObject"
}) response.StatusCode = http.StatusInternalServerError
return c.JSON(response.StatusCode, response)
} }
return c.JSON(http.StatusOK, map[string]string{ response.Message = "Document deleted"
"message": "Document deleted", response.StatusCode = http.StatusOK
})
return c.JSON(response.StatusCode, response)
} }

View file

@ -56,3 +56,43 @@ func (request V1DocumentPOST) Request(v *voki.Voki) (response apiresponse.V1Docu
return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s", request.Data.Bucket), &buf, true, &response) return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s", request.Data.Bucket), &buf, true, &response)
} }
var _ request.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{}
type V1DocumentDELETE struct {
Params struct {
Bucket string
Document string
}
}
func NewV1DocumentDELETE(bucket, document string) (request V1DocumentDELETE, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentDELETE requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
if document == "" {
err = fmt.Errorf("NewV1DocumentDELETE requires non-nil document name")
return
}
request.Params.Document = document
return
}
func (request V1DocumentDELETE) Complete() bool {
return request.Params.Bucket != "" && request.Params.Document != ""
}
func (request V1DocumentDELETE) Request(v *voki.Voki) (response apiresponse.V1DocumentDELETE, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1DocumentDELETE request")
return
}
return response, v.UnmarshalIfComplete(http.MethodDelete, fmt.Sprintf("/v1/bucket/%s/%s", request.Params.Bucket, request.Params.Document), nil, true, &response)
}

View file

@ -8,3 +8,11 @@ type V1DocumentPOST struct {
Size int64 Size int64
} }
} }
type V1DocumentDELETE struct {
Response
Data struct {
Bucket string
Document string
}
}

View file

@ -252,7 +252,11 @@ func RunServer() {
groupV1.PUT("/bucket/:bucket/:document", v1Handler.V1DocumentPUT) groupV1.PUT("/bucket/:bucket/:document", v1Handler.V1DocumentPUT)
groupV1.DELETE("/bucket/:bucket/:document", v1Handler.V1DocumentDELETE) if err := pave.EchoRegister[
apirequest.V1DocumentDELETE,
apiresponse.V1DocumentDELETE](groupV1, &p, "/v1", http.MethodDelete, "/bucket/:bucket/:document", "Delete document in specified bucket", "V1DocumentDELEte", v1Handler.V1DocumentDELETE); err != nil {
log.Fatal(err)
}
// HTML Routes // HTML Routes
client := http.DefaultClient client := http.DefaultClient