-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathapi_just_api.go
More file actions
102 lines (88 loc) · 2.58 KB
/
api_just_api.go
File metadata and controls
102 lines (88 loc) · 2.58 KB
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
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* optional body
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 1.0.1
*/
package petstoreserver
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
)
// JustApiAPIController binds http requests to an api service and writes the service results to the http response
type JustApiAPIController struct {
service JustApiAPIServicer
errorHandler ErrorHandler
}
// JustApiAPIOption for how the controller is set up.
type JustApiAPIOption func(*JustApiAPIController)
// WithJustApiAPIErrorHandler inject ErrorHandler into controller
func WithJustApiAPIErrorHandler(h ErrorHandler) JustApiAPIOption {
return func(c *JustApiAPIController) {
c.errorHandler = h
}
}
// NewJustApiAPIController creates a default api controller
func NewJustApiAPIController(s JustApiAPIServicer, opts ...JustApiAPIOption) *JustApiAPIController {
controller := &JustApiAPIController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the JustApiAPIController
func (c *JustApiAPIController) Routes() Routes {
return Routes{
"SendOptionalPayload": Route{
"SendOptionalPayload",
strings.ToUpper("Post"),
"/api/v1/silly",
c.SendOptionalPayload,
},
}
}
// OrderedRoutes returns all the api routes in a deterministic order for the JustApiAPIController
func (c *JustApiAPIController) OrderedRoutes() []Route {
return []Route{
Route{
"SendOptionalPayload",
strings.ToUpper("Post"),
"/api/v1/silly",
c.SendOptionalPayload,
},
}
}
// SendOptionalPayload -
func (c *JustApiAPIController) SendOptionalPayload(w http.ResponseWriter, r *http.Request) {
var payloadParam Payload
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&payloadParam); err != nil && !errors.Is(err, io.EOF) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertPayloadRequired(payloadParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
if err := AssertPayloadConstraints(payloadParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.SendOptionalPayload(r.Context(), payloadParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
_ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}