-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
store_response.go
155 lines (129 loc) · 4.45 KB
/
store_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package hit
import (
"io"
"io/ioutil"
"github.com/Eun/go-hit/internal/converter"
)
// IStoreResponse defines the functions that can be used to store data from the http response.
type IStoreResponse interface {
// Status stores the Response's Status
Status() IStoreStep
// Status stores the Response's StatusCode
StatusCode() IStoreStep
// Proto stores the Response's Proto
Proto() IStoreStep
// ProtoMajor stores the Response's ProtoMajor
ProtoMajor() IStoreStep
// ProtoMinor stores the Response's ProtoMinor
ProtoMinor() IStoreStep
// ContentLength stores the Response's ContentLength
ContentLength() IStoreStep
// TransferEncoding stores the Response's TransferEncoding
//
// The argument can be used to narrow down the store path
TransferEncoding() IStoreStep
// Header stores the Response's Header(s)
//
// If you specify the argument you can directly store the header value
//
// Usage:
// var headers http.Header
// Store().Response().Headers().In(&headers)
//
// var contentType string
// Store().Response().Headers("Content-Type").In(&contentType)
Headers(headerName ...string) IStoreStep
// Trailer stores the Response's Trailer(s)
//
// If you specify the argument you can directly store the trailer value
//
// Usage:
// var trailers http.Header
// Store().Response().Trailers().In(&trailers)
//
// var contentType string
// Store().Response().Trailers("Content-Type").In(&contentType)
Trailers(trailerName ...string) IStoreStep
// Body stores the Response's Body
//
// given the following body: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// Usage:
// var body string
// Store().Response().Body().String().In(&body) // store the whole body as string
// var name string
// Store().Response().Body().JSON().JQ(".Name").In(&name) // store "Joe" in name
Body() IStoreBody
// Uncompressed stores the Response's Uncompressed status
Uncompressed() IStoreStep
}
type storeResponse struct{}
func newStoreResponse() *storeResponse {
return &storeResponse{}
}
func (d *storeResponse) Status() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().Status, v)
})
}
func (d *storeResponse) StatusCode() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().StatusCode, v)
})
}
func (d *storeResponse) Proto() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().Proto, v)
})
}
func (d *storeResponse) ProtoMajor() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().ProtoMajor, v)
})
}
func (d *storeResponse) ProtoMinor() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().ProtoMinor, v)
})
}
func (d *storeResponse) ContentLength() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().ContentLength, v)
})
}
func (d *storeResponse) TransferEncoding() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().TransferEncoding, v)
})
}
func (d *storeResponse) Headers(headerName ...string) IStoreStep {
if header, ok := getLastStringArgument(headerName); ok {
return newStoreStep(func(hit Hit, v interface{}) error {
return storeStringSlice(hit.Response().Header.Values(header), v)
})
}
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().Header, v)
})
}
func (d *storeResponse) Trailers(trailerName ...string) IStoreStep {
if trailer, ok := getLastStringArgument(trailerName); ok {
return newStoreStep(func(hit Hit, v interface{}) error {
// we have to read the body to get the trailers
_, _ = io.Copy(ioutil.Discard, hit.Response().Body().Reader())
return storeStringSlice(hit.Response().Trailer.Values(trailer), v)
})
}
return newStoreStep(func(hit Hit, v interface{}) error {
// we have to read the body to get the trailers
_, _ = io.Copy(ioutil.Discard, hit.Response().Body().Reader())
return converter.Convert(hit.Response().Trailer, v)
})
}
func (d *storeResponse) Body() IStoreBody {
return newStoreBody(storeBodyResponse)
}
func (d *storeResponse) Uncompressed() IStoreStep {
return newStoreStep(func(hit Hit, v interface{}) error {
return converter.Convert(hit.Response().Uncompressed, v)
})
}