an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

152 lines
3.9 KiB

package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"path/filepath"
)
// UploadAttachment uploads a file as an attachment to a job
func (s *Session) UploadAttachment(jobID, filename, purpose string, fileContent []byte) (map[string]interface{}, error) {
url := fmt.Sprintf("%s/attachment", BaseURL)
// Create a buffer to hold the form data
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Add the purpose (attachment type)
if err := w.WriteField("purpose", purpose); err != nil {
return nil, fmt.Errorf("error writing purpose field: %v", err)
}
// Add the job ID
if err := w.WriteField("job", jobID); err != nil {
return nil, fmt.Errorf("error writing job field: %v", err)
}
// Add the file
fw, err := w.CreateFormFile("filename", filepath.Base(filename))
if err != nil {
return nil, fmt.Errorf("error creating form file: %v", err)
}
if _, err := io.Copy(fw, bytes.NewReader(fileContent)); err != nil {
return nil, fmt.Errorf("error copying file content: %v", err)
}
// Close the writer
if err := w.Close(); err != nil {
return nil, fmt.Errorf("error closing multipart writer: %v", err)
}
// Create the request
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
// Set headers
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Cookie", s.Cookie)
// Send the request
resp, err := s.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
// Check for errors
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
// Parse the response
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error parsing response: %v", err)
}
log.Printf("Successfully uploaded attachment %s to job %s", filename, jobID)
return result, nil
}
// GetAttachmentInfo gets information about a specific attachment
func (s *Session) GetAttachmentInfo(attachmentID string) (map[string]interface{}, error) {
url := fmt.Sprintf("%s/attachment/%s", BaseURL, attachmentID)
// Create the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
// Set headers
req.Header.Set("Cookie", s.Cookie)
// Send the request
resp, err := s.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
// Check for errors
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
// Parse the response
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error parsing response: %v", err)
}
return result, nil
}
// DeleteAttachment deletes an attachment
func (s *Session) DeleteAttachment(attachmentID string) error {
url := fmt.Sprintf("%s/attachment/%s", BaseURL, attachmentID)
// Create the request
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
// Set headers
req.Header.Set("Cookie", s.Cookie)
// Send the request
resp, err := s.Client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
// Check for errors
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
return nil
}