-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCollectionItemResponse.go
51 lines (43 loc) · 1.29 KB
/
CollectionItemResponse.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"path/filepath"
"strings"
)
type CollectionItemResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Code int `json:"code"`
Header []ResponseHeader `json:"header"`
Body string `json:"body"`
}
func (r CollectionItemResponse) BodyIncludePath(request CollectionItemRequest) string {
dir, file := filepath.Split(request.ShortUrl())
responseName := strings.Replace(r.Name, " ", "_", 9999)
file = fmt.Sprintf("%v-%v-%v", strings.ToLower(request.Method), responseName, file)
return fmt.Sprintf("responses%v%v.json", dir, file)
}
func (r CollectionItemResponse) BodyIncludeString(request CollectionItemRequest) template.HTML {
return template.HTML(fmt.Sprintf("<!-- include(%v) -->", r.BodyIncludePath(request)))
}
func (r CollectionItemResponse) ContentType() string {
contentType := ""
for _, header := range r.Header {
if strings.ToLower(header.Key) == "content-type" {
contentType = header.Value
}
}
return contentType
}
func (r CollectionItemResponse) FormattedBody() string {
var out bytes.Buffer
err := json.Indent(&out, []byte(r.Body), "", " ")
if err != nil {
return r.Body
}
return out.String()
}