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