Ajouter models/ pour type Bucket #109

Merged
vlbeaudoin merged 8 commits from refactor/models into main 2023-08-19 20:31:38 -04:00
Showing only changes of commit 73b5ce1bc2 - Show all commits

View file

@ -150,6 +150,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
} }
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) { func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) {
var response UploadDocumentResponse
endpoint := fmt.Sprintf("%s://%s:%d", endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol, a.Protocol,
a.Host, a.Host,
@ -165,34 +166,34 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
// Add the file to the request // Add the file to the request
file, err := file_header.Open() file, err := file_header.Open()
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#file_header.Open: %s", err) return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
} }
defer file.Close() defer file.Close()
filename_processed, err := url.QueryUnescape(file_header.Filename) filename_processed, err := url.QueryUnescape(file_header.Filename)
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err) return response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
} }
part, err := writer.CreateFormFile("document", filename_processed) part, err := writer.CreateFormFile("document", filename_processed)
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
} }
_, err = io.Copy(part, file) _, err = io.Copy(part, file)
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#io.Copy: %s", err) return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
} }
err = writer.Close() err = writer.Close()
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.Close: %s", err) return response, fmt.Errorf("UploadDocument#writer.Close: %s", err)
} }
// Create a new HTTP request with the multipart body // Create a new HTTP request with the multipart body
req, err := http.NewRequest(http.MethodPost, current_url, body) req, err := http.NewRequest(http.MethodPost, current_url, body)
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#http.NewRequest: %s", err) return response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
} }
req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Content-Type", writer.FormDataContentType())
@ -205,15 +206,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#client.Do: %s", err) return response, fmt.Errorf("UploadDocument#client.Do: %s", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Handle the response err = json.NewDecoder(resp.Body).Decode(&response)
var res UploadDocumentResponse return response, err
json.NewDecoder(resp.Body).Decode(&res)
return res, nil
} }
// CallWithData takes data and returns a string representing a response body. // CallWithData takes data and returns a string representing a response body.