-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestful.go
142 lines (130 loc) · 3.37 KB
/
restful.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package restful
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/PaesslerAG/jsonpath"
"github.com/dop251/goja"
"github.com/qntfy/kazaam"
)
type Options struct {
Method string
Payload string
Headers map[string]string
Transformer string
XPath string
JS string
}
func interfacify(input []string) []interface{} {
vals := make([]interface{}, len(input))
for i, v := range input {
vals[i] = v
}
return vals
}
func tokenize(input string) (string, []string) {
re := regexp.MustCompile(`(?m)\$\{(.+?)\}`)
substitution := "%s"
var variables []string
for _, variable := range re.FindAllStringSubmatch(input, -1) {
variables = append(variables, os.Getenv(variable[1]))
}
return re.ReplaceAllString(input, substitution), variables
}
func cleanString(format string, variables ...any) string {
return fmt.Sprintf(format, variables...)
}
func Call(url string, options *Options) (string, int) {
method := "GET"
var data []byte
format, tokens := tokenize(url)
parsedUrl := cleanString(format, interfacify(tokens)...)
if options.Payload != "" {
format, tokens := tokenize(options.Payload)
data = []byte(cleanString(format, interfacify(tokens)...))
}
if options.Method != "" {
method = options.Method
}
log.Println("Performing Http ...", parsedUrl, strings.ToUpper(method))
client := &http.Client{}
var req *http.Request
if data != nil {
req, _ = http.NewRequest(method, parsedUrl, bytes.NewBuffer(data))
} else {
req, _ = http.NewRequest(method, parsedUrl, nil)
}
if options.Headers != nil {
if _, ok := options.Headers["Content-Type"]; !ok {
req.Header.Set("Content-Type", "application/json")
}
for key, value := range options.Headers {
format, tokens := tokenize(value)
req.Header.Set(key, cleanString(format, interfacify(tokens)...))
}
}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
return err.Error(), resp.StatusCode
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
// Convert response body to string
bodyString := string(bodyBytes)
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return bodyString, resp.StatusCode
}
if !kazaam.IsJsonFast(bodyBytes) {
return "Invalid JSON", 400
}
v := interface{}(nil)
json.Unmarshal([]byte(bodyString), &v)
if options.JS != "" {
vm := goja.New()
vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
vm.Set("data", v)
val, err := vm.RunString(options.JS)
if err != nil {
panic(err)
}
v = val.Export()
// convert the map to JSON
b, err := json.Marshal(v)
if err != nil {
return err.Error(), 500
}
bodyString = string(b)
}
if options.XPath != "" {
log.Println("Performing xpath with ", options.XPath)
doc, err := jsonpath.Get(options.XPath, v)
if err != nil {
return err.Error(), 500
}
// convert the map to JSON
b, err := json.Marshal(doc)
if err != nil {
return err.Error(), 500
}
bodyString = string(b)
log.Println("xpath result ", bodyString)
}
if options.Transformer != "" {
log.Println("Performing transformation with ", options.Transformer)
k, _ := kazaam.NewKazaam(options.Transformer)
json, e := k.TransformJSONStringToString(bodyString)
if e != nil {
return e.Error(), 500
}
log.Println("Performed transformation with {0}", json)
return json, resp.StatusCode
}
return bodyString, resp.StatusCode
}