Migrer client API à voki
Autres changements: - Implémenter client voki à web_handlers - Retirer implémentations manuelles de api.Call et api.CallBytes - Fix web_handlers.HandlePublicDocumentation qui retournait le contenu du body même si le api request retournait StatusCode=http.StatusNotFound
This commit is contained in:
parent
bdeeab64a5
commit
2d27b1ea2d
5 changed files with 44 additions and 225 deletions
|
@ -1,8 +1,8 @@
|
|||
package web_handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
|
@ -52,9 +52,9 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
|||
|
||||
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
||||
if err != nil {
|
||||
response.StatusCode = v1BucketListResponse.StatusCode
|
||||
response.Message = v1BucketListResponse.Message
|
||||
response.Error = err.Error()
|
||||
response.Message = v1BucketListResponse.Message
|
||||
response.StatusCode = v1BucketListResponse.StatusCode
|
||||
|
||||
return c.Render(response.StatusCode, "documentation-html", response)
|
||||
}
|
||||
|
@ -63,24 +63,14 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
|||
|
||||
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
|
||||
// TODO move call to dedicated API client method
|
||||
content, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
|
||||
if err != nil {
|
||||
response.StatusCode = http.StatusInternalServerError
|
||||
response.Message = "Error during /v1/bucket/:bucket"
|
||||
response.Error = err.Error()
|
||||
|
||||
return c.Render(response.StatusCode, "documentation-html", response)
|
||||
}
|
||||
|
||||
var v1BucketReadResponse models.V1BucketReadResponse
|
||||
|
||||
err = json.Unmarshal(content, &v1BucketReadResponse)
|
||||
if err != nil {
|
||||
response.StatusCode = http.StatusInternalServerError
|
||||
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
||||
if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil {
|
||||
response.Error = err.Error()
|
||||
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
||||
response.StatusCode = http.StatusInternalServerError
|
||||
|
||||
return c.Render(response.StatusCode, "documentation-html", response)
|
||||
return c.Render(http.StatusOK, "documentation-html", response)
|
||||
}
|
||||
|
||||
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
||||
|
@ -96,7 +86,7 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
|||
//response.Message = "HandleDocumentation ok"
|
||||
|
||||
// TODO render .Message
|
||||
return c.Render(response.StatusCode, "documentation-html", response)
|
||||
return c.Render(http.StatusOK, "documentation-html", response)
|
||||
//return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
|
||||
}
|
||||
|
||||
|
@ -108,20 +98,25 @@ func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
|
|||
bucket := c.Param("bucket")
|
||||
document := c.Param("document")
|
||||
|
||||
result, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
|
||||
response, err := h.ApiClient.Voki.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document), nil, true)
|
||||
if err != nil {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
// Check if result can fit inside a map containing a message
|
||||
var result_map map[string]string
|
||||
|
||||
err = json.Unmarshal(result, &result_map)
|
||||
if err == nil {
|
||||
return c.JSON(http.StatusBadRequest, result_map)
|
||||
switch response.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
case http.StatusInternalServerError:
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, "application/octet-stream", result)
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, "application/octet-stream", body)
|
||||
}
|
||||
|
||||
func HandleAdmin(c echo.Context) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue