forked from betacraft/yaag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
106 lines (98 loc) · 2.69 KB
/
filter.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
package filters
import (
"encoding/json"
"encoding/xml"
"github.com/masato25/yaag/middleware"
"github.com/masato25/yaag/yaag"
"github.com/masato25/yaag/yaag/models"
"github.com/revel/revel"
"log"
"net/http/httptest"
"strings"
)
func FilterForApiDoc(c *revel.Controller, fc []revel.Filter) {
if record, _ := revel.Config.Bool("yaag.record"); !record {
fc[0](c, fc[1:])
return
}
w := httptest.NewRecorder()
c.Response = revel.NewResponse(w)
httpVerb := c.Request.Method
customParams := make(map[string]interface{})
headers := make(map[string]string)
hasJson := false
hasXml := false
body := middleware.ReadBody(c.Request.Request)
if c.Request.ContentType == "application/json" {
if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" {
err := json.Unmarshal([]byte(*body), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasJson = true
}
} else {
err := json.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasJson = true
}
}
} else if c.Request.ContentType == "application/xml" {
if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" {
err := xml.Unmarshal([]byte(*body), &customParams)
if err != nil {
log.Println("Xml Error ! ", err)
} else {
hasXml = true
}
} else {
err := xml.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasXml = true
}
}
}
log.Println(hasJson, hasXml)
// call remaiing filters
fc[0](c, fc[1:])
c.Result.Apply(c.Request, c.Response)
htmlValues := models.ApiCall{}
htmlValues.CommonRequestHeaders = make(map[string]string)
// get headers
for k, v := range c.Request.Header {
isCommon := false
for _, hk := range yaag.CommonHeaders {
if k == hk {
isCommon = true
htmlValues.CommonRequestHeaders[k] = strings.Join(v, " ")
break
}
}
if !isCommon {
headers[k] = strings.Join(v, " ")
}
}
htmlValues.MethodType = httpVerb
htmlValues.CurrentPath = c.Request.URL.Path
htmlValues.PostForm = make(map[string]string)
for k, v := range c.Params.Form {
htmlValues.PostForm[k] = strings.Join(v, " ")
}
htmlValues.RequestBody = *body
htmlValues.RequestHeader = headers
htmlValues.RequestUrlParams = make(map[string]string)
for k, v := range c.Request.URL.Query() {
htmlValues.RequestUrlParams[k] = strings.Join(v, " ")
}
htmlValues.ResponseHeader = make(map[string]string)
htmlValues.ResponseBody = w.Body.String()
for k, v := range w.Header() {
htmlValues.ResponseHeader[k] = strings.Join(v, " ")
}
htmlValues.ResponseCode = w.Code
go yaag.GenerateHtml(&htmlValues)
}