forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
64 lines (50 loc) · 1.22 KB
/
request.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
package doc
import (
"net/http"
"text/template"
)
var (
requestTmpl *template.Template
requestFmt = `{{if or .HasBody .HasHeader}}
+ Request {{if .HasContentType}}({{.Header.ContentType}}){{end}}{{with .Header}}
{{.Render}}{{end}}{{with .Body}}
{{.Render}}{{end}}{{end}}`
)
func init() {
requestTmpl = template.Must(template.New("request").Parse(requestFmt))
}
type Request struct {
Header *Header
Body *Body
Method string
// TODO:
// Attributes
// Schema
}
func NewRequest(req *http.Request) (*Request, error) {
body1, body2, err := cloneBody(req.Body)
if err != nil {
return nil, err
}
req.Body = nopCloser{body1}
b2bytes := body2.Bytes()
contentType := req.Header.Get("Content-Type")
contentEncoding := req.Header.Get("Content-Encoding")
return &Request{
Header: NewHeader(req.Header),
Body: NewBody(b2bytes, contentType, contentEncoding),
Method: req.Method,
}, nil
}
func (r *Request) Render() string {
return render(requestTmpl, r)
}
func (r *Request) HasBody() bool {
return r.Body != nil
}
func (r *Request) HasHeader() bool {
return r.Header != nil && len(r.Header.DisplayHeader) > 0
}
func (r *Request) HasContentType() bool {
return r.Header != nil && len(r.Header.ContentType) > 0
}