diff --git a/api/api.go b/api/api.go index 310bbdc..f70d2bf 100644 --- a/api/api.go +++ b/api/api.go @@ -271,3 +271,17 @@ func (a *API) CallWithData(method, route string, data []byte) (string, error) { //return "", errors.New(fmt.Sprintf("method must be 'POST' or 'PUT', got '%s'", method)) return "", errors.New(fmt.Sprintf("method must be 'POST', got '%s'", method)) } + +func (a *API) ListBuckets() (models.V1BucketListResponse, error) { + var response models.V1BucketListResponse + result, err := a.Call(http.MethodGet, "/v1/bucket") + if err != nil { + return response, err + } + + if err = json.Unmarshal(result, &response); err != nil { + return response, err + } + + return response, nil +} diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 3b8537b..c6ba7d6 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -61,19 +61,24 @@ func HandleV1Seed(c echo.Context) error { // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. func HandleV1BucketList(c echo.Context) error { + var response models.V1BucketListResponse + var cfg config.Config if err := viper.Unmarshal(&cfg); err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during viper.Unmarshal" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } mediaClient, err := media.NewMediaClientFromViper() if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during media.NewMediaClientFromViper()", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during media.NewMediaClientFromViper()" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } var buckets = make(map[string]string) @@ -81,7 +86,11 @@ func HandleV1BucketList(c echo.Context) error { for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets { exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name) if err != nil { - return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during minio#BucketExists" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } if exists { @@ -89,7 +98,12 @@ func HandleV1BucketList(c echo.Context) error { } } - return c.JSON(http.StatusOK, buckets) + response.StatusCode = http.StatusOK + response.Message = "Buckets list successful" + response.Data.Buckets = buckets + + return c.JSON(response.StatusCode, response) + //return c.JSON(response.StatusCode, response.Data.Buckets) } func HandleV1BucketRead(c echo.Context) error { diff --git a/models/responses.go b/models/responses.go index 815df1d..651b527 100644 --- a/models/responses.go +++ b/models/responses.go @@ -14,6 +14,20 @@ func (r Response) Respond() Responder { return r } +type HandleAdminDocumentsUploadResponse struct { + Response + Data struct { + Buckets []Bucket + } +} + +type HandleDocumentationResponse struct { + Response + Data struct { + Buckets []Bucket + } +} + type UploadDocumentResponse struct { Response Data UploadDocumentResponseData @@ -31,3 +45,10 @@ type V1SeedResponse struct { Buckets []string } } + +type V1BucketListResponse struct { + Response + Data struct { + Buckets map[string]string + } +} diff --git a/templates/html/admin-upload.gohtml b/templates/html/admin-upload.gohtml index c7afd53..f7f7440 100644 --- a/templates/html/admin-upload.gohtml +++ b/templates/html/admin-upload.gohtml @@ -12,7 +12,7 @@