-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
121 lines (102 loc) · 3.37 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
bottalk "github.com/bottalk/go-plugin"
)
type btRequest struct {
Token string `json:"token"`
UserID string `json:"user"`
Input json.RawMessage `json:"input"`
URL string `json:"url"`
Payload string `json:"payload"`
Headers []string `json:"headers"`
}
func errorResponse(message string) string {
return "{\"result\": \"fail\",\"message\":\"" + message + "\"}"
}
func doAction(method string, r *http.Request) string {
var BTR btRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&BTR)
if err != nil {
return errorResponse(err.Error())
}
log.Println(BTR.URL)
if len(BTR.URL) < 5 {
return errorResponse("Url is not defined")
}
req, err := http.NewRequest(strings.ToUpper(method), BTR.URL, bytes.NewBuffer([]byte(BTR.Payload)))
req.Header.Set("Content-Type", "application/json")
for _, hd := range BTR.Headers {
if strings.Contains(hd, ":") {
req.Header.Set(strings.Split(hd, ":")[0], strings.Split(hd, ":")[1])
}
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return errorResponse(err.Error())
}
output, _ := ioutil.ReadAll(res.Body)
log.Println(string(output))
return "{\"result\": \"ok\",\"status\":" + fmt.Sprintf("%d", res.StatusCode) + ",\"response\":" + string(output) + "}"
}
func main() {
plugin := bottalk.NewPlugin()
plugin.Name = "HTTP Plugin"
plugin.Description = "This plugin performs http queries with different methods"
plugin.Actions = map[string]bottalk.Action{
"get": bottalk.Action{
Name: "get",
Description: "This performs GET-request to remote endpoint",
Endpoint: "/get",
Action: func(r *http.Request) string {
return doAction("get", r)
},
Params: map[string]string{"url": "Endpoint url to call", "headers": "Array of headers to send"},
},
"post": bottalk.Action{
Name: "post",
Description: "This performs POST-request to remote endpoint",
Endpoint: "/post",
Action: func(r *http.Request) string {
return doAction("post", r)
},
Params: map[string]string{"url": "Endpoint url to call", "payload": "Payload to send in json format", "headers": "Array of headers to send"},
},
"patch": bottalk.Action{
Name: "patch",
Description: "This performs PATCH-request to remote endpoint",
Endpoint: "/patch",
Action: func(r *http.Request) string {
return doAction("patch", r)
},
Params: map[string]string{"url": "Endpoint url to call", "payload": "Payload to send in json format", "headers": "Array of headers to send"},
},
"put": bottalk.Action{
Name: "put",
Description: "This performs PUT-request to remote endpoint",
Endpoint: "/put",
Action: func(r *http.Request) string {
return doAction("put", r)
},
Params: map[string]string{"url": "Endpoint url to call", "payload": "Payload to send in json format", "headers": "Array of headers to send"},
},
"delete": bottalk.Action{
Name: "delete",
Description: "This performs DELETE-request to remote endpoint",
Endpoint: "/delete",
Action: func(r *http.Request) string {
return doAction("delete", r)
},
Params: map[string]string{"url": "Endpoint url to call", "payload": "Payload to send in json format", "headers": "Array of headers to send"},
},
}
plugin.Run(":9064")
}