-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
138 lines (120 loc) · 4.1 KB
/
response.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package go_requests
import (
"encoding"
"encoding/json"
"encoding/xml"
"net/http"
"strings"
"gopkg.in/yaml.v2"
)
// ContentType of the response to determine the unmarshal method.
type ContentType string
const (
// jsonContentType is the content type for json.
jsonContentType ContentType = "application/json"
// xmlContentType is the content type for xml.
xmlContentType ContentType = "application/xml"
// yamlContentType is the content type for yaml.
yamlContentType ContentType = "application/yaml"
// textContentType is the content type for text.
textContentType ContentType = "text/plain"
// noneContentType is the content type for none.
noneContentType ContentType = ""
)
// Response is the struct that holds the response of the HTTP request.
//
// - It contains the HTTP status code, HTTP header, HTTP status, and response body.
// - The response body is in []byte format.
// - The response body can be unmarshaled into a struct using the unmarshalJSON, unmarshalXML, or UnmarshalYAML methods.
// - The response body can be converted into a string using the String method.
// - The response body can be converted into a []byte using the Body method.
// - The HTTP status code can be retrieved using the StatusCode method.
// - The HTTP header can be retrieved using the Header method.
// - The HTTP status can be retrieved using the Status method.
type Response struct {
statusCode int
status string
header http.Header
body []byte
contentType string
}
// StatusCode returns the HTTP status code of the response.
func (r *Response) StatusCode() int {
return r.statusCode
}
// Header returns the HTTP header of the response.
func (r *Response) Header() http.Header {
return r.header
}
// Bytes returns the response body in []byte format.
func (r *Response) Bytes() []byte {
return r.body
}
// Status returns the HTTP status of the response.
func (r *Response) Status() string {
return r.status
}
// String returns a response body in string format.
func (r *Response) String() string {
return string(r.body)
}
// getContentType returns the content-type of the response. It returns an empty string if the content-type is not set.
func (r *Response) getContentType() ContentType {
if strings.Contains(r.contentType, "application/json") {
return jsonContentType
}
if strings.Contains(r.contentType, "application/xml") {
return xmlContentType
}
if strings.Contains(r.contentType, "application/yaml") {
return yamlContentType
}
if strings.Contains(r.contentType, "text/plain") {
return textContentType
}
return noneContentType
}
// unmarshalJSON unmarshal the response body into the given interface.
func (r *Response) unmarshalJSON(v interface{}) error {
return json.Unmarshal(r.body, &v)
}
// unmarshalXML unmarshal the response body into the given interface.
func (r *Response) unmarshalXML(v interface{}) error {
return xml.Unmarshal(r.body, &v)
}
// UnmarshalYAML unmarshal the response body into the given interface.
func (r *Response) UnmarshalYAML(v interface{}) error {
return yaml.Unmarshal(r.body, &v)
}
// unmarshalText unmarshal the response body into the given interface.
func (r *Response) unmarshalText(v interface{}) error {
if p, ok := v.(encoding.TextUnmarshaler); ok {
return p.UnmarshalText(r.body)
}
return UnsupportedContentType()
}
// Unmarshal the response body into the given interface.
// - It uses the content-type of the response to determine the unmarshal method.
// - It supports json, xml, and yaml.
// - It returns an error if the content-type is not supported.
// - It returns an error if the unmarshal method fails.
// - It returns an error if the given interface is not a pointer.
func (r *Response) Unmarshal(v interface{}) ErrorContentType {
switch r.getContentType() {
case jsonContentType:
return r.unmarshalJSON(v)
case xmlContentType:
return r.unmarshalXML(v)
case yamlContentType:
return r.UnmarshalYAML(v)
case textContentType:
return r.unmarshalText(v)
case noneContentType:
return NoContentType()
default:
return UnsupportedContentType()
}
}
func (r *Response) ContentType() ContentType {
return r.getContentType()
}