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