-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_run.go
More file actions
98 lines (80 loc) · 2.44 KB
/
workflow_run.go
File metadata and controls
98 lines (80 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package unstructured
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
)
// RunWorkflowRequest represents the request to run a workflow
type RunWorkflowRequest struct {
ID string
// InputFiles is a list of files to upload to the workflow.
// The files must implement the io.Reader interface.
InputFiles []File
}
// File represents a file to upload to the workflow.
type File interface {
Name() string
io.Reader
}
// FileBytes implements the File interface for an io.Reader in memory.
type FileBytes struct {
Filename string
Bytes io.Reader
}
// Name returns the name of the file.
func (f *FileBytes) Name() string { return f.Filename }
// Read reads the file into the given buffer.
func (f *FileBytes) Read(p []byte) (n int, err error) {
return f.Bytes.Read(p) //nolint:wrapcheck
}
// RunWorkflow runs a workflow by triggering a new job
func (c *Client) RunWorkflow(ctx context.Context, in *RunWorkflowRequest) (*Job, error) {
req, err := http.NewRequestWithContext(ctx,
http.MethodPost,
c.endpoint.JoinPath("workflows", in.ID, "run").String(),
nil,
)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
// Determine if we need to upload files
if len(in.InputFiles) > 0 {
if err := addfiles(req, in.InputFiles); err != nil {
return nil, fmt.Errorf("failed to add files to request: %w", err)
}
}
var job Job
if err := c.do(req, &job); err != nil {
return nil, fmt.Errorf("failed to run workflow: %w", err)
}
return &job, nil
}
// runWorkflowWithoutFiles handles the case where no files are provided
func addfiles(req *http.Request, files []File) error {
// Create a buffer to hold the multipart form data
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// Add each file to the multipart form
for _, f := range files {
// Create a form file field
part, err := writer.CreateFormFile("input_files", f.Name())
if err != nil {
return fmt.Errorf("failed to create form file for %s: %w", f, err)
}
// Copy the file content to the form part
if _, err := io.Copy(part, f); err != nil {
return fmt.Errorf("failed to copy file content for %s: %w", f, err)
}
}
// Close the multipart writer
if err := writer.Close(); err != nil {
return fmt.Errorf("failed to close multipart writer: %w", err)
}
req.Body = io.NopCloser(&buf)
// Set the content type header for multipart form data
req.Header.Set("Content-Type", writer.FormDataContentType())
return nil
}