1
9
10 package openapi
11
12 import (
13 "encoding/json"
14 "net/http"
15 "strings"
16 )
17
18
19 type DefaultApiController struct {
20 service DefaultApiServicer
21 errorHandler ErrorHandler
22 }
23
24
25 type DefaultApiOption func(*DefaultApiController)
26
27
28 func WithDefaultApiErrorHandler(h ErrorHandler) DefaultApiOption {
29 return func(c *DefaultApiController) {
30 c.errorHandler = h
31 }
32 }
33
34
35 func NewDefaultApiController(s DefaultApiServicer, opts ...DefaultApiOption) Router {
36 controller := &DefaultApiController{
37 service: s,
38 errorHandler: DefaultErrorHandler,
39 }
40
41 for _, opt := range opts {
42 opt(controller)
43 }
44
45 return controller
46 }
47
48
49 func (c *DefaultApiController) Routes() Routes {
50 return Routes{
51 {
52 "ComponentCommandPost",
53 strings.ToUpper("Post"),
54 "/api/v1/component/command",
55 c.ComponentCommandPost,
56 },
57 {
58 "ComponentGet",
59 strings.ToUpper("Get"),
60 "/api/v1/component",
61 c.ComponentGet,
62 },
63 {
64 "DevfileGet",
65 strings.ToUpper("Get"),
66 "/api/v1/devfile",
67 c.DevfileGet,
68 },
69 {
70 "DevfilePut",
71 strings.ToUpper("Put"),
72 "/api/v1/devfile",
73 c.DevfilePut,
74 },
75 {
76 "InstanceDelete",
77 strings.ToUpper("Delete"),
78 "/api/v1/instance",
79 c.InstanceDelete,
80 },
81 {
82 "InstanceGet",
83 strings.ToUpper("Get"),
84 "/api/v1/instance",
85 c.InstanceGet,
86 },
87 {
88 "TelemetryGet",
89 strings.ToUpper("Get"),
90 "/api/v1/telemetry",
91 c.TelemetryGet,
92 },
93 }
94 }
95
96
97 func (c *DefaultApiController) ComponentCommandPost(w http.ResponseWriter, r *http.Request) {
98 componentCommandPostRequestParam := ComponentCommandPostRequest{}
99 d := json.NewDecoder(r.Body)
100 d.DisallowUnknownFields()
101 if err := d.Decode(&componentCommandPostRequestParam); err != nil {
102 c.errorHandler(w, r, &ParsingError{Err: err}, nil)
103 return
104 }
105 if err := AssertComponentCommandPostRequestRequired(componentCommandPostRequestParam); err != nil {
106 c.errorHandler(w, r, err, nil)
107 return
108 }
109 result, err := c.service.ComponentCommandPost(r.Context(), componentCommandPostRequestParam)
110
111 if err != nil {
112 c.errorHandler(w, r, err, &result)
113 return
114 }
115
116 EncodeJSONResponse(result.Body, &result.Code, w)
117
118 }
119
120
121 func (c *DefaultApiController) ComponentGet(w http.ResponseWriter, r *http.Request) {
122 result, err := c.service.ComponentGet(r.Context())
123
124 if err != nil {
125 c.errorHandler(w, r, err, &result)
126 return
127 }
128
129 EncodeJSONResponse(result.Body, &result.Code, w)
130
131 }
132
133
134 func (c *DefaultApiController) DevfileGet(w http.ResponseWriter, r *http.Request) {
135 result, err := c.service.DevfileGet(r.Context())
136
137 if err != nil {
138 c.errorHandler(w, r, err, &result)
139 return
140 }
141
142 EncodeJSONResponse(result.Body, &result.Code, w)
143
144 }
145
146
147 func (c *DefaultApiController) DevfilePut(w http.ResponseWriter, r *http.Request) {
148 devfilePutRequestParam := DevfilePutRequest{}
149 d := json.NewDecoder(r.Body)
150 d.DisallowUnknownFields()
151 if err := d.Decode(&devfilePutRequestParam); err != nil {
152 c.errorHandler(w, r, &ParsingError{Err: err}, nil)
153 return
154 }
155 if err := AssertDevfilePutRequestRequired(devfilePutRequestParam); err != nil {
156 c.errorHandler(w, r, err, nil)
157 return
158 }
159 result, err := c.service.DevfilePut(r.Context(), devfilePutRequestParam)
160
161 if err != nil {
162 c.errorHandler(w, r, err, &result)
163 return
164 }
165
166 EncodeJSONResponse(result.Body, &result.Code, w)
167
168 }
169
170
171 func (c *DefaultApiController) InstanceDelete(w http.ResponseWriter, r *http.Request) {
172 result, err := c.service.InstanceDelete(r.Context())
173
174 if err != nil {
175 c.errorHandler(w, r, err, &result)
176 return
177 }
178
179 EncodeJSONResponse(result.Body, &result.Code, w)
180
181 }
182
183
184 func (c *DefaultApiController) InstanceGet(w http.ResponseWriter, r *http.Request) {
185 result, err := c.service.InstanceGet(r.Context())
186
187 if err != nil {
188 c.errorHandler(w, r, err, &result)
189 return
190 }
191
192 EncodeJSONResponse(result.Body, &result.Code, w)
193
194 }
195
196
197 func (c *DefaultApiController) TelemetryGet(w http.ResponseWriter, r *http.Request) {
198 result, err := c.service.TelemetryGet(r.Context())
199
200 if err != nil {
201 c.errorHandler(w, r, err, &result)
202 return
203 }
204
205 EncodeJSONResponse(result.Body, &result.Code, w)
206
207 }
208
View as plain text