spaas_api.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Code generated by go-swagger; DO NOT EDIT.
  2. package operations
  3. // This file was generated by the swagger tool.
  4. // Editing this file might prove futile when you re-run the swagger generate command
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "github.com/go-openapi/errors"
  10. "github.com/go-openapi/loads"
  11. "github.com/go-openapi/runtime"
  12. "github.com/go-openapi/runtime/middleware"
  13. "github.com/go-openapi/runtime/security"
  14. "github.com/go-openapi/spec"
  15. "github.com/go-openapi/strfmt"
  16. "github.com/go-openapi/swag"
  17. )
  18. // NewSpaasAPI creates a new Spaas instance
  19. func NewSpaasAPI(spec *loads.Document) *SpaasAPI {
  20. return &SpaasAPI{
  21. handlers: make(map[string]map[string]http.Handler),
  22. formats: strfmt.Default,
  23. defaultConsumes: "application/json",
  24. defaultProduces: "application/json",
  25. customConsumers: make(map[string]runtime.Consumer),
  26. customProducers: make(map[string]runtime.Producer),
  27. PreServerShutdown: func() {},
  28. ServerShutdown: func() {},
  29. spec: spec,
  30. useSwaggerUI: false,
  31. ServeError: errors.ServeError,
  32. BasicAuthenticator: security.BasicAuth,
  33. APIKeyAuthenticator: security.APIKeyAuth,
  34. BearerAuthenticator: security.BearerAuth,
  35. JSONConsumer: runtime.JSONConsumer(),
  36. JSONProducer: runtime.JSONProducer(),
  37. ProductionPlanHandler: ProductionPlanHandlerFunc(func(params ProductionPlanParams) middleware.Responder {
  38. return middleware.NotImplemented("operation ProductionPlan has not yet been implemented")
  39. }),
  40. }
  41. }
  42. /*SpaasAPI This API exposes an algorithm that computes optimal power production allocation for a given power need and power plant capacity. */
  43. type SpaasAPI struct {
  44. spec *loads.Document
  45. context *middleware.Context
  46. handlers map[string]map[string]http.Handler
  47. formats strfmt.Registry
  48. customConsumers map[string]runtime.Consumer
  49. customProducers map[string]runtime.Producer
  50. defaultConsumes string
  51. defaultProduces string
  52. Middleware func(middleware.Builder) http.Handler
  53. useSwaggerUI bool
  54. // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function.
  55. // It has a default implementation in the security package, however you can replace it for your particular usage.
  56. BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
  57. // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function.
  58. // It has a default implementation in the security package, however you can replace it for your particular usage.
  59. APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator
  60. // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function.
  61. // It has a default implementation in the security package, however you can replace it for your particular usage.
  62. BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
  63. // JSONConsumer registers a consumer for the following mime types:
  64. // - application/eu.reverservices.gem.spaas.v1+json
  65. // - application/json
  66. JSONConsumer runtime.Consumer
  67. // JSONProducer registers a producer for the following mime types:
  68. // - application/eu.reverservices.gem.spaas.v1+json
  69. // - application/json
  70. JSONProducer runtime.Producer
  71. // ProductionPlanHandler sets the operation handler for the production plan operation
  72. ProductionPlanHandler ProductionPlanHandler
  73. // ServeError is called when an error is received, there is a default handler
  74. // but you can set your own with this
  75. ServeError func(http.ResponseWriter, *http.Request, error)
  76. // PreServerShutdown is called before the HTTP(S) server is shutdown
  77. // This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic
  78. PreServerShutdown func()
  79. // ServerShutdown is called when the HTTP(S) server is shut down and done
  80. // handling all active connections and does not accept connections any more
  81. ServerShutdown func()
  82. // Custom command line argument groups with their descriptions
  83. CommandLineOptionsGroups []swag.CommandLineOptionsGroup
  84. // User defined logger function.
  85. Logger func(string, ...interface{})
  86. }
  87. // UseRedoc for documentation at /docs
  88. func (o *SpaasAPI) UseRedoc() {
  89. o.useSwaggerUI = false
  90. }
  91. // UseSwaggerUI for documentation at /docs
  92. func (o *SpaasAPI) UseSwaggerUI() {
  93. o.useSwaggerUI = true
  94. }
  95. // SetDefaultProduces sets the default produces media type
  96. func (o *SpaasAPI) SetDefaultProduces(mediaType string) {
  97. o.defaultProduces = mediaType
  98. }
  99. // SetDefaultConsumes returns the default consumes media type
  100. func (o *SpaasAPI) SetDefaultConsumes(mediaType string) {
  101. o.defaultConsumes = mediaType
  102. }
  103. // SetSpec sets a spec that will be served for the clients.
  104. func (o *SpaasAPI) SetSpec(spec *loads.Document) {
  105. o.spec = spec
  106. }
  107. // DefaultProduces returns the default produces media type
  108. func (o *SpaasAPI) DefaultProduces() string {
  109. return o.defaultProduces
  110. }
  111. // DefaultConsumes returns the default consumes media type
  112. func (o *SpaasAPI) DefaultConsumes() string {
  113. return o.defaultConsumes
  114. }
  115. // Formats returns the registered string formats
  116. func (o *SpaasAPI) Formats() strfmt.Registry {
  117. return o.formats
  118. }
  119. // RegisterFormat registers a custom format validator
  120. func (o *SpaasAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) {
  121. o.formats.Add(name, format, validator)
  122. }
  123. // Validate validates the registrations in the SpaasAPI
  124. func (o *SpaasAPI) Validate() error {
  125. var unregistered []string
  126. if o.JSONConsumer == nil {
  127. unregistered = append(unregistered, "JSONConsumer")
  128. }
  129. if o.JSONProducer == nil {
  130. unregistered = append(unregistered, "JSONProducer")
  131. }
  132. if o.ProductionPlanHandler == nil {
  133. unregistered = append(unregistered, "ProductionPlanHandler")
  134. }
  135. if len(unregistered) > 0 {
  136. return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", "))
  137. }
  138. return nil
  139. }
  140. // ServeErrorFor gets a error handler for a given operation id
  141. func (o *SpaasAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) {
  142. return o.ServeError
  143. }
  144. // AuthenticatorsFor gets the authenticators for the specified security schemes
  145. func (o *SpaasAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator {
  146. return nil
  147. }
  148. // Authorizer returns the registered authorizer
  149. func (o *SpaasAPI) Authorizer() runtime.Authorizer {
  150. return nil
  151. }
  152. // ConsumersFor gets the consumers for the specified media types.
  153. // MIME type parameters are ignored here.
  154. func (o *SpaasAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
  155. result := make(map[string]runtime.Consumer, len(mediaTypes))
  156. for _, mt := range mediaTypes {
  157. switch mt {
  158. case "application/eu.reverservices.gem.spaas.v1+json":
  159. result["application/eu.reverservices.gem.spaas.v1+json"] = o.JSONConsumer
  160. case "application/json":
  161. result["application/json"] = o.JSONConsumer
  162. }
  163. if c, ok := o.customConsumers[mt]; ok {
  164. result[mt] = c
  165. }
  166. }
  167. return result
  168. }
  169. // ProducersFor gets the producers for the specified media types.
  170. // MIME type parameters are ignored here.
  171. func (o *SpaasAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
  172. result := make(map[string]runtime.Producer, len(mediaTypes))
  173. for _, mt := range mediaTypes {
  174. switch mt {
  175. case "application/eu.reverservices.gem.spaas.v1+json":
  176. result["application/eu.reverservices.gem.spaas.v1+json"] = o.JSONProducer
  177. case "application/json":
  178. result["application/json"] = o.JSONProducer
  179. }
  180. if p, ok := o.customProducers[mt]; ok {
  181. result[mt] = p
  182. }
  183. }
  184. return result
  185. }
  186. // HandlerFor gets a http.Handler for the provided operation method and path
  187. func (o *SpaasAPI) HandlerFor(method, path string) (http.Handler, bool) {
  188. if o.handlers == nil {
  189. return nil, false
  190. }
  191. um := strings.ToUpper(method)
  192. if _, ok := o.handlers[um]; !ok {
  193. return nil, false
  194. }
  195. if path == "/" {
  196. path = ""
  197. }
  198. h, ok := o.handlers[um][path]
  199. return h, ok
  200. }
  201. // Context returns the middleware context for the spaas API
  202. func (o *SpaasAPI) Context() *middleware.Context {
  203. if o.context == nil {
  204. o.context = middleware.NewRoutableContext(o.spec, o, nil)
  205. }
  206. return o.context
  207. }
  208. func (o *SpaasAPI) initHandlerCache() {
  209. o.Context() // don't care about the result, just that the initialization happened
  210. if o.handlers == nil {
  211. o.handlers = make(map[string]map[string]http.Handler)
  212. }
  213. if o.handlers["POST"] == nil {
  214. o.handlers["POST"] = make(map[string]http.Handler)
  215. }
  216. o.handlers["POST"]["/productionplan"] = NewProductionPlan(o.context, o.ProductionPlanHandler)
  217. }
  218. // Serve creates a http handler to serve the API over HTTP
  219. // can be used directly in http.ListenAndServe(":8000", api.Serve(nil))
  220. func (o *SpaasAPI) Serve(builder middleware.Builder) http.Handler {
  221. o.Init()
  222. if o.Middleware != nil {
  223. return o.Middleware(builder)
  224. }
  225. if o.useSwaggerUI {
  226. return o.context.APIHandlerSwaggerUI(builder)
  227. }
  228. return o.context.APIHandler(builder)
  229. }
  230. // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit
  231. func (o *SpaasAPI) Init() {
  232. if len(o.handlers) == 0 {
  233. o.initHandlerCache()
  234. }
  235. }
  236. // RegisterConsumer allows you to add (or override) a consumer for a media type.
  237. func (o *SpaasAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) {
  238. o.customConsumers[mediaType] = consumer
  239. }
  240. // RegisterProducer allows you to add (or override) a producer for a media type.
  241. func (o *SpaasAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
  242. o.customProducers[mediaType] = producer
  243. }
  244. // AddMiddlewareFor adds a http middleware to existing handler
  245. func (o *SpaasAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) {
  246. um := strings.ToUpper(method)
  247. if path == "/" {
  248. path = ""
  249. }
  250. o.Init()
  251. if h, ok := o.handlers[um][path]; ok {
  252. o.handlers[method][path] = builder(h)
  253. }
  254. }