/
body.go
72 lines (58 loc) · 1.27 KB
/
body.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
package xhttpclient
import (
"io"
"net/http"
)
const (
ContentTypeValueJSON = "application/json; charset=utf-8"
ContentTypeValueFormUrlencoded = "application/x-www-form-urlencoded"
)
type BodyCodec interface {
Get() BodyCodec
Put(bc BodyCodec)
Encode(body any) (io.Reader, error)
Decode(r io.Reader, v any) error
}
type (
BodyCodecOnSend interface {
OnSend(req *http.Request)
}
BodyCodecOnReceive interface {
OnReceive(req *http.Request, resp *http.Response)
}
BodyCodecResponseIsSuccessful interface {
IsSuccessful(resp *http.Response) bool
}
BodyCodecResponseIsWrong interface {
IsWrong(resp *http.Response) bool
}
BodyCodecDecodeWrong interface {
DecodeWrong(r io.Reader, v any) error
}
)
type (
BodyHeaderContentLength interface {
ContentLength() int
}
BodyHeaderContentType interface {
ContentType() string
}
BodyHeaderContentEncoding interface {
ContentEncoding() string
}
)
type (
BodyHeaderAccept interface {
Accept() string
}
BodyHeaderAcceptEncoding interface {
AcceptEncoding() string
}
)
func ResponseIsSuccessfulGTE200LTE299(resp *http.Response) bool {
code := resp.StatusCode
return 200 <= code && code <= 299
}
func BodyCodecResponseIsWrongGTE400(resp *http.Response) bool {
return resp.StatusCode >= 400
}