From 7bc4eed8ae11e69312254c624741f593f17dee00 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 21 Aug 2024 13:39:03 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20update=20voki=20requests/responses=20?= =?UTF-8?q?=C3=A0=20v3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api.go | 2 +- apihandler/apihandler.go | 2 +- apihandler/bucket.go | 36 ++++++----- apihandler/document.go | 128 ++++++++++++++++++++----------------- apihandler/seed.go | 8 +-- apihandler/spec.go | 8 +-- apirequest/bucket.go | 7 +- apirequest/document.go | 11 ++-- apirequest/seed.go | 5 +- apirequest/spec.go | 5 +- apiresponse/apiresponse.go | 36 +++-------- apiresponse/bucket.go | 4 +- apiresponse/document.go | 8 +-- apiresponse/seed.go | 2 +- apiresponse/spec.go | 2 +- cmd/server.go | 2 +- go.mod | 5 +- go.sum | 10 ++- webhandler/webhandler.go | 55 ++++++++-------- webresponse/webresponse.go | 8 +-- 20 files changed, 169 insertions(+), 175 deletions(-) diff --git a/api/api.go b/api/api.go index 06bda1a..e89e5ac 100644 --- a/api/api.go +++ b/api/api.go @@ -9,7 +9,7 @@ import ( "net/http" "net/url" - "codeberg.org/vlbeaudoin/voki" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/config" diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index 0dac7db..4d0c96b 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -4,7 +4,7 @@ import ( "net/http" "sort" - "codeberg.org/vlbeaudoin/pave" + "codeberg.org/vlbeaudoin/pave/v2" "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" "github.com/labstack/echo/v4" diff --git a/apihandler/bucket.go b/apihandler/bucket.go index 04818f6..3cb37ee 100644 --- a/apihandler/bucket.go +++ b/apihandler/bucket.go @@ -4,6 +4,7 @@ import ( "context" "net/http" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" "github.com/labstack/echo/v4" @@ -17,9 +18,9 @@ func (h *V1Handler) V1BucketsGET(c echo.Context) error { if !request.Complete() { response.Message = "Incomplete V1BucketsGET request received" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } var buckets = make(map[string]string) @@ -27,11 +28,10 @@ func (h *V1Handler) V1BucketsGET(c echo.Context) error { for bucket_name, bucket_display_name := range h.Config.Server.Documents.Buckets { exists, err := h.MediaClient.MinioClient.BucketExists(context.Background(), bucket_name) if err != nil { - response.StatusCode = http.StatusInternalServerError response.Message = "Error during minio#BucketExists" - // response.Error = err.Error() + response.SetStatusCode(http.StatusInternalServerError) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if exists { @@ -39,11 +39,11 @@ func (h *V1Handler) V1BucketsGET(c echo.Context) error { } } - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) response.Message = "Buckets list successful" response.Data.Buckets = buckets - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } func (h *V1Handler) V1BucketGET(c echo.Context) error { @@ -54,9 +54,9 @@ func (h *V1Handler) V1BucketGET(c echo.Context) error { if !request.Complete() { response.Message = "Incomplete V1BucketGET request received" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } allowed := false @@ -67,7 +67,8 @@ func (h *V1Handler) V1BucketGET(c echo.Context) error { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) @@ -76,33 +77,34 @@ func (h *V1Handler) V1BucketGET(c echo.Context) error { exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket) if err != nil { - response.StatusCode = http.StatusInternalServerError response.Message = "Error during minio#BucketExists" + response.SetStatusCode(http.StatusInternalServerError) response.Error = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if !exists { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } objectCh := h.MediaClient.MinioClient.ListObjects(ctx, request.Params.Bucket, minio.ListObjectsOptions{}) for object := range objectCh { if object.Err != nil { - response.StatusCode = http.StatusInternalServerError response.Message = "Error during minio#ListObjects" + response.SetStatusCode(http.StatusInternalServerError) //TODO make sure this is safe //response.Error = object.Err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } response.Data.Keys = append(response.Data.Keys, object.Key) } - response.StatusCode = http.StatusOK response.Message = "V1BucketRead ok" + response.SetStatusCode(http.StatusOK) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } diff --git a/apihandler/document.go b/apihandler/document.go index b7fecd0..2d0a27c 100644 --- a/apihandler/document.go +++ b/apihandler/document.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" "github.com/labstack/echo/v4" @@ -34,28 +35,29 @@ func (h *V1Handler) V1DocumentsPOST(c echo.Context) (err error) { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } form, err := c.MultipartForm() if err != nil { response.Message = fmt.Sprintf("Téléversement invalide: %s", err) - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if form == nil { response.Message = "MultipartForm pointe vers une addresse mémoire nil" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if len(form.File) == 0 { response.Message = "Veuillez sélectionner au moins 1 document à téléverser" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } for inputName, inputFileHeaders := range form.File { @@ -66,21 +68,23 @@ func (h *V1Handler) V1DocumentsPOST(c echo.Context) (err error) { if request.Data.Documents == nil { response.Message = "Impossible d'obtenir les documents depuis le formulaire" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if !request.Complete() { response.Message = "Requête V1DocumentsPOST incomplète reçue" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } - response.StatusCode, response.Message = h.MediaClient.UploadFormFiles(request.Params.Bucket, request.Data.Documents) + var code int + code, response.Message = h.MediaClient.UploadFormFiles(request.Params.Bucket, request.Data.Documents) + response.SetStatusCode(code) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // V1DocumentPOST permet d'ajouter un object dans un bucket, par multipart/form-data @@ -92,11 +96,11 @@ func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) { request.Data.Document, err = c.FormFile("document") if err != nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile" response.Error = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } allowed := false @@ -107,14 +111,15 @@ func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } if !request.Complete() { response.Message = "Incomplete V1DocumentPOST request received" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) @@ -123,11 +128,11 @@ func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) { src, err := request.Data.Document.Open() if err != nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Error during form_file.Open()" response.Error = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } defer src.Close() @@ -135,20 +140,20 @@ func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) { ContentType: request.Data.Document.Header.Get("Content-Type"), }) if err != nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Error during minio#PutObject" //response.Error = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) response.Message = "ok" response.Data.Bucket = info.Bucket response.Data.Key = info.Key response.Data.Size = info.Size - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // V1DocumentGET permet de lire le contenu d'un fichier et protentiellement de le télécharger @@ -164,7 +169,8 @@ func (h *V1Handler) V1DocumentGET(c echo.Context) error { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) @@ -177,7 +183,8 @@ func (h *V1Handler) V1DocumentGET(c echo.Context) error { } if !bucket_exists { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) @@ -185,7 +192,8 @@ func (h *V1Handler) V1DocumentGET(c echo.Context) error { if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -223,14 +231,15 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } if !request.Complete() { response.Message = "Incomplete V1DocumentDELETE request received" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) @@ -243,21 +252,23 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { } if !bucket_exists { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } //response.Error = err.Error() response.Message = "Error during minio#StatObject" - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } //TODO Add error validation @@ -267,15 +278,15 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { if err != nil { //response.Error = err.Error() response.Message = "Error during minio#RemoveObject" - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } response.Message = "Document deleted" - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // V1DocumentKeyPUT @@ -289,25 +300,25 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) { var newKey string err = c.Bind(&newKey) if err != nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } request, err = apirequest.NewV1DocumentKeyPUT(bucket, document, newKey) if err != nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if !request.Complete() { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Incomplete V1DocumentKeyPUT request received" - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } var allowed bool @@ -318,7 +329,8 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) { } if !allowed { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) @@ -330,7 +342,8 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) { } if !bucketExists { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } // Check source object exists @@ -338,13 +351,14 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) { if _, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{}); err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } response.Message = fmt.Sprintf("Could not obtain information on %s/%s", request.Params.Bucket, request.Params.Document) - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // Copy object to newKey @@ -353,34 +367,34 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) { minio.CopyDestOptions{Bucket: request.Params.Bucket, Object: request.Data.NewKey}, minio.CopySrcOptions{Bucket: request.Params.Bucket, Object: request.Params.Document}, ); err != nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Impossible de copier un document pour le renommer" - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // Verify copy was successful if _, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Data.NewKey, minio.GetObjectOptions{}); err != nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Copie de document ne semble pas avoir fonctionnée" - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } // Delete old file if err := h.MediaClient.MinioClient.RemoveObject(ctx, request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{}); err != nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Erreur pendant tentative de supprimer document source" - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } //TODO cleanup // Return result - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) response.Message = "Document renommé avec succès" response.Data.Bucket = request.Params.Bucket response.Data.Key = request.Data.NewKey - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } diff --git a/apihandler/seed.go b/apihandler/seed.go index 77f93c6..9462eca 100644 --- a/apihandler/seed.go +++ b/apihandler/seed.go @@ -15,11 +15,11 @@ func (h *V1Handler) V1SeedPOST(c echo.Context) error { new_buckets, err := h.MediaClient.Seed() response.Data.Buckets = new_buckets if err != nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Error during mediaClient.Seed()" response.Error = err.Error() - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } if len(new_buckets) == 0 { @@ -29,7 +29,7 @@ func (h *V1Handler) V1SeedPOST(c echo.Context) error { response.Message = "Buckets successfully created" } - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } diff --git a/apihandler/spec.go b/apihandler/spec.go index 20985fb..afc69c4 100644 --- a/apihandler/spec.go +++ b/apihandler/spec.go @@ -18,9 +18,9 @@ func (h *V1Handler) V1SpecGET(c echo.Context) error { if !request.Complete() { response.Message = "Incomplete V1SpecGET request received" - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } response.Data.Spec = fmt.Sprintf("# pave spec for agecem-org %s\n", version.Version()) @@ -30,7 +30,7 @@ func (h *V1Handler) V1SpecGET(c echo.Context) error { } response.Message = "ok" - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) - return c.JSON(response.StatusCode, response) + return c.JSON(response.StatusCode(), response) } diff --git a/apirequest/bucket.go b/apirequest/bucket.go index e4ba60a..d47c5cc 100644 --- a/apirequest/bucket.go +++ b/apirequest/bucket.go @@ -4,12 +4,11 @@ import ( "fmt" "net/http" - "codeberg.org/vlbeaudoin/voki" - "codeberg.org/vlbeaudoin/voki/request" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apiresponse" ) -var _ request.Requester[apiresponse.V1BucketsGET] = V1BucketsGET{} +var _ voki.Requester[apiresponse.V1BucketsGET] = V1BucketsGET{} type V1BucketsGET struct{} @@ -28,7 +27,7 @@ func (request V1BucketsGET) Request(v *voki.Voki) (response apiresponse.V1Bucket return response, v.UnmarshalIfComplete(http.MethodGet, "/v1/bucket", nil, true, &response) } -var _ request.Requester[apiresponse.V1BucketGET] = V1BucketGET{} +var _ voki.Requester[apiresponse.V1BucketGET] = V1BucketGET{} type V1BucketGET struct { Params struct { diff --git a/apirequest/document.go b/apirequest/document.go index 3953a6d..f69c413 100644 --- a/apirequest/document.go +++ b/apirequest/document.go @@ -7,12 +7,11 @@ import ( "mime/multipart" "net/http" - "codeberg.org/vlbeaudoin/voki" - "codeberg.org/vlbeaudoin/voki/request" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apiresponse" ) -var _ request.Requester[apiresponse.V1DocumentsPOST] = V1DocumentsPOST{} +var _ voki.Requester[apiresponse.V1DocumentsPOST] = V1DocumentsPOST{} type V1DocumentsPOST struct { Data struct { @@ -75,7 +74,7 @@ func (request V1DocumentsPOST) Request(v *voki.Voki) (response apiresponse.V1Doc return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s/many", request.Params.Bucket), &buf, true, &response) } -var _ request.Requester[apiresponse.V1DocumentPOST] = V1DocumentPOST{} +var _ voki.Requester[apiresponse.V1DocumentPOST] = V1DocumentPOST{} // Deprecated: Use V1DocumentsPOST instead type V1DocumentPOST struct { @@ -124,7 +123,7 @@ func (request V1DocumentPOST) Request(v *voki.Voki) (response apiresponse.V1Docu return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s", request.Params.Bucket), &buf, true, &response) } -var _ request.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{} +var _ voki.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{} type V1DocumentDELETE struct { Params struct { @@ -164,7 +163,7 @@ func (request V1DocumentDELETE) Request(v *voki.Voki) (response apiresponse.V1Do return response, v.UnmarshalIfComplete(http.MethodDelete, fmt.Sprintf("/v1/bucket/%s/%s", request.Params.Bucket, request.Params.Document), nil, true, &response) } -var _ request.Requester[apiresponse.V1DocumentKeyPUT] = V1DocumentKeyPUT{} +var _ voki.Requester[apiresponse.V1DocumentKeyPUT] = V1DocumentKeyPUT{} type V1DocumentKeyPUT struct { Data struct { diff --git a/apirequest/seed.go b/apirequest/seed.go index 89187d7..33ec9d0 100644 --- a/apirequest/seed.go +++ b/apirequest/seed.go @@ -4,12 +4,11 @@ import ( "fmt" "net/http" - "codeberg.org/vlbeaudoin/voki" - "codeberg.org/vlbeaudoin/voki/request" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apiresponse" ) -var _ request.Requester[apiresponse.V1SeedPOST] = V1SeedPOST{} +var _ voki.Requester[apiresponse.V1SeedPOST] = V1SeedPOST{} type V1SeedPOST struct{} diff --git a/apirequest/spec.go b/apirequest/spec.go index b2aaa4e..d468cdd 100644 --- a/apirequest/spec.go +++ b/apirequest/spec.go @@ -4,12 +4,11 @@ import ( "fmt" "net/http" - "codeberg.org/vlbeaudoin/voki" - "codeberg.org/vlbeaudoin/voki/request" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apiresponse" ) -var _ request.Requester[apiresponse.V1SpecGET] = V1SpecGET{} +var _ voki.Requester[apiresponse.V1SpecGET] = V1SpecGET{} type V1SpecGET struct{} diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index 89adb8b..f650728 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -1,37 +1,19 @@ package apiresponse import ( - "net/http" - - "codeberg.org/vlbeaudoin/voki/response" + "codeberg.org/vlbeaudoin/voki/v3" ) -type Response struct { - response.ResponseWithError +type APIResponse struct { + voki.MessageResponse + statusCode int + Error string } -type SimpleResponse struct { - Message string +func (R APIResponse) StatusCode() int { + return R.statusCode } -func (r SimpleResponse) Respond() response.Responder { - return r -} - -func NotFoundResponse() (int, SimpleResponse) { - return http.StatusNotFound, SimpleResponse{ - Message: "Not Found", - } -} - -func NotImplementedResponse() (int, SimpleResponse) { - return http.StatusNotImplemented, SimpleResponse{ - Message: "Not Implemented", - } -} - -func InternalServerErrorResponse() (int, SimpleResponse) { - return http.StatusInternalServerError, SimpleResponse{ - Message: "Internal Server Error", - } +func (R *APIResponse) SetStatusCode(code int) { + R.statusCode = code } diff --git a/apiresponse/bucket.go b/apiresponse/bucket.go index 3f4008f..24313dc 100644 --- a/apiresponse/bucket.go +++ b/apiresponse/bucket.go @@ -1,14 +1,14 @@ package apiresponse type V1BucketsGET struct { - Response + APIResponse Data struct { Buckets map[string]string } } type V1BucketGET struct { - Response + APIResponse Data struct { Keys []string } diff --git a/apiresponse/document.go b/apiresponse/document.go index 488a65e..643e54e 100644 --- a/apiresponse/document.go +++ b/apiresponse/document.go @@ -6,7 +6,7 @@ type DataDocument struct { } type V1DocumentsPOST struct { - Response + APIResponse Data struct { Bucket string Documents []DataDocument @@ -15,7 +15,7 @@ type V1DocumentsPOST struct { // Deprecated: Use V1DocumentsPOST instead type V1DocumentPOST struct { - Response + APIResponse Data struct { Bucket string DataDocument @@ -23,11 +23,11 @@ type V1DocumentPOST struct { } type V1DocumentDELETE struct { - Response + APIResponse } type V1DocumentKeyPUT struct { - Response + APIResponse Data struct { Bucket string Key string diff --git a/apiresponse/seed.go b/apiresponse/seed.go index b266c5a..623613c 100644 --- a/apiresponse/seed.go +++ b/apiresponse/seed.go @@ -1,7 +1,7 @@ package apiresponse type V1SeedPOST struct { - Response + APIResponse Data struct { Buckets []string } diff --git a/apiresponse/spec.go b/apiresponse/spec.go index d9e87c7..a426f9a 100644 --- a/apiresponse/spec.go +++ b/apiresponse/spec.go @@ -1,7 +1,7 @@ package apiresponse type V1SpecGET struct { - Response + APIResponse Data struct { Spec string } diff --git a/cmd/server.go b/cmd/server.go index b528664..7669521 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -13,7 +13,7 @@ import ( "io" "net/http" - "codeberg.org/vlbeaudoin/pave" + "codeberg.org/vlbeaudoin/pave/v2" "codeberg.org/vlbeaudoin/serpents" "github.com/spf13/cobra" "github.com/spf13/viper" diff --git a/go.mod b/go.mod index 52c286b..55a9915 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module git.agecem.com/agecem/agecem-org go 1.23.0 require ( - codeberg.org/vlbeaudoin/pave v1.1.1 + codeberg.org/vlbeaudoin/pave/v2 v2.0.0 codeberg.org/vlbeaudoin/serpents v1.1.0 - codeberg.org/vlbeaudoin/voki v1.7.2 + codeberg.org/vlbeaudoin/voki/v3 v3.0.0 github.com/labstack/echo/v4 v4.12.0 github.com/minio/minio-go/v7 v7.0.75 github.com/spf13/cobra v1.8.1 @@ -13,7 +13,6 @@ require ( ) require ( - codeberg.org/vlbeaudoin/voki/v2 v2.1.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-ini/ini v1.67.0 // indirect diff --git a/go.sum b/go.sum index 45bfed8..a63fabb 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,9 @@ -codeberg.org/vlbeaudoin/pave v1.1.1 h1:9Yb87vbBd+bNL3DYrJEQ3tiw8trSrFfkAZqVwVG6pHk= -codeberg.org/vlbeaudoin/pave v1.1.1/go.mod h1:n+xUdWwcUjiic4+DzUMPYFiDNNkmYT4UYvLRXTdV3Kw= +codeberg.org/vlbeaudoin/pave/v2 v2.0.0 h1:hfB5KnqMMu17g5QBWgLvWOsqidrYaohRfu2LflmTrb0= +codeberg.org/vlbeaudoin/pave/v2 v2.0.0/go.mod h1:TsTfP6IA+3Ph33vLZigeJWS5vgBPgkW1tfs3zFPfycU= codeberg.org/vlbeaudoin/serpents v1.1.0 h1:U9f2+2D1yUVHx90yePi2ZOLRLG/Wkoob4JXDIVyoBwA= codeberg.org/vlbeaudoin/serpents v1.1.0/go.mod h1:3bE/R0ToABwcUJtS1VcGEBa86K5FYhrZGAbFl2qL8kQ= -codeberg.org/vlbeaudoin/voki v1.7.2 h1:9sMuOCuJ0t4hhZgr/rbRswHvyXN4CuizGJaB/Exmd90= -codeberg.org/vlbeaudoin/voki v1.7.2/go.mod h1:5XTLx/KiW/OfiupF3o7PAAAU/UhsPdKSrVMmtHbmkPI= -codeberg.org/vlbeaudoin/voki/v2 v2.1.0 h1:pXav77QGMHvMF1RyvkEwK3VKBdQh3ATmgh48TXX0tlU= -codeberg.org/vlbeaudoin/voki/v2 v2.1.0/go.mod h1:TVdOLAxB94EJkylt5dleJlTkBzuxau8Xwd4TANQIR7U= +codeberg.org/vlbeaudoin/voki/v3 v3.0.0 h1:XdF/UTe9YUNj3hYrAyEvdmIMDYLL8SkqTwPkqw1yJ2c= +codeberg.org/vlbeaudoin/voki/v3 v3.0.0/go.mod h1:+6LMXosAu2ijNKV04sMwkeujpH+cghZU1fydqj2y95g= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/webhandler/webhandler.go b/webhandler/webhandler.go index bf6ba3b..9dd8fee 100644 --- a/webhandler/webhandler.go +++ b/webhandler/webhandler.go @@ -7,6 +7,7 @@ import ( "net/url" "sort" + "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/api" "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" @@ -58,9 +59,9 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error { if err != nil { response.Error = err.Error() response.Message = v1BucketsGET.Message - response.StatusCode = v1BucketsGET.StatusCode + response.SetStatusCode(v1BucketsGET.StatusCode()) - return c.Render(response.StatusCode, "documentation-html", response) + return c.Render(response.StatusCode(), "documentation-html", response) } //TODO check v1BucketsGET StatusCode and Error @@ -72,7 +73,7 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error { if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil { response.Error = err.Error() response.Message = "Error during json.Unmarshal /v1/bucket/:bucket" - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) return c.Render(http.StatusOK, "documentation-html", response) } @@ -86,12 +87,12 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error { sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name }) - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) //response.Message = "HandleDocumentation ok" // TODO render .Message return c.Render(http.StatusOK, "documentation-html", response) - //return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets) + //return c.Render(response.StatusCode(), "documentation-html", response.Data.Buckets) } func HandleFormulaires(c echo.Context) error { @@ -109,13 +110,15 @@ func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error { response, err := h.ApiClient.Voki.Call(http.MethodGet, unescaped, nil, true) if err != nil { - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) } defer response.Body.Close() switch response.StatusCode { case http.StatusNotFound: - return c.JSON(apiresponse.NotFoundResponse()) + response := voki.ResponseNotFound{} + return c.JSON(response.StatusCode(), response) case http.StatusInternalServerError: return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"}) } @@ -137,11 +140,11 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { v1BucketsGET, err := h.ApiClient.ListBuckets() if err != nil { - response.StatusCode = v1BucketsGET.StatusCode + response.SetStatusCode(v1BucketsGET.StatusCode()) response.Error = err.Error() response.Message = v1BucketsGET.Message - return c.Render(response.StatusCode, "admin-upload-html", nil) + return c.Render(response.StatusCode(), "admin-upload-html", nil) } for bucketName, displayName := range v1BucketsGET.Data.Buckets { @@ -150,9 +153,9 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { DisplayName: displayName, }) } - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { @@ -161,11 +164,11 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { v1BucketsGET, err := h.ApiClient.ListBuckets() if err != nil { - response.StatusCode = v1BucketsGET.StatusCode + response.SetStatusCode(v1BucketsGET.StatusCode()) response.Message = v1BucketsGET.Message response.Error = err.Error() - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } for bucketName, displayName := range v1BucketsGET.Data.Buckets { @@ -179,26 +182,26 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { form, err := c.MultipartForm() if err != nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Impossible de téléverser" response.Error = err.Error() - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } if form == nil { - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = "Formulaire pointe vers une addresse mémoire nulle" response.Error = "Formulaire pointe vers une addresse mémoire nulle" - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } if len(form.File) == 0 { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Veuillez sélectionner au moins 1 fichier à téléverser" response.Error = "Input 'documents' ne contient aucun fichier" - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } for inputName, inputFileHeaders := range form.File { @@ -209,26 +212,26 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { } if request.Data.Documents == nil { - response.StatusCode = http.StatusBadRequest + response.SetStatusCode(http.StatusBadRequest) response.Message = "Impossible d'obtenir les documents depuis le formulaire" response.Error = "Impossible d'obtenir les documents depuis le formulaire" - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } uploadDocumentsResponse, err := h.ApiClient.UploadDocuments(request.Params.Bucket, request.Data.Documents...) if err != nil { //TODO figure out pourquoi `err` n'est jamais `nil` - response.StatusCode = uploadDocumentsResponse.StatusCode + response.SetStatusCode(uploadDocumentsResponse.StatusCode()) response.Message = uploadDocumentsResponse.Message response.Error = fmt.Sprintf("%s. Détails: %s", err.Error(), uploadDocumentsResponse.Error) /* - response.StatusCode = http.StatusInternalServerError + response.SetStatusCode(http.StatusInternalServerError) response.Message = fmt.Sprintf("api.(*API).UploadDocuments: %s", err) response.Error = err.Error() */ - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } //TODO figure out pourquoi on se rend jamais ici @@ -246,8 +249,8 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { status = fmt.Sprintf("%s. Erreur: %s", status, errMsg) } - response.StatusCode = http.StatusOK + response.SetStatusCode(http.StatusOK) response.Message = fmt.Sprintf("%s \n %s", status, info) - return c.Render(response.StatusCode, "admin-upload-html", response) + return c.Render(response.StatusCode(), "admin-upload-html", response) } diff --git a/webresponse/webresponse.go b/webresponse/webresponse.go index b17b463..a529e80 100644 --- a/webresponse/webresponse.go +++ b/webresponse/webresponse.go @@ -6,21 +6,21 @@ import ( ) type HandleAdminDocumentsUploadResponse struct { - apiresponse.Response + apiresponse.APIResponse Data struct { Buckets []models.Bucket } } type HandleDocumentationResponse struct { - apiresponse.Response + apiresponse.APIResponse Data struct { Buckets []models.Bucket } } -type UploadDocumentResponse struct { - apiresponse.Response +type UploadDocumentAPIResponse struct { + apiresponse.APIResponse Data UploadDocumentResponseData } -- 2.45.2