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) {
var response UploadDocumentResponse
endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol,
a.Host,
@ -165,34 +166,34 @@ 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 UploadDocumentResponse{}, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
}
defer file.Close()
filename_processed, err := url.QueryUnescape(file_header.Filename)
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)
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)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#io.Copy: %s", err)
return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
}
err = writer.Close()
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
req, err := http.NewRequest(http.MethodPost, current_url, body)
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())
@ -205,15 +206,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
client := &http.Client{}
resp, err := client.Do(req)
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()
// Handle the response
var res UploadDocumentResponse
json.NewDecoder(resp.Body).Decode(&res)
return res, nil
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// CallWithData takes data and returns a string representing a response body.