Merge pull request 'Ajouter et implémenter certaines responses' (#118) from fix/bucket_list_response into main

Reviewed-on: #118
This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-20 17:04:53 -05:00
commit 8c4fde507b
5 changed files with 142 additions and 67 deletions

View file

@ -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' or 'PUT', got '%s'", method))
return "", errors.New(fmt.Sprintf("method must be 'POST', 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
}

View file

@ -61,19 +61,24 @@ func HandleV1Seed(c echo.Context) error {
// HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent.
func HandleV1BucketList(c echo.Context) error { func HandleV1BucketList(c echo.Context) error {
var response models.V1BucketListResponse
var cfg config.Config var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil { if err := viper.Unmarshal(&cfg); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ response.StatusCode = http.StatusInternalServerError
"error": err.Error(), response.Message = "Error during viper.Unmarshal"
}) // response.Error = err.Error()
return c.JSON(response.StatusCode, response)
} }
mediaClient, err := media.NewMediaClientFromViper() mediaClient, err := media.NewMediaClientFromViper()
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ response.StatusCode = http.StatusInternalServerError
"message": "Error during media.NewMediaClientFromViper()", response.Message = "Error during media.NewMediaClientFromViper()"
"error": err.Error(), // response.Error = err.Error()
})
return c.JSON(response.StatusCode, response)
} }
var buckets = make(map[string]string) 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 { for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets {
exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name) exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
if err != nil { 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 { 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 { func HandleV1BucketRead(c echo.Context) error {

View file

@ -14,6 +14,20 @@ func (r Response) Respond() Responder {
return r return r
} }
type HandleAdminDocumentsUploadResponse struct {
Response
Data struct {
Buckets []Bucket
}
}
type HandleDocumentationResponse struct {
Response
Data struct {
Buckets []Bucket
}
}
type UploadDocumentResponse struct { type UploadDocumentResponse struct {
Response Response
Data UploadDocumentResponseData Data UploadDocumentResponseData
@ -31,3 +45,10 @@ type V1SeedResponse struct {
Buckets []string Buckets []string
} }
} }
type V1BucketListResponse struct {
Response
Data struct {
Buckets map[string]string
}
}

View file

@ -12,7 +12,7 @@
<form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data"> <form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data">
<label class="formLabel" for="bucket">Type de document:</label> <label class="formLabel" for="bucket">Type de document:</label>
<select class="formSelect" name="bucket" id="bucket"> <select class="formSelect" name="bucket" id="bucket">
{{ range .Buckets }} {{ range .Data.Buckets }}
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option> <option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
{{ end }} {{ end }}
</select> </select>

View file

@ -44,26 +44,27 @@ func HandleVieEtudianteOrganisme(c echo.Context) error {
} }
func HandleDocumentation(c echo.Context) error { func HandleDocumentation(c echo.Context) error {
var response models.HandleDocumentationResponse
client, err := api.NewApiClientFromViper() client, err := api.NewApiClientFromViper()
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) return c.Render(http.StatusInternalServerError, "documentation-html", nil)
} }
result, err := client.Call(http.MethodGet, "/v1/bucket") v1BucketListResponse, err := client.ListBuckets()
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) response.StatusCode = v1BucketListResponse.StatusCode
response.Message = v1BucketListResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "documentation-html", response)
} }
var buckets map[string]string //TODO check v1BucketListRespone StatusCode and Error
err = json.Unmarshal(result, &buckets) for bucket, displayName := range v1BucketListResponse.Data.Buckets {
if err != nil { // TODO move call to dedicated API client method
return c.Render(http.StatusInternalServerError, "documentation-html", nil) // TODO add Response type
}
var data []models.Bucket
for bucket, displayName := range buckets {
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) return c.Render(http.StatusInternalServerError, "documentation-html", nil)
@ -94,16 +95,19 @@ func HandleDocumentation(c echo.Context) error {
documents_processed := documents documents_processed := documents
*/ */
data = append(data, models.Bucket{ response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucket, Name: bucket,
DisplayName: displayName, DisplayName: displayName,
Documents: documents, Documents: documents,
}) })
} }
sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name }) sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
return c.Render(http.StatusOK, "documentation-html", data) response.StatusCode = http.StatusOK
// TODO render .Message
return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
} }
func HandleFormulaires(c echo.Context) error { func HandleFormulaires(c echo.Context) error {
@ -140,65 +144,80 @@ func HandleAdmin(c echo.Context) error {
} }
func HandleAdminDocumentsUpload(c echo.Context) error { func HandleAdminDocumentsUpload(c echo.Context) error {
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper() client, err := api.NewApiClientFromViper()
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) response.StatusCode = http.StatusInternalServerError
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", nil)
} }
result, err := client.Call(http.MethodGet, "/v1/bucket") v1BucketListResponse, err := client.ListBuckets()
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) response.StatusCode = v1BucketListResponse.StatusCode
response.Error = err.Error()
response.Message = v1BucketListResponse.Message
return c.Render(response.StatusCode, "admin-upload-html", nil)
} }
var buckets map[string]string for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var data struct {
Buckets []models.Bucket
Message string
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, models.Bucket{
Name: bucketName, Name: bucketName,
DisplayName: displayName, DisplayName: displayName,
}) })
} }
response.StatusCode = http.StatusOK
return c.Render(http.StatusOK, "admin-upload-html", data) return c.Render(response.StatusCode, "admin-upload-html", response)
} }
func HandleAdminDocumentsUploadPOST(c echo.Context) error { func HandleAdminDocumentsUploadPOST(c echo.Context) error {
var data struct { /*
Buckets []models.Bucket var data struct {
Message string Buckets []models.Bucket
} Message string
}
*/
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper() client, err := api.NewApiClientFromViper()
if err != nil { if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#api.New: %s", err) response.StatusCode = http.StatusInternalServerError
return c.Render(http.StatusInternalServerError, "admin-upload-html", data) response.Message = "Error during api.newApiClientFromViper()"
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
} }
result, err := client.Call(http.MethodGet, "/v1/bucket") v1BucketListResponse, err := client.ListBuckets()
if err != nil { if err != nil {
data.Message = "Error during GET /v1/bucket" response.StatusCode = v1BucketListResponse.StatusCode
return c.Render(http.StatusInternalServerError, "documentation-html", data) response.Message = v1BucketListResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
} }
var buckets map[string]string /*
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
data.Message = "Error during GET /v1/bucket"
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
err = json.Unmarshal(result, &buckets) var buckets map[string]string
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
for bucketName, displayName := range buckets { err = json.Unmarshal(result, &buckets)
data.Buckets = append(data.Buckets, models.Bucket{ if err != nil {
return c.Render(http.StatusInternalServerError, "admin-upload-html", nil)
}
*/
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucketName, Name: bucketName,
DisplayName: displayName, DisplayName: displayName,
}) })
@ -208,24 +227,31 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
document, err := c.FormFile("document") document, err := c.FormFile("document")
if err != nil { if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#c.FormFile: %s", err) response.StatusCode = http.StatusBadRequest
return c.Render(http.StatusBadRequest, "admin-upload-html", data) response.Message = "Formulaire invalide"
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
} }
response, err := client.UploadDocument(bucket, document) uploadDocumentResponse, err := client.UploadDocument(bucket, document)
if err != nil { if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#client.UploadDocument: %s", err) response.StatusCode = uploadDocumentResponse.StatusCode
return c.Render(http.StatusInternalServerError, "admin-upload-html", data) response.Message = uploadDocumentResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
} }
// Format response // Format response
var info, status string var info, status string
info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object) info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Object)
status = response.Message status = uploadDocumentResponse.Message
data.Message = fmt.Sprintf("%s - %s", status, info) response.StatusCode = http.StatusOK
response.Message = fmt.Sprintf("%s - %s", status, info)
return c.Render(http.StatusOK, "admin-upload-html", data) return c.Render(response.StatusCode, "admin-upload-html", response)
} }