From 0962ea5a2015f43de16ec548dd38aa569050d21f Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 24 Oct 2023 17:39:15 -0400 Subject: [PATCH] =?UTF-8?q?D=C3=A9placer=20api=20responses=20vers=20apires?= =?UTF-8?q?ponse/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ api/api.go | 8 ++--- api_handlers/api_handlers.go | 30 +++++++++---------- .../apiresponse.go | 18 ++++++++--- web_handlers/web_handlers.go | 13 ++++---- 5 files changed, 42 insertions(+), 29 deletions(-) rename models/responses.go => apiresponse/apiresponse.go (81%) diff --git a/Dockerfile b/Dockerfile index c8f609b..d2396ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,8 @@ ADD api/ api/ ADD api_handlers/ api_handlers/ +ADD apiresponse/ apiresponse/ + ADD config/ config/ ADD media/ media/ diff --git a/api/api.go b/api/api.go index d84be3c..9e8fbf7 100644 --- a/api/api.go +++ b/api/api.go @@ -10,8 +10,8 @@ import ( "net/url" "codeberg.org/vlbeaudoin/voki" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/config" - "git.agecem.com/agecem/agecem-org/models" "github.com/spf13/viper" ) @@ -35,8 +35,8 @@ func New(client *http.Client, host, key string, port int, protocol string) (*API return &API{Voki: voki.New(client, host, key, port, protocol)}, nil } -func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) { - var response models.V1DocumentCreateResponse +func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (apiresponse.V1DocumentCreateResponse, error) { + var response apiresponse.V1DocumentCreateResponse endpoint := fmt.Sprintf("%s://%s:%d", a.Voki.Protocol, a.Voki.Host, @@ -100,6 +100,6 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( return response, err } -func (a *API) ListBuckets() (response models.V1BucketListResponse, err error) { +func (a *API) ListBuckets() (response apiresponse.V1BucketListResponse, err error) { return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response) } diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 8ce859f..21ddb63 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -5,9 +5,9 @@ import ( "net/http" "sort" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" - "git.agecem.com/agecem/agecem-org/models" "github.com/labstack/echo/v4" "github.com/minio/minio-go/v7" ) @@ -30,7 +30,7 @@ func (h *V1Handler) HandleV1(c echo.Context) error { // HandleV1Seed créé des buckets dans minio selon la liste de buckets dans server.documents.buckets // Les buckets sont créés avec paramètres par défaut, et sont ensuite visible dans /v1/bucket. func (h *V1Handler) HandleV1Seed(c echo.Context) error { - var response models.V1SeedResponse + var response apiresponse.V1SeedResponse new_buckets, err := h.MediaClient.Seed() response.Data.Buckets = new_buckets @@ -56,7 +56,7 @@ func (h *V1Handler) HandleV1Seed(c echo.Context) error { // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. func (h *V1Handler) HandleV1BucketList(c echo.Context) error { - var response models.V1BucketListResponse + var response apiresponse.V1BucketListResponse var buckets = make(map[string]string) @@ -83,7 +83,7 @@ func (h *V1Handler) HandleV1BucketList(c echo.Context) error { } func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { - var response models.V1BucketReadResponse + var response apiresponse.V1BucketReadResponse bucket := c.Param("bucket") @@ -95,7 +95,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -112,7 +112,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { } if !exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) @@ -137,7 +137,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { // HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { - var response models.V1DocumentCreateResponse + var response apiresponse.V1DocumentCreateResponse bucket := c.Param("bucket") @@ -158,7 +158,7 @@ func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -208,7 +208,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -221,7 +221,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { } if !bucket_exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) @@ -229,7 +229,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -253,7 +253,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { // HandleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename func (h *V1Handler) HandleV1DocumentUpdate(c echo.Context) error { - return c.JSON(models.NotImplementedResponse()) + return c.JSON(apiresponse.NotImplementedResponse()) } // HandleV1DocumentDelete permet de supprimer un object @@ -269,7 +269,7 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -282,14 +282,14 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error { } if !bucket_exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/models/responses.go b/apiresponse/apiresponse.go similarity index 81% rename from models/responses.go rename to apiresponse/apiresponse.go index dd72936..71ca286 100644 --- a/models/responses.go +++ b/apiresponse/apiresponse.go @@ -1,6 +1,10 @@ -package models +package apiresponse -import "net/http" +import ( + "net/http" + + "git.agecem.com/agecem/agecem-org/models" +) type Responder interface { Respond() Responder @@ -36,17 +40,23 @@ func NotImplementedResponse() (int, SimpleResponse) { } } +func InternalServerErrorResponse() (int, SimpleResponse) { + return http.StatusInternalServerError, SimpleResponse{ + Message: "Internal Server Error", + } +} + type HandleAdminDocumentsUploadResponse struct { Response Data struct { - Buckets []Bucket + Buckets []models.Bucket } } type HandleDocumentationResponse struct { Response Data struct { - Buckets []Bucket + Buckets []models.Bucket } } diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index da02d10..8582c69 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -8,6 +8,7 @@ import ( "sort" "git.agecem.com/agecem/agecem-org/api" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/models" "github.com/labstack/echo/v4" ) @@ -49,7 +50,7 @@ func HandleVieEtudianteOrganisme(c echo.Context) error { } func (h *WebHandler) HandleDocumentation(c echo.Context) error { - var response models.HandleDocumentationResponse + var response apiresponse.HandleDocumentationResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -64,7 +65,7 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error { for bucket, displayName := range v1BucketListResponse.Data.Buckets { // TODO move call to dedicated API client method - var v1BucketReadResponse models.V1BucketReadResponse + var v1BucketReadResponse apiresponse.V1BucketReadResponse if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil { response.Error = err.Error() @@ -106,13 +107,13 @@ 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(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } defer response.Body.Close() switch response.StatusCode { case http.StatusNotFound: - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) case http.StatusInternalServerError: return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"}) } @@ -130,7 +131,7 @@ func HandleAdmin(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { - var response models.HandleAdminDocumentsUploadResponse + var response apiresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -153,7 +154,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { - var response models.HandleAdminDocumentsUploadResponse + var response apiresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil {