-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinternal_response.go
More file actions
43 lines (34 loc) · 908 Bytes
/
internal_response.go
File metadata and controls
43 lines (34 loc) · 908 Bytes
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
package httpclient
import (
"encoding/json"
"gopkg.in/h2non/gentleman.v2"
)
// Internal Response is the only way to let ReadContent be testable
type internalResponse struct {
gentlemanResponse *gentleman.Response
bytes []byte
}
func buildInternalResponse(resp *gentleman.Response) *internalResponse {
return &internalResponse{
gentlemanResponse: resp,
bytes: nil,
}
}
func (ir *internalResponse) StatusCode() int {
return ir.gentlemanResponse.StatusCode
}
func (ir *internalResponse) GetHeader(name string) string {
return ir.gentlemanResponse.Header.Get(name)
}
func (ir *internalResponse) Bytes() []byte {
if ir.bytes == nil {
ir.bytes = ir.gentlemanResponse.Bytes()
}
return ir.bytes
}
func (ir *internalResponse) JSON(data any) error {
return json.Unmarshal(ir.Bytes(), data)
}
func (ir *internalResponse) String() string {
return string(ir.Bytes())
}