-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
90 lines (81 loc) · 2.13 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
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
package tranquility
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"strings"
"text/template"
)
//Action provides an abstraction for Http Request basic parameters
type Action struct {
Method string
URL string
Headers map[string]string
Parameters map[string]string
Body string
}
//Response provides an abstraction for a parsed HTTP response
type Response struct {
Status string
StatusCode int
Header http.Header
Body string
ContentLength int64
Request *http.Request
}
var parser = template.New("env-parser")
//parseString is an auxiliary function to replace templates in a given string
func parseString(toParsed string) string {
var parsed bytes.Buffer
tmpl, err := parser.Parse(toParsed)
if err != nil {
panic(err)
}
tmpl.Option("missingkey=error")
err = tmpl.Execute(&parsed, Env)
if err != nil {
panic(err)
}
return parsed.String()
}
//setup replaces all template variables in URL, Headers and Body preparing the HTTP request to be executed
func (action Action) setup() (*http.Request, error) {
baseURL, err := url.Parse(parseString(action.URL))
if err != nil {
return nil, err
}
if action.Parameters != nil {
params := url.Values{}
for k, v := range action.Parameters {
params.Add(k, parseString(v))
}
baseURL.RawQuery = params.Encode()
}
req, err := http.NewRequest(action.Method, baseURL.String(), strings.NewReader(parseString(action.Body)))
if err != nil {
return nil, err
}
if action.Headers != nil {
for k, v := range action.Headers {
req.Header.Add(k, parseString(v))
}
}
return req, nil
}
//Run perfoms a HTTP response based on the parameters of the action and parses the result into a Response
func (action Action) Run() (*Response, error) {
req, err := action.setup()
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
response := &Response{Status: res.Status, StatusCode: res.StatusCode, Header: res.Header, Body: string(body),
ContentLength: res.ContentLength, Request: res.Request}
return response, nil
}