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