-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexecution_requests.go
44 lines (35 loc) · 1.09 KB
/
execution_requests.go
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
package se2
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
const (
pathExec = "/name/%s/%s/%s"
)
// Exec takes a context, a byte slice payload, an ident, namespace, and plugin triad to identify the plugin to run with
// the payload as input. It returns a byte slice as output, and an error if something went wrong.
func (c *Client) Exec(ctx context.Context, payload []byte, ident, namespace, plugin string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf(c.execHost+pathExec, ident, namespace, plugin), bytes.NewReader(payload))
if err != nil {
return nil, errors.Wrap(err, "client.Exec: http.NewRequest")
}
res, err := c.do(req)
if err != nil {
return nil, errors.Wrap(err, "client.Exec: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf(httpResponseCodeErrorFormat, "client.Exec", http.StatusOK, res.StatusCode)
}
b, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "client.Exec: io.ReadAll(res.Body)")
}
return b, nil
}