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