-
Notifications
You must be signed in to change notification settings - Fork 1
/
condition.go
70 lines (61 loc) · 1.58 KB
/
condition.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
package emit
import (
"errors"
"fmt"
"net/http"
"strings"
)
func IsJSON(response *http.Response) error {
return ist(response, "Json", "application/json")
}
func IsTEXT(response *http.Response) error {
return ist(response, "Text", "text/plain")
}
func IsHTML(response *http.Response) error {
return ist(response, "Text", "text/html")
}
func IsSTREAM(response *http.Response) error {
return ist(response, "Stream", "text/event-stream", "application/stream")
}
func IsPROTO(response *http.Response) error {
return ist(response, "Proto", "application/connect+proto")
}
func Status(status int) func(response *http.Response) error {
return func(response *http.Response) error {
if response == nil {
return Error{-1, "Status", "", errors.New("response is nil")}
}
if response.StatusCode != status {
msg := ""
if isJ(response.Header) {
msg = TextResponse(response)
}
_ = response.Body.Close()
return Error{response.StatusCode, "Status", msg, errors.New(response.Status)}
}
return nil
}
}
func ist(response *http.Response, bus string, ts ...string) error {
if response == nil {
return Error{-1, bus, "", errors.New("response is nil")}
}
h := response.Header
for _, t := range ts {
if strings.Contains(h.Get("Content-Type"), t) {
return nil
}
}
msg := ""
if isJ(response.Header) {
msg = TextResponse(response)
}
_ = response.Body.Close()
return Error{-1, bus, msg, fmt.Errorf("response is not [ %s ]", ts)}
}
func isJ(header http.Header) bool {
if header == nil {
return false
}
return strings.Contains(header.Get("Content-Type"), "application/json")
}