Fix affichage de réponse à création et /documentation

Retirer caractères spéciaux lors de l'import
This commit is contained in:
Victor Lacasse-Beaudoin 2023-04-28 19:30:31 -04:00
parent 5d984ccacb
commit 3dd4dd6e29
2 changed files with 52 additions and 29 deletions

View file

@ -26,6 +26,17 @@ type APIOptions struct {
Password string
}
type UploadDocumentResponse struct {
Info UploadDocumentResponseInfo `json:"info"`
Message string `json:"message"`
}
type UploadDocumentResponseInfo struct {
Bucket string `json:"bucket"`
Object string `json:"key"`
Size float64 `json:"size"`
}
func New(protocol, host string, port int, opts APIOptions) (*API, error) {
api := API{
Protocol: protocol,
@ -109,7 +120,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
}
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (map[string]interface{}, error) {
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) {
endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol,
a.Host,
@ -125,29 +136,29 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
// Add the file to the request
file, err := file_header.Open()
if err != nil {
return nil, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
}
defer file.Close()
part, err := writer.CreateFormFile("document", file_header.Filename)
if err != nil {
return nil, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
}
_, err = io.Copy(part, file)
if err != nil {
return nil, fmt.Errorf("UploadDocument#io.Copy: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#io.Copy: %s", err)
}
err = writer.Close()
if err != nil {
return nil, fmt.Errorf("UploadDocument#writer.Close: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.Close: %s", err)
}
// Create a new HTTP request with the multipart body
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
@ -160,12 +171,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("UploadDocument#client.Do: %s", err)
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#client.Do: %s", err)
}
defer resp.Body.Close()
// Handle the response
var res map[string]interface{}
var res UploadDocumentResponse
json.NewDecoder(resp.Body).Decode(&res)
return res, nil