/
encode_decode.go
270 lines (255 loc) · 7.73 KB
/
encode_decode.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Code generated by goa v2.2.4, DO NOT EDIT.
//
// text HTTP client encoders and decoders
//
// Command:
// $ goa gen goa.design/examples/encodings/design -o
// $(GOPATH)/src/goa.design/examples/encodings
package client
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
text "goa.design/examples/encodings/gen/text"
goahttp "goa.design/goa/http"
)
// BuildConcatstringsRequest instantiates a HTTP request object with method and
// path set to call the "text" service "concatstrings" endpoint
func (c *Client) BuildConcatstringsRequest(ctx context.Context, v interface{}) (*http.Request, error) {
var (
a string
b string
)
{
p, ok := v.(*text.ConcatstringsPayload)
if !ok {
return nil, goahttp.ErrInvalidType("text", "concatstrings", "*text.ConcatstringsPayload", v)
}
a = p.A
b = p.B
}
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ConcatstringsTextPath(a, b)}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("text", "concatstrings", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// DecodeConcatstringsResponse returns a decoder for responses returned by the
// text concatstrings endpoint. restoreBody controls whether the response body
// should be restored after having been read.
func DecodeConcatstringsResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
return func(resp *http.Response) (interface{}, error) {
if restoreBody {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body string
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("text", "concatstrings", err)
}
return body, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("text", "concatstrings", resp.StatusCode, string(body))
}
}
}
// BuildConcatbytesRequest instantiates a HTTP request object with method and
// path set to call the "text" service "concatbytes" endpoint
func (c *Client) BuildConcatbytesRequest(ctx context.Context, v interface{}) (*http.Request, error) {
var (
a string
b string
)
{
p, ok := v.(*text.ConcatbytesPayload)
if !ok {
return nil, goahttp.ErrInvalidType("text", "concatbytes", "*text.ConcatbytesPayload", v)
}
a = p.A
b = p.B
}
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ConcatbytesTextPath(a, b)}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("text", "concatbytes", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// DecodeConcatbytesResponse returns a decoder for responses returned by the
// text concatbytes endpoint. restoreBody controls whether the response body
// should be restored after having been read.
func DecodeConcatbytesResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
return func(resp *http.Response) (interface{}, error) {
if restoreBody {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body []byte
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("text", "concatbytes", err)
}
return body, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("text", "concatbytes", resp.StatusCode, string(body))
}
}
}
// BuildConcatstringfieldRequest instantiates a HTTP request object with method
// and path set to call the "text" service "concatstringfield" endpoint
func (c *Client) BuildConcatstringfieldRequest(ctx context.Context, v interface{}) (*http.Request, error) {
var (
a string
b string
)
{
p, ok := v.(*text.ConcatstringfieldPayload)
if !ok {
return nil, goahttp.ErrInvalidType("text", "concatstringfield", "*text.ConcatstringfieldPayload", v)
}
a = p.A
b = p.B
}
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ConcatstringfieldTextPath(a, b)}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("text", "concatstringfield", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// DecodeConcatstringfieldResponse returns a decoder for responses returned by
// the text concatstringfield endpoint. restoreBody controls whether the
// response body should be restored after having been read.
func DecodeConcatstringfieldResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
return func(resp *http.Response) (interface{}, error) {
if restoreBody {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body string
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("text", "concatstringfield", err)
}
res := NewConcatstringfieldMyConcatenationOK(body)
return res, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("text", "concatstringfield", resp.StatusCode, string(body))
}
}
}
// BuildConcatbytesfieldRequest instantiates a HTTP request object with method
// and path set to call the "text" service "concatbytesfield" endpoint
func (c *Client) BuildConcatbytesfieldRequest(ctx context.Context, v interface{}) (*http.Request, error) {
var (
a string
b string
)
{
p, ok := v.(*text.ConcatbytesfieldPayload)
if !ok {
return nil, goahttp.ErrInvalidType("text", "concatbytesfield", "*text.ConcatbytesfieldPayload", v)
}
a = p.A
b = p.B
}
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ConcatbytesfieldTextPath(a, b)}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("text", "concatbytesfield", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// DecodeConcatbytesfieldResponse returns a decoder for responses returned by
// the text concatbytesfield endpoint. restoreBody controls whether the
// response body should be restored after having been read.
func DecodeConcatbytesfieldResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
return func(resp *http.Response) (interface{}, error) {
if restoreBody {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body []byte
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("text", "concatbytesfield", err)
}
res := NewConcatbytesfieldMyConcatenationOK(body)
return res, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("text", "concatbytesfield", resp.StatusCode, string(body))
}
}
}