Déplacer api responses vers apiresponse/

This commit is contained in:
Victor Lacasse-Beaudoin 2023-10-24 17:39:15 -04:00
parent cb977f767d
commit 0962ea5a20
5 changed files with 42 additions and 29 deletions

View file

@ -14,6 +14,8 @@ ADD api/ api/
ADD api_handlers/ api_handlers/
ADD apiresponse/ apiresponse/
ADD config/ config/
ADD media/ media/

View file

@ -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)
}

View file

@ -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{}{

View file

@ -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
}
}

View file

@ -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 {