-
Notifications
You must be signed in to change notification settings - Fork 14
/
enrich.go
61 lines (48 loc) · 1.3 KB
/
enrich.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
package transform
import (
"bytes"
"encoding/json"
"fmt"
"io"
gohttp "net/http"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
)
// enrichHTTPInterp is used for interpolating data into URLs.
const enrichHTTPInterp = `${DATA}`
type enrichDNSConfig struct {
Object iconfig.Object `json:"object"`
Request iconfig.Request `json:"request"`
}
func (c *enrichDNSConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *enrichDNSConfig) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
if c.Request.Timeout == "" {
c.Request.Timeout = "1s"
}
return nil
}
func enrichHTTPParseResponse(resp *gohttp.Response) ([]byte, error) {
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
dst := &bytes.Buffer{}
if json.Valid(buf) {
// Compact converts a multi-line object into a single-line object.
if err := json.Compact(dst, buf); err != nil {
return nil, err
}
} else {
dst = bytes.NewBuffer(buf)
}
return dst.Bytes(), nil
}