github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/examples/auto-configure/restapi/operations/a_to_do_list_application_api.go (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 package operations 4 5 // This file was generated by the swagger tool. 6 // Editing this file might prove futile when you re-run the swagger generate command 7 8 import ( 9 "fmt" 10 "net/http" 11 "strings" 12 13 "github.com/go-openapi/errors" 14 "github.com/go-openapi/loads" 15 "github.com/go-openapi/runtime" 16 "github.com/go-openapi/runtime/middleware" 17 "github.com/go-openapi/runtime/security" 18 "github.com/go-openapi/spec" 19 "github.com/go-openapi/strfmt" 20 "github.com/go-openapi/swag" 21 22 "github.com/go-swagger/go-swagger/examples/auto-configure/restapi/operations/todos" 23 ) 24 25 // NewAToDoListApplicationAPI creates a new AToDoListApplication instance 26 func NewAToDoListApplicationAPI(spec *loads.Document) *AToDoListApplicationAPI { 27 return &AToDoListApplicationAPI{ 28 handlers: make(map[string]map[string]http.Handler), 29 formats: strfmt.Default, 30 defaultConsumes: "application/json", 31 defaultProduces: "application/json", 32 customConsumers: make(map[string]runtime.Consumer), 33 customProducers: make(map[string]runtime.Producer), 34 PreServerShutdown: func() {}, 35 ServerShutdown: func() {}, 36 spec: spec, 37 useSwaggerUI: false, 38 ServeError: errors.ServeError, 39 BasicAuthenticator: security.BasicAuth, 40 APIKeyAuthenticator: security.APIKeyAuth, 41 BearerAuthenticator: security.BearerAuth, 42 43 JSONConsumer: runtime.JSONConsumer(), 44 45 JSONProducer: runtime.JSONProducer(), 46 47 TodosAddOneHandler: todos.AddOneHandlerFunc(func(params todos.AddOneParams, principal interface{}) middleware.Responder { 48 return middleware.NotImplemented("operation todos.AddOne has not yet been implemented") 49 }), 50 TodosDestroyOneHandler: todos.DestroyOneHandlerFunc(func(params todos.DestroyOneParams, principal interface{}) middleware.Responder { 51 return middleware.NotImplemented("operation todos.DestroyOne has not yet been implemented") 52 }), 53 TodosFindTodosHandler: todos.FindTodosHandlerFunc(func(params todos.FindTodosParams, principal interface{}) middleware.Responder { 54 return middleware.NotImplemented("operation todos.FindTodos has not yet been implemented") 55 }), 56 TodosUpdateOneHandler: todos.UpdateOneHandlerFunc(func(params todos.UpdateOneParams, principal interface{}) middleware.Responder { 57 return middleware.NotImplemented("operation todos.UpdateOne has not yet been implemented") 58 }), 59 60 // Applies when the "x-todolist-token" header is set 61 KeyAuth: func(token string) (interface{}, error) { 62 return nil, errors.NotImplemented("api key auth (key) x-todolist-token from header param [x-todolist-token] has not yet been implemented") 63 }, 64 // default authorizer is authorized meaning no requests are blocked 65 APIAuthorizer: security.Authorized(), 66 } 67 } 68 69 /*AToDoListApplicationAPI The product of a tutorial on goswagger.io */ 70 type AToDoListApplicationAPI struct { 71 spec *loads.Document 72 context *middleware.Context 73 handlers map[string]map[string]http.Handler 74 formats strfmt.Registry 75 customConsumers map[string]runtime.Consumer 76 customProducers map[string]runtime.Producer 77 defaultConsumes string 78 defaultProduces string 79 Middleware func(middleware.Builder) http.Handler 80 useSwaggerUI bool 81 82 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 83 // It has a default implementation in the security package, however you can replace it for your particular usage. 84 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 85 86 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 87 // It has a default implementation in the security package, however you can replace it for your particular usage. 88 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 89 90 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 91 // It has a default implementation in the security package, however you can replace it for your particular usage. 92 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 93 94 // JSONConsumer registers a consumer for the following mime types: 95 // - application/io.goswagger.examples.todo-list.v1+json 96 JSONConsumer runtime.Consumer 97 98 // JSONProducer registers a producer for the following mime types: 99 // - application/io.goswagger.examples.todo-list.v1+json 100 JSONProducer runtime.Producer 101 102 // KeyAuth registers a function that takes a token and returns a principal 103 // it performs authentication based on an api key x-todolist-token provided in the header 104 KeyAuth func(string) (interface{}, error) 105 106 // APIAuthorizer provides access control (ACL/RBAC/ABAC) by providing access to the request and authenticated principal 107 APIAuthorizer runtime.Authorizer 108 109 // TodosAddOneHandler sets the operation handler for the add one operation 110 TodosAddOneHandler todos.AddOneHandler 111 // TodosDestroyOneHandler sets the operation handler for the destroy one operation 112 TodosDestroyOneHandler todos.DestroyOneHandler 113 // TodosFindTodosHandler sets the operation handler for the find todos operation 114 TodosFindTodosHandler todos.FindTodosHandler 115 // TodosUpdateOneHandler sets the operation handler for the update one operation 116 TodosUpdateOneHandler todos.UpdateOneHandler 117 118 // ServeError is called when an error is received, there is a default handler 119 // but you can set your own with this 120 ServeError func(http.ResponseWriter, *http.Request, error) 121 122 // PreServerShutdown is called before the HTTP(S) server is shutdown 123 // This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic 124 PreServerShutdown func() 125 126 // ServerShutdown is called when the HTTP(S) server is shut down and done 127 // handling all active connections and does not accept connections any more 128 ServerShutdown func() 129 130 // Custom command line argument groups with their descriptions 131 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 132 133 // User defined logger function. 134 Logger func(string, ...interface{}) 135 } 136 137 // UseRedoc for documentation at /docs 138 func (o *AToDoListApplicationAPI) UseRedoc() { 139 o.useSwaggerUI = false 140 } 141 142 // UseSwaggerUI for documentation at /docs 143 func (o *AToDoListApplicationAPI) UseSwaggerUI() { 144 o.useSwaggerUI = true 145 } 146 147 // SetDefaultProduces sets the default produces media type 148 func (o *AToDoListApplicationAPI) SetDefaultProduces(mediaType string) { 149 o.defaultProduces = mediaType 150 } 151 152 // SetDefaultConsumes returns the default consumes media type 153 func (o *AToDoListApplicationAPI) SetDefaultConsumes(mediaType string) { 154 o.defaultConsumes = mediaType 155 } 156 157 // SetSpec sets a spec that will be served for the clients. 158 func (o *AToDoListApplicationAPI) SetSpec(spec *loads.Document) { 159 o.spec = spec 160 } 161 162 // DefaultProduces returns the default produces media type 163 func (o *AToDoListApplicationAPI) DefaultProduces() string { 164 return o.defaultProduces 165 } 166 167 // DefaultConsumes returns the default consumes media type 168 func (o *AToDoListApplicationAPI) DefaultConsumes() string { 169 return o.defaultConsumes 170 } 171 172 // Formats returns the registered string formats 173 func (o *AToDoListApplicationAPI) Formats() strfmt.Registry { 174 return o.formats 175 } 176 177 // RegisterFormat registers a custom format validator 178 func (o *AToDoListApplicationAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 179 o.formats.Add(name, format, validator) 180 } 181 182 // Validate validates the registrations in the AToDoListApplicationAPI 183 func (o *AToDoListApplicationAPI) Validate() error { 184 var unregistered []string 185 186 if o.JSONConsumer == nil { 187 unregistered = append(unregistered, "JSONConsumer") 188 } 189 190 if o.JSONProducer == nil { 191 unregistered = append(unregistered, "JSONProducer") 192 } 193 194 if o.KeyAuth == nil { 195 unregistered = append(unregistered, "XTodolistTokenAuth") 196 } 197 198 if o.TodosAddOneHandler == nil { 199 unregistered = append(unregistered, "todos.AddOneHandler") 200 } 201 if o.TodosDestroyOneHandler == nil { 202 unregistered = append(unregistered, "todos.DestroyOneHandler") 203 } 204 if o.TodosFindTodosHandler == nil { 205 unregistered = append(unregistered, "todos.FindTodosHandler") 206 } 207 if o.TodosUpdateOneHandler == nil { 208 unregistered = append(unregistered, "todos.UpdateOneHandler") 209 } 210 211 if len(unregistered) > 0 { 212 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 213 } 214 215 return nil 216 } 217 218 // ServeErrorFor gets a error handler for a given operation id 219 func (o *AToDoListApplicationAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 220 return o.ServeError 221 } 222 223 // AuthenticatorsFor gets the authenticators for the specified security schemes 224 func (o *AToDoListApplicationAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 225 result := make(map[string]runtime.Authenticator) 226 for name := range schemes { 227 switch name { 228 case "key": 229 scheme := schemes[name] 230 result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.KeyAuth) 231 232 } 233 } 234 return result 235 } 236 237 // Authorizer returns the registered authorizer 238 func (o *AToDoListApplicationAPI) Authorizer() runtime.Authorizer { 239 return o.APIAuthorizer 240 } 241 242 // ConsumersFor gets the consumers for the specified media types. 243 // MIME type parameters are ignored here. 244 func (o *AToDoListApplicationAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 245 result := make(map[string]runtime.Consumer, len(mediaTypes)) 246 for _, mt := range mediaTypes { 247 switch mt { 248 case "application/io.goswagger.examples.todo-list.v1+json": 249 result["application/io.goswagger.examples.todo-list.v1+json"] = o.JSONConsumer 250 } 251 252 if c, ok := o.customConsumers[mt]; ok { 253 result[mt] = c 254 } 255 } 256 return result 257 } 258 259 // ProducersFor gets the producers for the specified media types. 260 // MIME type parameters are ignored here. 261 func (o *AToDoListApplicationAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 262 result := make(map[string]runtime.Producer, len(mediaTypes)) 263 for _, mt := range mediaTypes { 264 switch mt { 265 case "application/io.goswagger.examples.todo-list.v1+json": 266 result["application/io.goswagger.examples.todo-list.v1+json"] = o.JSONProducer 267 } 268 269 if p, ok := o.customProducers[mt]; ok { 270 result[mt] = p 271 } 272 } 273 return result 274 } 275 276 // HandlerFor gets a http.Handler for the provided operation method and path 277 func (o *AToDoListApplicationAPI) HandlerFor(method, path string) (http.Handler, bool) { 278 if o.handlers == nil { 279 return nil, false 280 } 281 um := strings.ToUpper(method) 282 if _, ok := o.handlers[um]; !ok { 283 return nil, false 284 } 285 if path == "/" { 286 path = "" 287 } 288 h, ok := o.handlers[um][path] 289 return h, ok 290 } 291 292 // Context returns the middleware context for the a to do list application API 293 func (o *AToDoListApplicationAPI) Context() *middleware.Context { 294 if o.context == nil { 295 o.context = middleware.NewRoutableContext(o.spec, o, nil) 296 } 297 298 return o.context 299 } 300 301 func (o *AToDoListApplicationAPI) initHandlerCache() { 302 o.Context() // don't care about the result, just that the initialization happened 303 if o.handlers == nil { 304 o.handlers = make(map[string]map[string]http.Handler) 305 } 306 307 if o.handlers["POST"] == nil { 308 o.handlers["POST"] = make(map[string]http.Handler) 309 } 310 o.handlers["POST"][""] = todos.NewAddOne(o.context, o.TodosAddOneHandler) 311 if o.handlers["DELETE"] == nil { 312 o.handlers["DELETE"] = make(map[string]http.Handler) 313 } 314 o.handlers["DELETE"]["/{id}"] = todos.NewDestroyOne(o.context, o.TodosDestroyOneHandler) 315 if o.handlers["GET"] == nil { 316 o.handlers["GET"] = make(map[string]http.Handler) 317 } 318 o.handlers["GET"][""] = todos.NewFindTodos(o.context, o.TodosFindTodosHandler) 319 if o.handlers["PUT"] == nil { 320 o.handlers["PUT"] = make(map[string]http.Handler) 321 } 322 o.handlers["PUT"]["/{id}"] = todos.NewUpdateOne(o.context, o.TodosUpdateOneHandler) 323 } 324 325 // Serve creates a http handler to serve the API over HTTP 326 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 327 func (o *AToDoListApplicationAPI) Serve(builder middleware.Builder) http.Handler { 328 o.Init() 329 330 if o.Middleware != nil { 331 return o.Middleware(builder) 332 } 333 if o.useSwaggerUI { 334 return o.context.APIHandlerSwaggerUI(builder) 335 } 336 return o.context.APIHandler(builder) 337 } 338 339 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 340 func (o *AToDoListApplicationAPI) Init() { 341 if len(o.handlers) == 0 { 342 o.initHandlerCache() 343 } 344 } 345 346 // RegisterConsumer allows you to add (or override) a consumer for a media type. 347 func (o *AToDoListApplicationAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 348 o.customConsumers[mediaType] = consumer 349 } 350 351 // RegisterProducer allows you to add (or override) a producer for a media type. 352 func (o *AToDoListApplicationAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 353 o.customProducers[mediaType] = producer 354 } 355 356 // AddMiddlewareFor adds a http middleware to existing handler 357 func (o *AToDoListApplicationAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) { 358 um := strings.ToUpper(method) 359 if path == "/" { 360 path = "" 361 } 362 o.Init() 363 if h, ok := o.handlers[um][path]; ok { 364 o.handlers[method][path] = builder(h) 365 } 366 }