agecem-org/api/api.go
Victor Lacasse-Beaudoin 2d27b1ea2d 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
2023-10-24 17:00:49 -04:00

105 lines
2.8 KiB
Go

package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"codeberg.org/vlbeaudoin/voki"
"git.agecem.com/agecem/agecem-org/config"
"git.agecem.com/agecem/agecem-org/models"
"github.com/spf13/viper"
)
type API struct {
Voki *voki.Voki
}
// NewFromViper returns a pointer to a new API object,
// provided the configuration options are managed by
// https://git.agecem.com/agecem/agecem-org/config
func NewFromViper(client *http.Client) (api *API, err error) {
var config config.Config
if err = viper.Unmarshal(&config); err != nil {
return nil, err
}
return New(client, config.Server.Api.Host, config.Server.Api.Key, config.Server.Api.Port, config.Server.Api.Protocol)
}
func New(client *http.Client, host, key string, port int, protocol string) (*API, error) {
return &API{Voki: voki.New(client, host, key, port, protocol)}, nil
}
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) {
var response models.V1DocumentCreateResponse
endpoint := fmt.Sprintf("%s://%s:%d",
a.Voki.Protocol,
a.Voki.Host,
a.Voki.Port,
)
current_url := fmt.Sprintf("%s/v1/bucket/%s", endpoint, bucket)
// Create a new multipart writer
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add the file to the request
file, err := file_header.Open()
if err != nil {
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 response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
}
part, err := writer.CreateFormFile("document", filename_processed)
if err != nil {
return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
}
_, err = io.Copy(part, file)
if err != nil {
return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
}
err = writer.Close()
if err != nil {
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 response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
if a.Voki.Key != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.Voki.Key))
}
// Send the HTTP request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return response, fmt.Errorf("UploadDocument#client.Do: %s", err)
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
func (a *API) ListBuckets() (response models.V1BucketListResponse, err error) {
return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response)
}