-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
60 lines (53 loc) · 1.04 KB
/
action.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package receiver
import (
"bytes"
"context"
"io"
"log"
"net/http"
"time"
)
const (
requestTimeout = 20 * time.Second
)
var (
httpClient = &http.Client{
Timeout: requestTimeout,
}
)
// Executable provides Exec method for action.
type Executable interface {
Exec(context.Context, []byte) error
}
// HTTPAction implements action for HTTP.
type HTTPAction struct {
header http.Header
method string
url string
}
// NewHTTPAction returns a new http action.
func NewHTTPAction(header http.Header, method, url string) *HTTPAction {
return &HTTPAction{
header: header,
method: method,
url: url,
}
}
// Exec executes pubsub action.
func (a *HTTPAction) Exec(ctx context.Context, payload []byte) error {
var body io.Reader
if len(payload) > 0 {
body = bytes.NewBuffer(payload)
}
req, err := http.NewRequest(a.method, a.url, body)
if err != nil {
return err
}
req.Header = a.header
resp, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
log.Printf("Sent, status: %v\n", resp.Status)
return nil
}