github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/examples/task-tracker/restapi/operations/task_tracker_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/task-tracker/restapi/operations/tasks" 23 ) 24 25 // NewTaskTrackerAPI creates a new TaskTracker instance 26 func NewTaskTrackerAPI(spec *loads.Document) *TaskTrackerAPI { 27 return &TaskTrackerAPI{ 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 MultipartformConsumer: runtime.DiscardConsumer, 45 46 JSONProducer: runtime.JSONProducer(), 47 48 TasksAddCommentToTaskHandler: tasks.AddCommentToTaskHandlerFunc(func(params tasks.AddCommentToTaskParams, principal interface{}) middleware.Responder { 49 return middleware.NotImplemented("operation tasks.AddCommentToTask has not yet been implemented") 50 }), 51 TasksCreateTaskHandler: tasks.CreateTaskHandlerFunc(func(params tasks.CreateTaskParams, principal interface{}) middleware.Responder { 52 return middleware.NotImplemented("operation tasks.CreateTask has not yet been implemented") 53 }), 54 TasksDeleteTaskHandler: tasks.DeleteTaskHandlerFunc(func(params tasks.DeleteTaskParams, principal interface{}) middleware.Responder { 55 return middleware.NotImplemented("operation tasks.DeleteTask has not yet been implemented") 56 }), 57 TasksGetTaskCommentsHandler: tasks.GetTaskCommentsHandlerFunc(func(params tasks.GetTaskCommentsParams) middleware.Responder { 58 return middleware.NotImplemented("operation tasks.GetTaskComments has not yet been implemented") 59 }), 60 TasksGetTaskDetailsHandler: tasks.GetTaskDetailsHandlerFunc(func(params tasks.GetTaskDetailsParams) middleware.Responder { 61 return middleware.NotImplemented("operation tasks.GetTaskDetails has not yet been implemented") 62 }), 63 TasksListTasksHandler: tasks.ListTasksHandlerFunc(func(params tasks.ListTasksParams) middleware.Responder { 64 return middleware.NotImplemented("operation tasks.ListTasks has not yet been implemented") 65 }), 66 TasksUpdateTaskHandler: tasks.UpdateTaskHandlerFunc(func(params tasks.UpdateTaskParams, principal interface{}) middleware.Responder { 67 return middleware.NotImplemented("operation tasks.UpdateTask has not yet been implemented") 68 }), 69 TasksUploadTaskFileHandler: tasks.UploadTaskFileHandlerFunc(func(params tasks.UploadTaskFileParams, principal interface{}) middleware.Responder { 70 return middleware.NotImplemented("operation tasks.UploadTaskFile has not yet been implemented") 71 }), 72 73 // Applies when the "token" query is set 74 APIKeyAuth: func(token string) (interface{}, error) { 75 return nil, errors.NotImplemented("api key auth (api_key) token from query param [token] has not yet been implemented") 76 }, 77 // Applies when the "X-Token" header is set 78 TokenHeaderAuth: func(token string) (interface{}, error) { 79 return nil, errors.NotImplemented("api key auth (token_header) X-Token from header param [X-Token] has not yet been implemented") 80 }, 81 // default authorizer is authorized meaning no requests are blocked 82 APIAuthorizer: security.Authorized(), 83 } 84 } 85 86 /* 87 TaskTrackerAPI This application implements a very simple issue tracker. 88 It's implemented as an API which is described by this swagger spec document. 89 90 The go-swagger project uses this specification to test the code generation. 91 This document contains all possible values for a swagger definition. 92 This means that it exercises the framework relatively well. 93 */ 94 type TaskTrackerAPI struct { 95 spec *loads.Document 96 context *middleware.Context 97 handlers map[string]map[string]http.Handler 98 formats strfmt.Registry 99 customConsumers map[string]runtime.Consumer 100 customProducers map[string]runtime.Producer 101 defaultConsumes string 102 defaultProduces string 103 Middleware func(middleware.Builder) http.Handler 104 useSwaggerUI bool 105 106 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 107 // It has a default implementation in the security package, however you can replace it for your particular usage. 108 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 109 110 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 111 // It has a default implementation in the security package, however you can replace it for your particular usage. 112 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 113 114 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 115 // It has a default implementation in the security package, however you can replace it for your particular usage. 116 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 117 118 // JSONConsumer registers a consumer for the following mime types: 119 // - application/vnd.goswagger.examples.task-tracker.v1+json 120 JSONConsumer runtime.Consumer 121 // MultipartformConsumer registers a consumer for the following mime types: 122 // - multipart/form-data 123 MultipartformConsumer runtime.Consumer 124 125 // JSONProducer registers a producer for the following mime types: 126 // - application/vnd.goswagger.examples.task-tracker.v1+json 127 JSONProducer runtime.Producer 128 129 // APIKeyAuth registers a function that takes a token and returns a principal 130 // it performs authentication based on an api key token provided in the query 131 APIKeyAuth func(string) (interface{}, error) 132 133 // TokenHeaderAuth registers a function that takes a token and returns a principal 134 // it performs authentication based on an api key X-Token provided in the header 135 TokenHeaderAuth func(string) (interface{}, error) 136 137 // APIAuthorizer provides access control (ACL/RBAC/ABAC) by providing access to the request and authenticated principal 138 APIAuthorizer runtime.Authorizer 139 140 // TasksAddCommentToTaskHandler sets the operation handler for the add comment to task operation 141 TasksAddCommentToTaskHandler tasks.AddCommentToTaskHandler 142 // TasksCreateTaskHandler sets the operation handler for the create task operation 143 TasksCreateTaskHandler tasks.CreateTaskHandler 144 // TasksDeleteTaskHandler sets the operation handler for the delete task operation 145 TasksDeleteTaskHandler tasks.DeleteTaskHandler 146 // TasksGetTaskCommentsHandler sets the operation handler for the get task comments operation 147 TasksGetTaskCommentsHandler tasks.GetTaskCommentsHandler 148 // TasksGetTaskDetailsHandler sets the operation handler for the get task details operation 149 TasksGetTaskDetailsHandler tasks.GetTaskDetailsHandler 150 // TasksListTasksHandler sets the operation handler for the list tasks operation 151 TasksListTasksHandler tasks.ListTasksHandler 152 // TasksUpdateTaskHandler sets the operation handler for the update task operation 153 TasksUpdateTaskHandler tasks.UpdateTaskHandler 154 // TasksUploadTaskFileHandler sets the operation handler for the upload task file operation 155 TasksUploadTaskFileHandler tasks.UploadTaskFileHandler 156 157 // ServeError is called when an error is received, there is a default handler 158 // but you can set your own with this 159 ServeError func(http.ResponseWriter, *http.Request, error) 160 161 // PreServerShutdown is called before the HTTP(S) server is shutdown 162 // This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic 163 PreServerShutdown func() 164 165 // ServerShutdown is called when the HTTP(S) server is shut down and done 166 // handling all active connections and does not accept connections any more 167 ServerShutdown func() 168 169 // Custom command line argument groups with their descriptions 170 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 171 172 // User defined logger function. 173 Logger func(string, ...interface{}) 174 } 175 176 // UseRedoc for documentation at /docs 177 func (o *TaskTrackerAPI) UseRedoc() { 178 o.useSwaggerUI = false 179 } 180 181 // UseSwaggerUI for documentation at /docs 182 func (o *TaskTrackerAPI) UseSwaggerUI() { 183 o.useSwaggerUI = true 184 } 185 186 // SetDefaultProduces sets the default produces media type 187 func (o *TaskTrackerAPI) SetDefaultProduces(mediaType string) { 188 o.defaultProduces = mediaType 189 } 190 191 // SetDefaultConsumes returns the default consumes media type 192 func (o *TaskTrackerAPI) SetDefaultConsumes(mediaType string) { 193 o.defaultConsumes = mediaType 194 } 195 196 // SetSpec sets a spec that will be served for the clients. 197 func (o *TaskTrackerAPI) SetSpec(spec *loads.Document) { 198 o.spec = spec 199 } 200 201 // DefaultProduces returns the default produces media type 202 func (o *TaskTrackerAPI) DefaultProduces() string { 203 return o.defaultProduces 204 } 205 206 // DefaultConsumes returns the default consumes media type 207 func (o *TaskTrackerAPI) DefaultConsumes() string { 208 return o.defaultConsumes 209 } 210 211 // Formats returns the registered string formats 212 func (o *TaskTrackerAPI) Formats() strfmt.Registry { 213 return o.formats 214 } 215 216 // RegisterFormat registers a custom format validator 217 func (o *TaskTrackerAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 218 o.formats.Add(name, format, validator) 219 } 220 221 // Validate validates the registrations in the TaskTrackerAPI 222 func (o *TaskTrackerAPI) Validate() error { 223 var unregistered []string 224 225 if o.JSONConsumer == nil { 226 unregistered = append(unregistered, "JSONConsumer") 227 } 228 if o.MultipartformConsumer == nil { 229 unregistered = append(unregistered, "MultipartformConsumer") 230 } 231 232 if o.JSONProducer == nil { 233 unregistered = append(unregistered, "JSONProducer") 234 } 235 236 if o.APIKeyAuth == nil { 237 unregistered = append(unregistered, "TokenAuth") 238 } 239 if o.TokenHeaderAuth == nil { 240 unregistered = append(unregistered, "XTokenAuth") 241 } 242 243 if o.TasksAddCommentToTaskHandler == nil { 244 unregistered = append(unregistered, "tasks.AddCommentToTaskHandler") 245 } 246 if o.TasksCreateTaskHandler == nil { 247 unregistered = append(unregistered, "tasks.CreateTaskHandler") 248 } 249 if o.TasksDeleteTaskHandler == nil { 250 unregistered = append(unregistered, "tasks.DeleteTaskHandler") 251 } 252 if o.TasksGetTaskCommentsHandler == nil { 253 unregistered = append(unregistered, "tasks.GetTaskCommentsHandler") 254 } 255 if o.TasksGetTaskDetailsHandler == nil { 256 unregistered = append(unregistered, "tasks.GetTaskDetailsHandler") 257 } 258 if o.TasksListTasksHandler == nil { 259 unregistered = append(unregistered, "tasks.ListTasksHandler") 260 } 261 if o.TasksUpdateTaskHandler == nil { 262 unregistered = append(unregistered, "tasks.UpdateTaskHandler") 263 } 264 if o.TasksUploadTaskFileHandler == nil { 265 unregistered = append(unregistered, "tasks.UploadTaskFileHandler") 266 } 267 268 if len(unregistered) > 0 { 269 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 270 } 271 272 return nil 273 } 274 275 // ServeErrorFor gets a error handler for a given operation id 276 func (o *TaskTrackerAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 277 return o.ServeError 278 } 279 280 // AuthenticatorsFor gets the authenticators for the specified security schemes 281 func (o *TaskTrackerAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 282 result := make(map[string]runtime.Authenticator) 283 for name := range schemes { 284 switch name { 285 case "api_key": 286 scheme := schemes[name] 287 result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.APIKeyAuth) 288 289 case "token_header": 290 scheme := schemes[name] 291 result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.TokenHeaderAuth) 292 293 } 294 } 295 return result 296 } 297 298 // Authorizer returns the registered authorizer 299 func (o *TaskTrackerAPI) Authorizer() runtime.Authorizer { 300 return o.APIAuthorizer 301 } 302 303 // ConsumersFor gets the consumers for the specified media types. 304 // MIME type parameters are ignored here. 305 func (o *TaskTrackerAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 306 result := make(map[string]runtime.Consumer, len(mediaTypes)) 307 for _, mt := range mediaTypes { 308 switch mt { 309 case "application/vnd.goswagger.examples.task-tracker.v1+json": 310 result["application/vnd.goswagger.examples.task-tracker.v1+json"] = o.JSONConsumer 311 case "multipart/form-data": 312 result["multipart/form-data"] = o.MultipartformConsumer 313 } 314 315 if c, ok := o.customConsumers[mt]; ok { 316 result[mt] = c 317 } 318 } 319 return result 320 } 321 322 // ProducersFor gets the producers for the specified media types. 323 // MIME type parameters are ignored here. 324 func (o *TaskTrackerAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 325 result := make(map[string]runtime.Producer, len(mediaTypes)) 326 for _, mt := range mediaTypes { 327 switch mt { 328 case "application/vnd.goswagger.examples.task-tracker.v1+json": 329 result["application/vnd.goswagger.examples.task-tracker.v1+json"] = o.JSONProducer 330 } 331 332 if p, ok := o.customProducers[mt]; ok { 333 result[mt] = p 334 } 335 } 336 return result 337 } 338 339 // HandlerFor gets a http.Handler for the provided operation method and path 340 func (o *TaskTrackerAPI) HandlerFor(method, path string) (http.Handler, bool) { 341 if o.handlers == nil { 342 return nil, false 343 } 344 um := strings.ToUpper(method) 345 if _, ok := o.handlers[um]; !ok { 346 return nil, false 347 } 348 if path == "/" { 349 path = "" 350 } 351 h, ok := o.handlers[um][path] 352 return h, ok 353 } 354 355 // Context returns the middleware context for the task tracker API 356 func (o *TaskTrackerAPI) Context() *middleware.Context { 357 if o.context == nil { 358 o.context = middleware.NewRoutableContext(o.spec, o, nil) 359 } 360 361 return o.context 362 } 363 364 func (o *TaskTrackerAPI) initHandlerCache() { 365 o.Context() // don't care about the result, just that the initialization happened 366 if o.handlers == nil { 367 o.handlers = make(map[string]map[string]http.Handler) 368 } 369 370 if o.handlers["POST"] == nil { 371 o.handlers["POST"] = make(map[string]http.Handler) 372 } 373 o.handlers["POST"]["/tasks/{id}/comments"] = tasks.NewAddCommentToTask(o.context, o.TasksAddCommentToTaskHandler) 374 if o.handlers["POST"] == nil { 375 o.handlers["POST"] = make(map[string]http.Handler) 376 } 377 o.handlers["POST"]["/tasks"] = tasks.NewCreateTask(o.context, o.TasksCreateTaskHandler) 378 if o.handlers["DELETE"] == nil { 379 o.handlers["DELETE"] = make(map[string]http.Handler) 380 } 381 o.handlers["DELETE"]["/tasks/{id}"] = tasks.NewDeleteTask(o.context, o.TasksDeleteTaskHandler) 382 if o.handlers["GET"] == nil { 383 o.handlers["GET"] = make(map[string]http.Handler) 384 } 385 o.handlers["GET"]["/tasks/{id}/comments"] = tasks.NewGetTaskComments(o.context, o.TasksGetTaskCommentsHandler) 386 if o.handlers["GET"] == nil { 387 o.handlers["GET"] = make(map[string]http.Handler) 388 } 389 o.handlers["GET"]["/tasks/{id}"] = tasks.NewGetTaskDetails(o.context, o.TasksGetTaskDetailsHandler) 390 if o.handlers["GET"] == nil { 391 o.handlers["GET"] = make(map[string]http.Handler) 392 } 393 o.handlers["GET"]["/tasks"] = tasks.NewListTasks(o.context, o.TasksListTasksHandler) 394 if o.handlers["PUT"] == nil { 395 o.handlers["PUT"] = make(map[string]http.Handler) 396 } 397 o.handlers["PUT"]["/tasks/{id}"] = tasks.NewUpdateTask(o.context, o.TasksUpdateTaskHandler) 398 if o.handlers["POST"] == nil { 399 o.handlers["POST"] = make(map[string]http.Handler) 400 } 401 o.handlers["POST"]["/tasks/{id}/files"] = tasks.NewUploadTaskFile(o.context, o.TasksUploadTaskFileHandler) 402 } 403 404 // Serve creates a http handler to serve the API over HTTP 405 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 406 func (o *TaskTrackerAPI) Serve(builder middleware.Builder) http.Handler { 407 o.Init() 408 409 if o.Middleware != nil { 410 return o.Middleware(builder) 411 } 412 if o.useSwaggerUI { 413 return o.context.APIHandlerSwaggerUI(builder) 414 } 415 return o.context.APIHandler(builder) 416 } 417 418 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 419 func (o *TaskTrackerAPI) Init() { 420 if len(o.handlers) == 0 { 421 o.initHandlerCache() 422 } 423 } 424 425 // RegisterConsumer allows you to add (or override) a consumer for a media type. 426 func (o *TaskTrackerAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 427 o.customConsumers[mediaType] = consumer 428 } 429 430 // RegisterProducer allows you to add (or override) a producer for a media type. 431 func (o *TaskTrackerAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 432 o.customProducers[mediaType] = producer 433 } 434 435 // AddMiddlewareFor adds a http middleware to existing handler 436 func (o *TaskTrackerAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) { 437 um := strings.ToUpper(method) 438 if path == "/" { 439 path = "" 440 } 441 o.Init() 442 if h, ok := o.handlers[um][path]; ok { 443 o.handlers[um][path] = builder(h) 444 } 445 }