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