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