This repository has been archived by the owner on May 17, 2024. It is now read-only.
generated from findy-network/findy-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_schema.go
106 lines (91 loc) · 2.86 KB
/
api_schema.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
/*
* Aries Agent Test Harness Backchannel API
*
* This page documents the backchannel API the test harness uses to communicate with agents under tests. For more information checkout the [Aries Interoperability Information](http://aries-interop.info) page.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// SchemaApiController binds http requests to an api service and writes the service results to the http response
type SchemaApiController struct {
service SchemaApiServicer
errorHandler ErrorHandler
}
// SchemaApiOption for how the controller is set up.
type SchemaApiOption func(*SchemaApiController)
// WithSchemaApiErrorHandler inject ErrorHandler into controller
func WithSchemaApiErrorHandler(h ErrorHandler) SchemaApiOption {
return func(c *SchemaApiController) {
c.errorHandler = h
}
}
// NewSchemaApiController creates a default api controller
func NewSchemaApiController(s SchemaApiServicer, opts ...SchemaApiOption) Router {
controller := &SchemaApiController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the SchemaApiController
func (c *SchemaApiController) Routes() Routes {
return Routes{
{
"SchemaCreate",
strings.ToUpper("Post"),
"/agent/command/{schema:schema\\/?}",
c.SchemaCreate,
},
{
"SchemaGetById",
strings.ToUpper("Get"),
"/agent/command/schema/{schemaId}",
c.SchemaGetById,
},
}
}
// SchemaCreate - Create a new schema
func (c *SchemaApiController) SchemaCreate(w http.ResponseWriter, r *http.Request) {
schemaCreateRequestParam := SchemaCreateRequest{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&schemaCreateRequestParam); err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertSchemaCreateRequestRequired(schemaCreateRequestParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.SchemaCreate(r.Context(), schemaCreateRequestParam)
// 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, w)
}
// SchemaGetById - Get schema by id
func (c *SchemaApiController) SchemaGetById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
schemaIdParam := params["schemaId"]
result, err := c.service.SchemaGetById(r.Context(), schemaIdParam)
// 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, w)
}