2024-06-18 19:58:59 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-08-15 21:40:17 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2024-06-18 19:58:59 +01:00
|
|
|
"fmt"
|
2024-08-15 21:40:17 +01:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2024-06-18 19:58:59 +01:00
|
|
|
"net/http"
|
2024-08-15 21:40:17 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-06-18 19:58:59 +01:00
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
|
|
)
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
const (
|
|
|
|
baseMediaPath string = "/api/v1/media"
|
|
|
|
)
|
|
|
|
|
2024-06-18 19:58:59 +01:00
|
|
|
func (g *Client) GetMediaAttachment(mediaAttachmentID string) (model.Attachment, error) {
|
2024-08-02 08:15:15 +01:00
|
|
|
url := g.Authentication.Instance + baseMediaPath + "/" + mediaAttachmentID
|
2024-06-18 19:58:59 +01:00
|
|
|
|
|
|
|
var attachment model.Attachment
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
params := requestParameters{
|
2024-08-02 13:04:40 +01:00
|
|
|
httpMethod: http.MethodGet,
|
|
|
|
url: url,
|
2024-08-02 08:15:15 +01:00
|
|
|
requestBody: nil,
|
|
|
|
contentType: "",
|
2024-08-02 13:04:40 +01:00
|
|
|
output: &attachment,
|
2024-08-02 08:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
2024-06-18 19:58:59 +01:00
|
|
|
return model.Attachment{}, fmt.Errorf("received an error after sending the request to get the media attachment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachment, nil
|
|
|
|
}
|
2024-08-02 08:15:15 +01:00
|
|
|
|
2024-08-15 21:40:17 +01:00
|
|
|
func (g *Client) CreateMediaAttachment(path, description, focus string) (model.Attachment, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf("unable to open the file: %w", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// create the request body using a writer from the multipart package
|
|
|
|
requestBody := bytes.Buffer{}
|
|
|
|
requestBodyWriter := multipart.NewWriter(&requestBody)
|
|
|
|
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
|
|
|
|
part, err := requestBodyWriter.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf("unable to create the new part: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(part, file); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf("unable to copy the file contents to the form: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the description
|
|
|
|
if description != "" {
|
|
|
|
descriptionFormFieldWriter, err := requestBodyWriter.CreateFormField("description")
|
|
|
|
if err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"unable to create the writer for the 'description' form field: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.WriteString(descriptionFormFieldWriter, description); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"unable to write the description to the form: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the focus values
|
|
|
|
if focus != "" {
|
|
|
|
focusFormFieldWriter, err := requestBodyWriter.CreateFormField("focus")
|
|
|
|
if err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"unable to create the writer for the 'focus' form field: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.WriteString(focusFormFieldWriter, focus); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"unable to write the focus values to the form: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := requestBodyWriter.Close(); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf("unable to close the writer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
url := g.Authentication.Instance + baseMediaPath
|
|
|
|
|
|
|
|
var attachment model.Attachment
|
|
|
|
|
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
|
|
|
requestBody: &requestBody,
|
|
|
|
contentType: requestBodyWriter.FormDataContentType(),
|
|
|
|
output: &attachment,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"received an error after sending the request to create the media attachment: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) UpdateMediaAttachment(mediaAttachmentID, description, focus string) (model.Attachment, error) {
|
|
|
|
form := struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Focus string `json:"focus"`
|
|
|
|
}{
|
|
|
|
Description: description,
|
|
|
|
Focus: focus,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(form)
|
|
|
|
if err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf("unable to marshal the form: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
|
|
url := g.Authentication.Instance + baseMediaPath + "/" + mediaAttachmentID
|
|
|
|
|
|
|
|
var updatedMediaAttachment model.Attachment
|
|
|
|
|
|
|
|
params := requestParameters{
|
|
|
|
httpMethod: http.MethodPut,
|
|
|
|
url: url,
|
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
|
|
|
output: &updatedMediaAttachment,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
|
|
return model.Attachment{}, fmt.Errorf(
|
|
|
|
"received an error after sending the request to update the media attachment: %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedMediaAttachment, nil
|
|
|
|
}
|