-
Notifications
You must be signed in to change notification settings - Fork 479
/
http.go
104 lines (83 loc) · 2.04 KB
/
http.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
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
99
100
101
102
103
104
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
package http
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/dapr/pkg/logger"
)
// HTTPSource is a binding for an http url endpoint invocation
// nolint:golint
type HTTPSource struct {
metadata httpMetadata
logger logger.Logger
}
type httpMetadata struct {
URL string `json:"url"`
Method string `json:"method"`
}
// NewHTTP returns a new HTTPSource
func NewHTTP(logger logger.Logger) *HTTPSource {
return &HTTPSource{logger: logger}
}
// Init performs metadata parsing
func (h *HTTPSource) Init(metadata bindings.Metadata) error {
b, err := json.Marshal(metadata.Properties)
if err != nil {
return err
}
var m httpMetadata
err = json.Unmarshal(b, &m)
if err != nil {
return err
}
h.metadata = m
return nil
}
func (h *HTTPSource) get(url string) ([]byte, error) {
client := http.Client{Timeout: time.Second * 60}
// nolint: noctx
resp, err := client.Get(url)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return b, nil
}
func (h *HTTPSource) Read(handler func(*bindings.ReadResponse) error) error {
b, err := h.get(h.metadata.URL)
if err != nil {
return err
}
handler(&bindings.ReadResponse{
Data: b,
})
return nil
}
func (h *HTTPSource) Operations() []bindings.OperationKind {
return []bindings.OperationKind{bindings.CreateOperation}
}
func (h *HTTPSource) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
client := http.Client{Timeout: time.Second * 5}
// nolint: noctx
resp, err := client.Post(h.metadata.URL, "application/json; charset=utf-8", bytes.NewBuffer(req.Data))
if err != nil {
return nil, err
}
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return nil, nil
}