github.com/Percona-Lab/go-swagger@v0.19.0/examples/contributed-templates/stratoscale/restapi/operations/petstore_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/contributed-templates/stratoscale/restapi/operations/pet" 23 "github.com/go-swagger/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/store" 24 ) 25 26 // NewPetstoreAPI creates a new Petstore instance 27 func NewPetstoreAPI(spec *loads.Document) *PetstoreAPI { 28 return &PetstoreAPI{ 29 handlers: make(map[string]map[string]http.Handler), 30 formats: strfmt.Default, 31 defaultConsumes: "application/json", 32 defaultProduces: "application/json", 33 customConsumers: make(map[string]runtime.Consumer), 34 customProducers: make(map[string]runtime.Producer), 35 ServerShutdown: func() {}, 36 spec: spec, 37 ServeError: errors.ServeError, 38 BasicAuthenticator: security.BasicAuth, 39 APIKeyAuthenticator: security.APIKeyAuth, 40 BearerAuthenticator: security.BearerAuth, 41 JSONConsumer: runtime.JSONConsumer(), 42 JSONProducer: runtime.JSONProducer(), 43 StoreInventoryGetHandler: store.InventoryGetHandlerFunc(func(params store.InventoryGetParams, principal interface{}) middleware.Responder { 44 return middleware.NotImplemented("operation StoreInventoryGet has not yet been implemented") 45 }), 46 StoreOrderCreateHandler: store.OrderCreateHandlerFunc(func(params store.OrderCreateParams, principal interface{}) middleware.Responder { 47 return middleware.NotImplemented("operation StoreOrderCreate has not yet been implemented") 48 }), 49 StoreOrderDeleteHandler: store.OrderDeleteHandlerFunc(func(params store.OrderDeleteParams, principal interface{}) middleware.Responder { 50 return middleware.NotImplemented("operation StoreOrderDelete has not yet been implemented") 51 }), 52 StoreOrderGetHandler: store.OrderGetHandlerFunc(func(params store.OrderGetParams, principal interface{}) middleware.Responder { 53 return middleware.NotImplemented("operation StoreOrderGet has not yet been implemented") 54 }), 55 PetPetCreateHandler: pet.PetCreateHandlerFunc(func(params pet.PetCreateParams, principal interface{}) middleware.Responder { 56 return middleware.NotImplemented("operation PetPetCreate has not yet been implemented") 57 }), 58 PetPetDeleteHandler: pet.PetDeleteHandlerFunc(func(params pet.PetDeleteParams, principal interface{}) middleware.Responder { 59 return middleware.NotImplemented("operation PetPetDelete has not yet been implemented") 60 }), 61 PetPetGetHandler: pet.PetGetHandlerFunc(func(params pet.PetGetParams, principal interface{}) middleware.Responder { 62 return middleware.NotImplemented("operation PetPetGet has not yet been implemented") 63 }), 64 PetPetListHandler: pet.PetListHandlerFunc(func(params pet.PetListParams, principal interface{}) middleware.Responder { 65 return middleware.NotImplemented("operation PetPetList has not yet been implemented") 66 }), 67 PetPetUpdateHandler: pet.PetUpdateHandlerFunc(func(params pet.PetUpdateParams, principal interface{}) middleware.Responder { 68 return middleware.NotImplemented("operation PetPetUpdate has not yet been implemented") 69 }), 70 PetPetUploadImageHandler: pet.PetUploadImageHandlerFunc(func(params pet.PetUploadImageParams, principal interface{}) middleware.Responder { 71 return middleware.NotImplemented("operation PetPetUploadImage has not yet been implemented") 72 }), 73 74 // Applies when the "X-Auth-Roles" header is set 75 RolesAuth: func(token string) (interface{}, error) { 76 return nil, errors.NotImplemented("api key auth (roles) X-Auth-Roles from header param [X-Auth-Roles] has not yet been implemented") 77 }, 78 79 // default authorizer is authorized meaning no requests are blocked 80 APIAuthorizer: security.Authorized(), 81 } 82 } 83 84 /*PetstoreAPI This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. */ 85 type PetstoreAPI struct { 86 spec *loads.Document 87 context *middleware.Context 88 handlers map[string]map[string]http.Handler 89 formats strfmt.Registry 90 customConsumers map[string]runtime.Consumer 91 customProducers map[string]runtime.Producer 92 defaultConsumes string 93 defaultProduces string 94 Middleware func(middleware.Builder) http.Handler 95 96 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 97 // It has a default implemention in the security package, however you can replace it for your particular usage. 98 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 99 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 100 // It has a default implemention in the security package, however you can replace it for your particular usage. 101 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 102 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 103 // It has a default implemention in the security package, however you can replace it for your particular usage. 104 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 105 106 // JSONConsumer registers a consumer for a "application/json" mime type 107 JSONConsumer runtime.Consumer 108 109 // JSONProducer registers a producer for a "application/json" mime type 110 JSONProducer runtime.Producer 111 112 // RolesAuth registers a function that takes a token and returns a principal 113 // it performs authentication based on an api key X-Auth-Roles provided in the header 114 RolesAuth func(string) (interface{}, error) 115 116 // APIAuthorizer provides access control (ACL/RBAC/ABAC) by providing access to the request and authenticated principal 117 APIAuthorizer runtime.Authorizer 118 119 // StoreInventoryGetHandler sets the operation handler for the inventory get operation 120 StoreInventoryGetHandler store.InventoryGetHandler 121 // StoreOrderCreateHandler sets the operation handler for the order create operation 122 StoreOrderCreateHandler store.OrderCreateHandler 123 // StoreOrderDeleteHandler sets the operation handler for the order delete operation 124 StoreOrderDeleteHandler store.OrderDeleteHandler 125 // StoreOrderGetHandler sets the operation handler for the order get operation 126 StoreOrderGetHandler store.OrderGetHandler 127 // PetPetCreateHandler sets the operation handler for the pet create operation 128 PetPetCreateHandler pet.PetCreateHandler 129 // PetPetDeleteHandler sets the operation handler for the pet delete operation 130 PetPetDeleteHandler pet.PetDeleteHandler 131 // PetPetGetHandler sets the operation handler for the pet get operation 132 PetPetGetHandler pet.PetGetHandler 133 // PetPetListHandler sets the operation handler for the pet list operation 134 PetPetListHandler pet.PetListHandler 135 // PetPetUpdateHandler sets the operation handler for the pet update operation 136 PetPetUpdateHandler pet.PetUpdateHandler 137 // PetPetUploadImageHandler sets the operation handler for the pet upload image operation 138 PetPetUploadImageHandler pet.PetUploadImageHandler 139 140 // ServeError is called when an error is received, there is a default handler 141 // but you can set your own with this 142 ServeError func(http.ResponseWriter, *http.Request, error) 143 144 // ServerShutdown is called when the HTTP(S) server is shut down and done 145 // handling all active connections and does not accept connections any more 146 ServerShutdown func() 147 148 // Custom command line argument groups with their descriptions 149 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 150 151 // User defined logger function. 152 Logger func(string, ...interface{}) 153 } 154 155 // SetDefaultProduces sets the default produces media type 156 func (o *PetstoreAPI) SetDefaultProduces(mediaType string) { 157 o.defaultProduces = mediaType 158 } 159 160 // SetDefaultConsumes returns the default consumes media type 161 func (o *PetstoreAPI) SetDefaultConsumes(mediaType string) { 162 o.defaultConsumes = mediaType 163 } 164 165 // SetSpec sets a spec that will be served for the clients. 166 func (o *PetstoreAPI) SetSpec(spec *loads.Document) { 167 o.spec = spec 168 } 169 170 // DefaultProduces returns the default produces media type 171 func (o *PetstoreAPI) DefaultProduces() string { 172 return o.defaultProduces 173 } 174 175 // DefaultConsumes returns the default consumes media type 176 func (o *PetstoreAPI) DefaultConsumes() string { 177 return o.defaultConsumes 178 } 179 180 // Formats returns the registered string formats 181 func (o *PetstoreAPI) Formats() strfmt.Registry { 182 return o.formats 183 } 184 185 // RegisterFormat registers a custom format validator 186 func (o *PetstoreAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 187 o.formats.Add(name, format, validator) 188 } 189 190 // Validate validates the registrations in the PetstoreAPI 191 func (o *PetstoreAPI) Validate() error { 192 var unregistered []string 193 194 if o.JSONConsumer == nil { 195 unregistered = append(unregistered, "JSONConsumer") 196 } 197 198 if o.JSONProducer == nil { 199 unregistered = append(unregistered, "JSONProducer") 200 } 201 202 if o.RolesAuth == nil { 203 unregistered = append(unregistered, "XAuthRolesAuth") 204 } 205 206 if o.StoreInventoryGetHandler == nil { 207 unregistered = append(unregistered, "store.InventoryGetHandler") 208 } 209 210 if o.StoreOrderCreateHandler == nil { 211 unregistered = append(unregistered, "store.OrderCreateHandler") 212 } 213 214 if o.StoreOrderDeleteHandler == nil { 215 unregistered = append(unregistered, "store.OrderDeleteHandler") 216 } 217 218 if o.StoreOrderGetHandler == nil { 219 unregistered = append(unregistered, "store.OrderGetHandler") 220 } 221 222 if o.PetPetCreateHandler == nil { 223 unregistered = append(unregistered, "pet.PetCreateHandler") 224 } 225 226 if o.PetPetDeleteHandler == nil { 227 unregistered = append(unregistered, "pet.PetDeleteHandler") 228 } 229 230 if o.PetPetGetHandler == nil { 231 unregistered = append(unregistered, "pet.PetGetHandler") 232 } 233 234 if o.PetPetListHandler == nil { 235 unregistered = append(unregistered, "pet.PetListHandler") 236 } 237 238 if o.PetPetUpdateHandler == nil { 239 unregistered = append(unregistered, "pet.PetUpdateHandler") 240 } 241 242 if o.PetPetUploadImageHandler == nil { 243 unregistered = append(unregistered, "pet.PetUploadImageHandler") 244 } 245 246 if len(unregistered) > 0 { 247 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 248 } 249 250 return nil 251 } 252 253 // ServeErrorFor gets a error handler for a given operation id 254 func (o *PetstoreAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 255 return o.ServeError 256 } 257 258 // AuthenticatorsFor gets the authenticators for the specified security schemes 259 func (o *PetstoreAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 260 261 result := make(map[string]runtime.Authenticator) 262 for name, scheme := range schemes { 263 switch name { 264 265 case "roles": 266 267 result[name] = o.APIKeyAuthenticator(scheme.Name, scheme.In, o.RolesAuth) 268 269 } 270 } 271 return result 272 273 } 274 275 // Authorizer returns the registered authorizer 276 func (o *PetstoreAPI) Authorizer() runtime.Authorizer { 277 278 return o.APIAuthorizer 279 280 } 281 282 // ConsumersFor gets the consumers for the specified media types 283 func (o *PetstoreAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 284 285 result := make(map[string]runtime.Consumer) 286 for _, mt := range mediaTypes { 287 switch mt { 288 289 case "application/json": 290 result["application/json"] = o.JSONConsumer 291 292 } 293 294 if c, ok := o.customConsumers[mt]; ok { 295 result[mt] = c 296 } 297 } 298 return result 299 300 } 301 302 // ProducersFor gets the producers for the specified media types 303 func (o *PetstoreAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 304 305 result := make(map[string]runtime.Producer) 306 for _, mt := range mediaTypes { 307 switch mt { 308 309 case "application/json": 310 result["application/json"] = o.JSONProducer 311 312 } 313 314 if p, ok := o.customProducers[mt]; ok { 315 result[mt] = p 316 } 317 } 318 return result 319 320 } 321 322 // HandlerFor gets a http.Handler for the provided operation method and path 323 func (o *PetstoreAPI) HandlerFor(method, path string) (http.Handler, bool) { 324 if o.handlers == nil { 325 return nil, false 326 } 327 um := strings.ToUpper(method) 328 if _, ok := o.handlers[um]; !ok { 329 return nil, false 330 } 331 if path == "/" { 332 path = "" 333 } 334 h, ok := o.handlers[um][path] 335 return h, ok 336 } 337 338 // Context returns the middleware context for the petstore API 339 func (o *PetstoreAPI) Context() *middleware.Context { 340 if o.context == nil { 341 o.context = middleware.NewRoutableContext(o.spec, o, nil) 342 } 343 344 return o.context 345 } 346 347 func (o *PetstoreAPI) initHandlerCache() { 348 o.Context() // don't care about the result, just that the initialization happened 349 350 if o.handlers == nil { 351 o.handlers = make(map[string]map[string]http.Handler) 352 } 353 354 if o.handlers["GET"] == nil { 355 o.handlers["GET"] = make(map[string]http.Handler) 356 } 357 o.handlers["GET"]["/store/inventory"] = store.NewInventoryGet(o.context, o.StoreInventoryGetHandler) 358 359 if o.handlers["POST"] == nil { 360 o.handlers["POST"] = make(map[string]http.Handler) 361 } 362 o.handlers["POST"]["/store/order"] = store.NewOrderCreate(o.context, o.StoreOrderCreateHandler) 363 364 if o.handlers["DELETE"] == nil { 365 o.handlers["DELETE"] = make(map[string]http.Handler) 366 } 367 o.handlers["DELETE"]["/store/order/{orderId}"] = store.NewOrderDelete(o.context, o.StoreOrderDeleteHandler) 368 369 if o.handlers["GET"] == nil { 370 o.handlers["GET"] = make(map[string]http.Handler) 371 } 372 o.handlers["GET"]["/store/order/{orderId}"] = store.NewOrderGet(o.context, o.StoreOrderGetHandler) 373 374 if o.handlers["POST"] == nil { 375 o.handlers["POST"] = make(map[string]http.Handler) 376 } 377 o.handlers["POST"]["/pet"] = pet.NewPetCreate(o.context, o.PetPetCreateHandler) 378 379 if o.handlers["DELETE"] == nil { 380 o.handlers["DELETE"] = make(map[string]http.Handler) 381 } 382 o.handlers["DELETE"]["/pet/{petId}"] = pet.NewPetDelete(o.context, o.PetPetDeleteHandler) 383 384 if o.handlers["GET"] == nil { 385 o.handlers["GET"] = make(map[string]http.Handler) 386 } 387 o.handlers["GET"]["/pet/{petId}"] = pet.NewPetGet(o.context, o.PetPetGetHandler) 388 389 if o.handlers["GET"] == nil { 390 o.handlers["GET"] = make(map[string]http.Handler) 391 } 392 o.handlers["GET"]["/pet"] = pet.NewPetList(o.context, o.PetPetListHandler) 393 394 if o.handlers["PUT"] == nil { 395 o.handlers["PUT"] = make(map[string]http.Handler) 396 } 397 o.handlers["PUT"]["/pet"] = pet.NewPetUpdate(o.context, o.PetPetUpdateHandler) 398 399 if o.handlers["POST"] == nil { 400 o.handlers["POST"] = make(map[string]http.Handler) 401 } 402 o.handlers["POST"]["/pet/{petId}/image"] = pet.NewPetUploadImage(o.context, o.PetPetUploadImageHandler) 403 404 } 405 406 // Serve creates a http handler to serve the API over HTTP 407 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 408 func (o *PetstoreAPI) Serve(builder middleware.Builder) http.Handler { 409 o.Init() 410 411 if o.Middleware != nil { 412 return o.Middleware(builder) 413 } 414 return o.context.APIHandler(builder) 415 } 416 417 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 418 func (o *PetstoreAPI) Init() { 419 if len(o.handlers) == 0 { 420 o.initHandlerCache() 421 } 422 } 423 424 // RegisterConsumer allows you to add (or override) a consumer for a media type. 425 func (o *PetstoreAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 426 o.customConsumers[mediaType] = consumer 427 } 428 429 // RegisterProducer allows you to add (or override) a producer for a media type. 430 func (o *PetstoreAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 431 o.customProducers[mediaType] = producer 432 }