Ajouter et implémenter certaines responses #118

Merged
vlbeaudoin merged 4 commits from fix/bucket_list_response into main 2023-08-20 18:04:54 -04:00
5 changed files with 142 additions and 67 deletions
api
api_handlers
models
templates/html
web_handlers

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', 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.
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 {

View file

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

View file

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

View file

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