-
Notifications
You must be signed in to change notification settings - Fork 8
/
encode_decode.go
184 lines (174 loc) · 5.41 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
// Code generated by goa v3.0.10, DO NOT EDIT.
//
// pipeline HTTP client encoders and decoders
//
// Command:
// $ goa gen github.com/artefactual-labs/enduro/internal/api/design -o
// internal/api
package client
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
pipeline "github.com/artefactual-labs/enduro/internal/api/gen/pipeline"
pipelineviews "github.com/artefactual-labs/enduro/internal/api/gen/pipeline/views"
goahttp "goa.design/goa/v3/http"
goa "goa.design/goa/v3/pkg"
)
// BuildListRequest instantiates a HTTP request object with method and path set
// to call the "pipeline" service "list" endpoint
func (c *Client) BuildListRequest(ctx context.Context, v interface{}) (*http.Request, error) {
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ListPipelinePath()}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("pipeline", "list", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// EncodeListRequest returns an encoder for requests sent to the pipeline list
// server.
func EncodeListRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Request, interface{}) error {
return func(req *http.Request, v interface{}) error {
p, ok := v.(*pipeline.ListPayload)
if !ok {
return goahttp.ErrInvalidType("pipeline", "list", "*pipeline.ListPayload", v)
}
values := req.URL.Query()
if p.Name != nil {
values.Add("name", *p.Name)
}
req.URL.RawQuery = values.Encode()
return nil
}
}
// DecodeListResponse returns a decoder for responses returned by the pipeline
// list endpoint. restoreBody controls whether the response body should be
// restored after having been read.
func DecodeListResponse(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 ListResponseBody
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("pipeline", "list", err)
}
for _, e := range body {
if e != nil {
if err2 := ValidateEnduroStoredPipelineResponse(e); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
if err != nil {
return nil, goahttp.ErrValidationError("pipeline", "list", err)
}
res := NewListEnduroStoredPipelineOK(body)
return res, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("pipeline", "list", resp.StatusCode, string(body))
}
}
}
// BuildShowRequest instantiates a HTTP request object with method and path set
// to call the "pipeline" service "show" endpoint
func (c *Client) BuildShowRequest(ctx context.Context, v interface{}) (*http.Request, error) {
var (
id string
)
{
p, ok := v.(*pipeline.ShowPayload)
if !ok {
return nil, goahttp.ErrInvalidType("pipeline", "show", "*pipeline.ShowPayload", v)
}
id = p.ID
}
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ShowPipelinePath(id)}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("pipeline", "show", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// DecodeShowResponse returns a decoder for responses returned by the pipeline
// show endpoint. restoreBody controls whether the response body should be
// restored after having been read.
// DecodeShowResponse may return the following errors:
// - "not_found" (type *pipeline.NotFound): http.StatusNotFound
// - error: internal error
func DecodeShowResponse(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 ShowResponseBody
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("pipeline", "show", err)
}
p := NewShowEnduroStoredPipelineOK(&body)
view := "default"
vres := &pipelineviews.EnduroStoredPipeline{Projected: p, View: view}
if err = pipelineviews.ValidateEnduroStoredPipeline(vres); err != nil {
return nil, goahttp.ErrValidationError("pipeline", "show", err)
}
res := pipeline.NewEnduroStoredPipeline(vres)
return res, nil
case http.StatusNotFound:
var (
body ShowNotFoundResponseBody
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("pipeline", "show", err)
}
err = ValidateShowNotFoundResponseBody(&body)
if err != nil {
return nil, goahttp.ErrValidationError("pipeline", "show", err)
}
return nil, NewShowNotFound(&body)
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("pipeline", "show", resp.StatusCode, string(body))
}
}
}