github.com/emreu/go-swagger@v0.22.1/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 implementation 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 implementation 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 implementation 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 := range schemes {
   269  		switch name {
   270  
   271  		case "api_key":
   272  
   273  			scheme := schemes[name]
   274  			result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.APIKeyAuth)
   275  
   276  		case "token_header":
   277  
   278  			scheme := schemes[name]
   279  			result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.TokenHeaderAuth)
   280  
   281  		}
   282  	}
   283  	return result
   284  
   285  }
   286  
   287  // Authorizer returns the registered authorizer
   288  func (o *TaskTrackerAPI) Authorizer() runtime.Authorizer {
   289  
   290  	return o.APIAuthorizer
   291  
   292  }
   293  
   294  // ConsumersFor gets the consumers for the specified media types
   295  func (o *TaskTrackerAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
   296  
   297  	result := make(map[string]runtime.Consumer)
   298  	for _, mt := range mediaTypes {
   299  		switch mt {
   300  
   301  		case "application/vnd.goswagger.examples.task-tracker.v1+json":
   302  			result["application/vnd.goswagger.examples.task-tracker.v1+json"] = o.JSONConsumer
   303  
   304  		case "multipart/form-data":
   305  			result["multipart/form-data"] = o.MultipartformConsumer
   306  
   307  		}
   308  
   309  		if c, ok := o.customConsumers[mt]; ok {
   310  			result[mt] = c
   311  		}
   312  	}
   313  	return result
   314  
   315  }
   316  
   317  // ProducersFor gets the producers for the specified media types
   318  func (o *TaskTrackerAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
   319  
   320  	result := make(map[string]runtime.Producer)
   321  	for _, mt := range mediaTypes {
   322  		switch mt {
   323  
   324  		case "application/vnd.goswagger.examples.task-tracker.v1+json":
   325  			result["application/vnd.goswagger.examples.task-tracker.v1+json"] = o.JSONProducer
   326  
   327  		}
   328  
   329  		if p, ok := o.customProducers[mt]; ok {
   330  			result[mt] = p
   331  		}
   332  	}
   333  	return result
   334  
   335  }
   336  
   337  // HandlerFor gets a http.Handler for the provided operation method and path
   338  func (o *TaskTrackerAPI) HandlerFor(method, path string) (http.Handler, bool) {
   339  	if o.handlers == nil {
   340  		return nil, false
   341  	}
   342  	um := strings.ToUpper(method)
   343  	if _, ok := o.handlers[um]; !ok {
   344  		return nil, false
   345  	}
   346  	if path == "/" {
   347  		path = ""
   348  	}
   349  	h, ok := o.handlers[um][path]
   350  	return h, ok
   351  }
   352  
   353  // Context returns the middleware context for the task tracker API
   354  func (o *TaskTrackerAPI) Context() *middleware.Context {
   355  	if o.context == nil {
   356  		o.context = middleware.NewRoutableContext(o.spec, o, nil)
   357  	}
   358  
   359  	return o.context
   360  }
   361  
   362  func (o *TaskTrackerAPI) initHandlerCache() {
   363  	o.Context() // don't care about the result, just that the initialization happened
   364  
   365  	if o.handlers == nil {
   366  		o.handlers = make(map[string]map[string]http.Handler)
   367  	}
   368  
   369  	if o.handlers["POST"] == nil {
   370  		o.handlers["POST"] = make(map[string]http.Handler)
   371  	}
   372  	o.handlers["POST"]["/tasks/{id}/comments"] = tasks.NewAddCommentToTask(o.context, o.TasksAddCommentToTaskHandler)
   373  
   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  
   379  	if o.handlers["DELETE"] == nil {
   380  		o.handlers["DELETE"] = make(map[string]http.Handler)
   381  	}
   382  	o.handlers["DELETE"]["/tasks/{id}"] = tasks.NewDeleteTask(o.context, o.TasksDeleteTaskHandler)
   383  
   384  	if o.handlers["GET"] == nil {
   385  		o.handlers["GET"] = make(map[string]http.Handler)
   386  	}
   387  	o.handlers["GET"]["/tasks/{id}/comments"] = tasks.NewGetTaskComments(o.context, o.TasksGetTaskCommentsHandler)
   388  
   389  	if o.handlers["GET"] == nil {
   390  		o.handlers["GET"] = make(map[string]http.Handler)
   391  	}
   392  	o.handlers["GET"]["/tasks/{id}"] = tasks.NewGetTaskDetails(o.context, o.TasksGetTaskDetailsHandler)
   393  
   394  	if o.handlers["GET"] == nil {
   395  		o.handlers["GET"] = make(map[string]http.Handler)
   396  	}
   397  	o.handlers["GET"]["/tasks"] = tasks.NewListTasks(o.context, o.TasksListTasksHandler)
   398  
   399  	if o.handlers["PUT"] == nil {
   400  		o.handlers["PUT"] = make(map[string]http.Handler)
   401  	}
   402  	o.handlers["PUT"]["/tasks/{id}"] = tasks.NewUpdateTask(o.context, o.TasksUpdateTaskHandler)
   403  
   404  	if o.handlers["POST"] == nil {
   405  		o.handlers["POST"] = make(map[string]http.Handler)
   406  	}
   407  	o.handlers["POST"]["/tasks/{id}/files"] = tasks.NewUploadTaskFile(o.context, o.TasksUploadTaskFileHandler)
   408  
   409  }
   410  
   411  // Serve creates a http handler to serve the API over HTTP
   412  // can be used directly in http.ListenAndServe(":8000", api.Serve(nil))
   413  func (o *TaskTrackerAPI) Serve(builder middleware.Builder) http.Handler {
   414  	o.Init()
   415  
   416  	if o.Middleware != nil {
   417  		return o.Middleware(builder)
   418  	}
   419  	return o.context.APIHandler(builder)
   420  }
   421  
   422  // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit
   423  func (o *TaskTrackerAPI) Init() {
   424  	if len(o.handlers) == 0 {
   425  		o.initHandlerCache()
   426  	}
   427  }
   428  
   429  // RegisterConsumer allows you to add (or override) a consumer for a media type.
   430  func (o *TaskTrackerAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) {
   431  	o.customConsumers[mediaType] = consumer
   432  }
   433  
   434  // RegisterProducer allows you to add (or override) a producer for a media type.
   435  func (o *TaskTrackerAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
   436  	o.customProducers[mediaType] = producer
   437  }