diff --git a/http/jsonapi/generator/generate_handler.go b/http/jsonapi/generator/generate_handler.go index f4012e58..d1ee45e3 100644 --- a/http/jsonapi/generator/generate_handler.go +++ b/http/jsonapi/generator/generate_handler.go @@ -75,6 +75,7 @@ func (g *Generator) BuildHandler(schema *openapi3.Swagger) error { funcs := []routeGeneratorFunc{ g.generateRequestResponseTypes, g.buildServiceInterface, + g.buildRouterHelpers, g.buildRouter, g.buildRouterWithFallbackAsArg, } @@ -382,11 +383,63 @@ func (g *Generator) buildRouterWithFallbackAsArg(routes []*route, schema *openap } else { g.goSource.Func().Id("RouterWithFallback").Params( - serviceInterfaceVariable).Op("*").Qual(pkgGorillaMux, "Router").Block(routerBody...) + serviceInterfaceVariable, jen.Id("fallback").Qual("net/http", "Handler")).Op("*").Qual(pkgGorillaMux, "Router").Block(routerBody...) } return nil } +func (g *Generator) buildRouterHelpers(routes []*route, schema *openapi3.Swagger) error { + needsSecurity := hasSecuritySchema(schema) + + // sort the routes with query parameter to the top + sortableRoutes := sortableRouteList(routes) + sort.Stable(&sortableRoutes) + + fallbackName := "fallback" + fallback := jen.Id(fallbackName).Qual("net/http", "Handler") + // add all route handlers + for i := 0; i < len(sortableRoutes); i++ { + route := sortableRoutes[i] + var routeCallParams *jen.Statement + if needsSecurity { + routeCallParams = jen.List(jen.Id("service"), jen.Id("authBackend")) + } else { + routeCallParams = jen.List(jen.Id("service")) + } + primaryHandler := jen.Id(route.handler).Call(routeCallParams) + fallbackHandler := jen.Id(fallbackName) + ifElse := make([]jen.Code, 0) + for _, handler := range []jen.Code{primaryHandler, fallbackHandler} { + block := jen.Return(handler) + ifElse = append(ifElse, block) + } + + if len(ifElse) < 1 { + panic("if-else slice should contain two elements, one with the service interface being called and one passing the NotFoundHandler") + } + + implGuard := jen.If( + jen.List(jen.Id("service"), jen.Id("ok")).Op(":=").Id("service").Assert(jen.Id(generateSubServiceName(route.handler))), + jen.Id("ok")).Block(ifElse[0]).Else().Block(ifElse[1]) + + comment := jen.Commentf("%s helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler.", generateHandlerTypeAssertionHelperName(route.handler)) + + var callParams *jen.Statement + if needsSecurity { + callParams = jen.List(jen.Id("service").Id("interface{}"), fallback, jen.Id("authBackend").Id(authBackendInterface)) + } else { + callParams = jen.List(jen.Id("service").Id("interface{}"), fallback) + } + helper := jen.Func().Id(generateHandlerTypeAssertionHelperName(route.handler)). + Params(callParams).Qual("net/http", "Handler").Block(implGuard).Line().Line() + + g.goSource.Line().Add(comment) + g.goSource.Add(helper) + } + + return nil +} + func (g *Generator) buildRouterBodyWithFallback(routes []*route, schema *openapi3.Swagger, fallback jen.Code) ([]jen.Code, error) { needsSecurity := hasSecuritySchema(schema) startInd := 0 @@ -442,45 +495,30 @@ func (g *Generator) buildRouterBodyWithFallback(routes []*route, schema *openapi route := sortableRoutes[i] var routeCallParams *jen.Statement if needsSecurity { - routeCallParams = jen.List(jen.Id("service"), jen.Id("authBackend")) + routeCallParams = jen.List(jen.Id("service"), fallback, jen.Id("authBackend")) } else { - routeCallParams = jen.List(jen.Id("service")) + routeCallParams = jen.List(jen.Id("service"), fallback) } - primaryHandler := jen.Id(route.handler).Call(routeCallParams) - fallbackHandler := fallback - ifElse := make([]jen.Code, 0) - for _, handler := range []jen.Code{primaryHandler, fallbackHandler} { - // build single route - routeStmt := jen.Id(subrouterID).Dot("Methods").Call(jen.Lit(route.method)). - Dot("Path").Call(jen.Lit(route.url.Path)). - Dot("Handler").Call(handler) - - // add query parameters for route matching - if len(route.queryValues) > 0 { - for key, value := range route.queryValues { - if len(value) != 1 { - panic("query paths can only handle one query parameter with the same name!") - } - routeStmt.Dot("Queries").Call(jen.Lit(key), jen.Lit(value[0])) + helper := jen.Id(generateHandlerTypeAssertionHelperName(route.handler)).Call(routeCallParams) + routeStmt := jen.Id(subrouterID).Dot("Methods").Call(jen.Lit(route.method)). + Dot("Path").Call(jen.Lit(route.url.Path)) + + // add query parameters for route matching + if len(route.queryValues) > 0 { + for key, value := range route.queryValues { + if len(value) != 1 { + panic("query paths can only handle one query parameter with the same name!") } + routeStmt.Dot("Queries").Call(jen.Lit(key), jen.Lit(value[0])) } - - // add the name to build routes - routeStmt.Dot("Name").Call(jen.Lit(route.serviceFunc)) - - // add to control-flow - ifElse = append(ifElse, routeStmt) } - if len(ifElse) < 1 { - panic("if-else slice should contain two elements, one with the service interface being called and one passing the NotFoundHandler") - } + // add the name to build routes + routeStmt.Dot("Name").Call(jen.Lit(route.serviceFunc)) - implGuard := jen.If( - jen.List(jen.Id("service"), jen.Id("ok")).Op(":=").Id("service").Assert(jen.Id(generateSubServiceName(route.handler))), - jen.Id("ok")).Block(ifElse[0]).Else().Block(ifElse[1]) + routeStmt.Dot("Handler").Call(helper) - routeStmts = append(routeStmts, implGuard) + routeStmts = append(routeStmts, routeStmt) } } @@ -821,3 +859,7 @@ func generateParamName(param *openapi3.ParameterRef) string { func generateSubServiceName(handler string) string { return fmt.Sprintf("%s%s", handler, serviceInterface) } + +func generateHandlerTypeAssertionHelperName(handler string) string { + return fmt.Sprintf("%sWithFallbackHelper", handler) +} diff --git a/http/jsonapi/generator/internal/articles/open-api_test.go b/http/jsonapi/generator/internal/articles/open-api_test.go index 12ebb24f..c91bb391 100644 --- a/http/jsonapi/generator/internal/articles/open-api_test.go +++ b/http/jsonapi/generator/internal/articles/open-api_test.go @@ -364,30 +364,45 @@ type Service interface { UpdateArticleInlineRefHandlerService } -/* -Router implements: Articles Test Service - -Articles Test Service -*/ -func Router(service interface{}) *mux.Router { - router := mux.NewRouter() - // Subrouter s1 - Path: - s1 := router.PathPrefix("").Subrouter() +// UpdateArticleCommentsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateArticleCommentsHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(UpdateArticleCommentsHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Handler(UpdateArticleCommentsHandler(service)).Name("UpdateArticleComments") + return UpdateArticleCommentsHandler(service) } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Handler(router.NotFoundHandler).Name("UpdateArticleComments") + return fallback } +} + +// UpdateArticleInlineTypeHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateArticleInlineTypeHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(UpdateArticleInlineTypeHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Handler(UpdateArticleInlineTypeHandler(service)).Name("UpdateArticleInlineType") + return UpdateArticleInlineTypeHandler(service) } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Handler(router.NotFoundHandler).Name("UpdateArticleInlineType") + return fallback } +} + +// UpdateArticleInlineRefHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateArticleInlineRefHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(UpdateArticleInlineRefHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Handler(UpdateArticleInlineRefHandler(service)).Name("UpdateArticleInlineRef") + return UpdateArticleInlineRefHandler(service) } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Handler(router.NotFoundHandler).Name("UpdateArticleInlineRef") + return fallback } +} + +/* +Router implements: Articles Test Service + +Articles Test Service +*/ +func Router(service interface{}) *mux.Router { + router := mux.NewRouter() + // Subrouter s1 - Path: + s1 := router.PathPrefix("").Subrouter() + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Name("UpdateArticleComments").Handler(UpdateArticleCommentsHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Name("UpdateArticleInlineType").Handler(UpdateArticleInlineTypeHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Name("UpdateArticleInlineRef").Handler(UpdateArticleInlineRefHandlerWithFallbackHelper(service, router.NotFoundHandler)) return router } @@ -396,24 +411,12 @@ Router implements: Articles Test Service Articles Test Service */ -func RouterWithFallback(service interface{}) *mux.Router { +func RouterWithFallback(service interface{}, fallback http.Handler) *mux.Router { router := mux.NewRouter() // Subrouter s1 - Path: s1 := router.PathPrefix("").Subrouter() - if service, ok := service.(UpdateArticleCommentsHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Handler(UpdateArticleCommentsHandler(service)).Name("UpdateArticleComments") - } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Handler(fallback).Name("UpdateArticleComments") - } - if service, ok := service.(UpdateArticleInlineTypeHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Handler(UpdateArticleInlineTypeHandler(service)).Name("UpdateArticleInlineType") - } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Handler(fallback).Name("UpdateArticleInlineType") - } - if service, ok := service.(UpdateArticleInlineRefHandlerService); ok { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Handler(UpdateArticleInlineRefHandler(service)).Name("UpdateArticleInlineRef") - } else { - s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Handler(fallback).Name("UpdateArticleInlineRef") - } + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/comments").Name("UpdateArticleComments").Handler(UpdateArticleCommentsHandlerWithFallbackHelper(service, fallback)) + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inline").Name("UpdateArticleInlineType").Handler(UpdateArticleInlineTypeHandlerWithFallbackHelper(service, fallback)) + s1.Methods("PATCH").Path("/api/articles/{uuid}/relationships/inlineref").Name("UpdateArticleInlineRef").Handler(UpdateArticleInlineRefHandlerWithFallbackHelper(service, fallback)) return router } diff --git a/http/jsonapi/generator/internal/fueling/open-api_test.go b/http/jsonapi/generator/internal/fueling/open-api_test.go index 24a5d49f..b569e54e 100644 --- a/http/jsonapi/generator/internal/fueling/open-api_test.go +++ b/http/jsonapi/generator/internal/fueling/open-api_test.go @@ -605,57 +605,61 @@ type Service interface { WaitOnPumpStatusChangeHandlerService } -/* -Router implements: PACE Fueling API - -Fueling API -*/ -func Router(service interface{}) *mux.Router { - router := mux.NewRouter() - // Subrouter s1 - Path: /fueling/beta/ - s1 := router.PathPrefix("/fueling/beta/").Subrouter() +// WaitOnPumpStatusChangeHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func WaitOnPumpStatusChangeHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(WaitOnPumpStatusChangeHandlerService); ok { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(WaitOnPumpStatusChangeHandler(service)).Name("WaitOnPumpStatusChange") + return WaitOnPumpStatusChangeHandler(service) } else { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(router.NotFoundHandler).Name("WaitOnPumpStatusChange") + return fallback } +} + +// GetPumpHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPumpHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(GetPumpHandlerService); ok { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(GetPumpHandler(service)).Name("GetPump") + return GetPumpHandler(service) } else { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(router.NotFoundHandler).Name("GetPump") + return fallback } +} + +// ProcessPaymentHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func ProcessPaymentHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(ProcessPaymentHandlerService); ok { - s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(ProcessPaymentHandler(service)).Name("ProcessPayment") + return ProcessPaymentHandler(service) } else { - s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(router.NotFoundHandler).Name("ProcessPayment") + return fallback } +} + +// ApproachingAtTheForecourtHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func ApproachingAtTheForecourtHandlerWithFallbackHelper(service interface{}, fallback http.Handler) http.Handler { if service, ok := service.(ApproachingAtTheForecourtHandlerService); ok { - s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(ApproachingAtTheForecourtHandler(service)).Name("ApproachingAtTheForecourt") + return ApproachingAtTheForecourtHandler(service) } else { - s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(router.NotFoundHandler).Name("ApproachingAtTheForecourt") + return fallback } +} + +/* +Router implements: PACE Fueling API + +Fueling API +*/ +func Router(service interface{}) *mux.Router { + router := mux.NewRouter() + // Subrouter s1 - Path: /fueling/beta/ + s1 := router.PathPrefix("/fueling/beta/").Subrouter() + s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Name("WaitOnPumpStatusChange").Handler(WaitOnPumpStatusChangeHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Name("GetPump").Handler(GetPumpHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Name("ApproachingAtTheForecourt").Handler(ApproachingAtTheForecourtHandlerWithFallbackHelper(service, router.NotFoundHandler)) // Subrouter s2 - Path: /fueling/v1/ s2 := router.PathPrefix("/fueling/v1/").Subrouter() - if service, ok := service.(WaitOnPumpStatusChangeHandlerService); ok { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(WaitOnPumpStatusChangeHandler(service)).Name("WaitOnPumpStatusChange") - } else { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(router.NotFoundHandler).Name("WaitOnPumpStatusChange") - } - if service, ok := service.(GetPumpHandlerService); ok { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(GetPumpHandler(service)).Name("GetPump") - } else { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(router.NotFoundHandler).Name("GetPump") - } - if service, ok := service.(ProcessPaymentHandlerService); ok { - s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(ProcessPaymentHandler(service)).Name("ProcessPayment") - } else { - s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(router.NotFoundHandler).Name("ProcessPayment") - } - if service, ok := service.(ApproachingAtTheForecourtHandlerService); ok { - s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(ApproachingAtTheForecourtHandler(service)).Name("ApproachingAtTheForecourt") - } else { - s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(router.NotFoundHandler).Name("ApproachingAtTheForecourt") - } + s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Name("WaitOnPumpStatusChange").Handler(WaitOnPumpStatusChangeHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Name("GetPump").Handler(GetPumpHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, router.NotFoundHandler)) + s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Name("ApproachingAtTheForecourt").Handler(ApproachingAtTheForecourtHandlerWithFallbackHelper(service, router.NotFoundHandler)) return router } @@ -664,51 +668,19 @@ Router implements: PACE Fueling API Fueling API */ -func RouterWithFallback(service interface{}) *mux.Router { +func RouterWithFallback(service interface{}, fallback http.Handler) *mux.Router { router := mux.NewRouter() // Subrouter s1 - Path: /fueling/beta/ s1 := router.PathPrefix("/fueling/beta/").Subrouter() - if service, ok := service.(WaitOnPumpStatusChangeHandlerService); ok { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(WaitOnPumpStatusChangeHandler(service)).Name("WaitOnPumpStatusChange") - } else { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(fallback).Name("WaitOnPumpStatusChange") - } - if service, ok := service.(GetPumpHandlerService); ok { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(GetPumpHandler(service)).Name("GetPump") - } else { - s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(fallback).Name("GetPump") - } - if service, ok := service.(ProcessPaymentHandlerService); ok { - s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(ProcessPaymentHandler(service)).Name("ProcessPayment") - } else { - s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(fallback).Name("ProcessPayment") - } - if service, ok := service.(ApproachingAtTheForecourtHandlerService); ok { - s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(ApproachingAtTheForecourtHandler(service)).Name("ApproachingAtTheForecourt") - } else { - s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(fallback).Name("ApproachingAtTheForecourt") - } + s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Name("WaitOnPumpStatusChange").Handler(WaitOnPumpStatusChangeHandlerWithFallbackHelper(service, fallback)) + s1.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Name("GetPump").Handler(GetPumpHandlerWithFallbackHelper(service, fallback)) + s1.Methods("POST").Path("/gas-station/{gasStationId}/payment").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, fallback)) + s1.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Name("ApproachingAtTheForecourt").Handler(ApproachingAtTheForecourtHandlerWithFallbackHelper(service, fallback)) // Subrouter s2 - Path: /fueling/v1/ s2 := router.PathPrefix("/fueling/v1/").Subrouter() - if service, ok := service.(WaitOnPumpStatusChangeHandlerService); ok { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(WaitOnPumpStatusChangeHandler(service)).Name("WaitOnPumpStatusChange") - } else { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Handler(fallback).Name("WaitOnPumpStatusChange") - } - if service, ok := service.(GetPumpHandlerService); ok { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(GetPumpHandler(service)).Name("GetPump") - } else { - s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Handler(fallback).Name("GetPump") - } - if service, ok := service.(ProcessPaymentHandlerService); ok { - s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(ProcessPaymentHandler(service)).Name("ProcessPayment") - } else { - s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Handler(fallback).Name("ProcessPayment") - } - if service, ok := service.(ApproachingAtTheForecourtHandlerService); ok { - s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(ApproachingAtTheForecourtHandler(service)).Name("ApproachingAtTheForecourt") - } else { - s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Handler(fallback).Name("ApproachingAtTheForecourt") - } + s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}/wait-for-status-change").Name("WaitOnPumpStatusChange").Handler(WaitOnPumpStatusChangeHandlerWithFallbackHelper(service, fallback)) + s2.Methods("GET").Path("/gas-stations/{gasStationId}/pumps/{pumpId}").Name("GetPump").Handler(GetPumpHandlerWithFallbackHelper(service, fallback)) + s2.Methods("POST").Path("/gas-station/{gasStationId}/payment").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, fallback)) + s2.Methods("POST").Path("/gas-stations/{gasStationId}/approaching").Name("ApproachingAtTheForecourt").Handler(ApproachingAtTheForecourtHandlerWithFallbackHelper(service, fallback)) return router } diff --git a/http/jsonapi/generator/internal/pay/open-api_test.go b/http/jsonapi/generator/internal/pay/open-api_test.go index e8cbeb92..e857dc08 100644 --- a/http/jsonapi/generator/internal/pay/open-api_test.go +++ b/http/jsonapi/generator/internal/pay/open-api_test.go @@ -1015,59 +1015,99 @@ type Service interface { ProcessPaymentHandlerService } -/* -Router implements: PACE Payment API - -Welcome to the PACE Payment API documentation. -This API is responsible for managing payment methods for users as well as authorizing payments on behalf of PACE services. -*/ -func Router(service interface{}, authBackend AuthorizationBackend) *mux.Router { - router := mux.NewRouter() - authBackend.InitOAuth2(cfgOAuth2) - authBackend.InitOpenID(cfgOpenID) - authBackend.InitProfileKey(cfgProfileKey) - // Subrouter s1 - Path: /pay - s1 := router.PathPrefix("/pay").Subrouter() +// DeletePaymentTokenHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeletePaymentTokenHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeletePaymentTokenHandlerService); ok { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Handler(DeletePaymentTokenHandler(service, authBackend)).Name("DeletePaymentToken") + return DeletePaymentTokenHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Handler(router.NotFoundHandler).Name("DeletePaymentToken") + return fallback } +} + +// AuthorizePaymentMethodHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func AuthorizePaymentMethodHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(AuthorizePaymentMethodHandlerService); ok { - s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Handler(AuthorizePaymentMethodHandler(service, authBackend)).Name("AuthorizePaymentMethod") + return AuthorizePaymentMethodHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Handler(router.NotFoundHandler).Name("AuthorizePaymentMethod") + return fallback } +} + +// CreatePaymentMethodSEPAHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func CreatePaymentMethodSEPAHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(CreatePaymentMethodSEPAHandlerService); ok { - s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Handler(CreatePaymentMethodSEPAHandler(service, authBackend)).Name("CreatePaymentMethodSEPA") + return CreatePaymentMethodSEPAHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Handler(router.NotFoundHandler).Name("CreatePaymentMethodSEPA") + return fallback } +} + +// DeletePaymentMethodHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeletePaymentMethodHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeletePaymentMethodHandlerService); ok { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Handler(DeletePaymentMethodHandler(service, authBackend)).Name("DeletePaymentMethod") + return DeletePaymentMethodHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Handler(router.NotFoundHandler).Name("DeletePaymentMethod") + return fallback } +} + +// ProcessPaymentHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func ProcessPaymentHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(ProcessPaymentHandlerService); ok { - s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Handler(ProcessPaymentHandler(service, authBackend)).Name("ProcessPayment") + return ProcessPaymentHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Handler(router.NotFoundHandler).Name("ProcessPayment") + return fallback } +} + +// GetPaymentMethodsIncludingCreditCheckHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPaymentMethodsIncludingCreditCheckHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPaymentMethodsIncludingCreditCheckHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsIncludingCreditCheckHandler(service, authBackend)).Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck") + return GetPaymentMethodsIncludingCreditCheckHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(router.NotFoundHandler).Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck") + return fallback } +} + +// GetPaymentMethodsIncludingPaymentTokenHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPaymentMethodsIncludingPaymentTokenHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPaymentMethodsIncludingPaymentTokenHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsIncludingPaymentTokenHandler(service, authBackend)).Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken") + return GetPaymentMethodsIncludingPaymentTokenHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(router.NotFoundHandler).Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken") + return fallback } +} + +// GetPaymentMethodsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPaymentMethodsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPaymentMethodsHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsHandler(service, authBackend)).Name("GetPaymentMethods") + return GetPaymentMethodsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(router.NotFoundHandler).Name("GetPaymentMethods") + return fallback } +} + +/* +Router implements: PACE Payment API + +Welcome to the PACE Payment API documentation. +This API is responsible for managing payment methods for users as well as authorizing payments on behalf of PACE services. +*/ +func Router(service interface{}, authBackend AuthorizationBackend) *mux.Router { + router := mux.NewRouter() + authBackend.InitOAuth2(cfgOAuth2) + authBackend.InitOpenID(cfgOpenID) + authBackend.InitProfileKey(cfgProfileKey) + // Subrouter s1 - Path: /pay + s1 := router.PathPrefix("/pay").Subrouter() + s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Name("DeletePaymentToken").Handler(DeletePaymentTokenHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Name("AuthorizePaymentMethod").Handler(AuthorizePaymentMethodHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Name("CreatePaymentMethodSEPA").Handler(CreatePaymentMethodSEPAHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Name("DeletePaymentMethod").Handler(DeletePaymentMethodHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck").Handler(GetPaymentMethodsIncludingCreditCheckHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken").Handler(GetPaymentMethodsIncludingPaymentTokenHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Name("GetPaymentMethods").Handler(GetPaymentMethodsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) return router } @@ -1084,45 +1124,13 @@ func RouterWithFallback(service interface{}, authBackend AuthorizationBackend, f authBackend.InitProfileKey(cfgProfileKey) // Subrouter s1 - Path: /pay s1 := router.PathPrefix("/pay").Subrouter() - if service, ok := service.(DeletePaymentTokenHandlerService); ok { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Handler(DeletePaymentTokenHandler(service, authBackend)).Name("DeletePaymentToken") - } else { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Handler(fallback).Name("DeletePaymentToken") - } - if service, ok := service.(AuthorizePaymentMethodHandlerService); ok { - s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Handler(AuthorizePaymentMethodHandler(service, authBackend)).Name("AuthorizePaymentMethod") - } else { - s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Handler(fallback).Name("AuthorizePaymentMethod") - } - if service, ok := service.(CreatePaymentMethodSEPAHandlerService); ok { - s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Handler(CreatePaymentMethodSEPAHandler(service, authBackend)).Name("CreatePaymentMethodSEPA") - } else { - s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Handler(fallback).Name("CreatePaymentMethodSEPA") - } - if service, ok := service.(DeletePaymentMethodHandlerService); ok { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Handler(DeletePaymentMethodHandler(service, authBackend)).Name("DeletePaymentMethod") - } else { - s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Handler(fallback).Name("DeletePaymentMethod") - } - if service, ok := service.(ProcessPaymentHandlerService); ok { - s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Handler(ProcessPaymentHandler(service, authBackend)).Name("ProcessPayment") - } else { - s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Handler(fallback).Name("ProcessPayment") - } - if service, ok := service.(GetPaymentMethodsIncludingCreditCheckHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsIncludingCreditCheckHandler(service, authBackend)).Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck") - } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(fallback).Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck") - } - if service, ok := service.(GetPaymentMethodsIncludingPaymentTokenHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsIncludingPaymentTokenHandler(service, authBackend)).Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken") - } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(fallback).Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken") - } - if service, ok := service.(GetPaymentMethodsHandlerService); ok { - s1.Methods("GET").Path("/beta/payment-methods").Handler(GetPaymentMethodsHandler(service, authBackend)).Name("GetPaymentMethods") - } else { - s1.Methods("GET").Path("/beta/payment-methods").Handler(fallback).Name("GetPaymentMethods") - } + s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}/paymentTokens/{paymentTokenId}").Name("DeletePaymentToken").Handler(DeletePaymentTokenHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/payment-methods/{paymentMethodId}/authorize").Name("AuthorizePaymentMethod").Handler(AuthorizePaymentMethodHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/payment-methods/sepa-direct-debit").Name("CreatePaymentMethodSEPA").Handler(CreatePaymentMethodSEPAHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("DELETE").Path("/beta/payment-methods/{paymentMethodId}").Name("DeletePaymentMethod").Handler(DeletePaymentMethodHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/transaction/{pathDecimal}").Name("ProcessPayment").Handler(ProcessPaymentHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Queries("include", "creditCheck").Name("GetPaymentMethodsIncludingCreditCheck").Handler(GetPaymentMethodsIncludingCreditCheckHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Queries("include", "paymentToken").Name("GetPaymentMethodsIncludingPaymentToken").Handler(GetPaymentMethodsIncludingPaymentTokenHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/payment-methods").Name("GetPaymentMethods").Handler(GetPaymentMethodsHandlerWithFallbackHelper(service, fallback, authBackend)) return router } diff --git a/http/jsonapi/generator/internal/poi/open-api_test.go b/http/jsonapi/generator/internal/poi/open-api_test.go index 965e78c4..361afc50 100644 --- a/http/jsonapi/generator/internal/poi/open-api_test.go +++ b/http/jsonapi/generator/internal/poi/open-api_test.go @@ -4379,198 +4379,378 @@ type Service interface { GetTilesHandlerService } -/* -Router implements: PACE POI API - -POI API -*/ -func Router(service interface{}, authBackend AuthorizationBackend) *mux.Router { - router := mux.NewRouter() - authBackend.InitDeviceID(cfgDeviceID) - authBackend.InitOAuth2(cfgOAuth2) - authBackend.InitOIDC(cfgOIDC) - // Subrouter s1 - Path: /poi - s1 := router.PathPrefix("/poi").Subrouter() +// DeleteGasStationReferenceStatusHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeleteGasStationReferenceStatusHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeleteGasStationReferenceStatusHandlerService); ok { - s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(DeleteGasStationReferenceStatusHandler(service, authBackend)).Name("DeleteGasStationReferenceStatus") + return DeleteGasStationReferenceStatusHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(router.NotFoundHandler).Name("DeleteGasStationReferenceStatus") + return fallback } +} + +// PutGasStationReferenceStatusHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func PutGasStationReferenceStatusHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(PutGasStationReferenceStatusHandlerService); ok { - s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(PutGasStationReferenceStatusHandler(service, authBackend)).Name("PutGasStationReferenceStatus") + return PutGasStationReferenceStatusHandler(service, authBackend) } else { - s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(router.NotFoundHandler).Name("PutGasStationReferenceStatus") + return fallback } +} + +// GetAppPOIsRelationshipsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetAppPOIsRelationshipsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetAppPOIsRelationshipsHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Handler(GetAppPOIsRelationshipsHandler(service, authBackend)).Name("GetAppPOIsRelationships") + return GetAppPOIsRelationshipsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Handler(router.NotFoundHandler).Name("GetAppPOIsRelationships") + return fallback } +} + +// UpdateAppPOIsRelationshipsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateAppPOIsRelationshipsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(UpdateAppPOIsRelationshipsHandlerService); ok { - s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Handler(UpdateAppPOIsRelationshipsHandler(service, authBackend)).Name("UpdateAppPOIsRelationships") + return UpdateAppPOIsRelationshipsHandler(service, authBackend) } else { - s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Handler(router.NotFoundHandler).Name("UpdateAppPOIsRelationships") + return fallback } +} + +// GetPriceHistoryHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPriceHistoryHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPriceHistoryHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Handler(GetPriceHistoryHandler(service, authBackend)).Name("GetPriceHistory") + return GetPriceHistoryHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Handler(router.NotFoundHandler).Name("GetPriceHistory") + return fallback } +} + +// DeduplicatePoiHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeduplicatePoiHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeduplicatePoiHandlerService); ok { - s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Handler(DeduplicatePoiHandler(service, authBackend)).Name("DeduplicatePoi") + return DeduplicatePoiHandler(service, authBackend) } else { - s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Handler(router.NotFoundHandler).Name("DeduplicatePoi") + return fallback } +} + +// MovePoiAtPositionHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func MovePoiAtPositionHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(MovePoiAtPositionHandlerService); ok { - s1.Methods("PATCH").Path("/beta/admin/poi/move").Handler(MovePoiAtPositionHandler(service, authBackend)).Name("MovePoiAtPosition") + return MovePoiAtPositionHandler(service, authBackend) } else { - s1.Methods("PATCH").Path("/beta/admin/poi/move").Handler(router.NotFoundHandler).Name("MovePoiAtPosition") + return fallback } +} + +// GetDuplicatesKMLHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetDuplicatesKMLHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetDuplicatesKMLHandlerService); ok { - s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Handler(GetDuplicatesKMLHandler(service, authBackend)).Name("GetDuplicatesKML") + return GetDuplicatesKMLHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Handler(router.NotFoundHandler).Name("GetDuplicatesKML") + return fallback } +} + +// GetGasStationFuelTypeNameMappingHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetGasStationFuelTypeNameMappingHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetGasStationFuelTypeNameMappingHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Handler(GetGasStationFuelTypeNameMappingHandler(service, authBackend)).Name("GetGasStationFuelTypeNameMapping") + return GetGasStationFuelTypeNameMappingHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Handler(router.NotFoundHandler).Name("GetGasStationFuelTypeNameMapping") + return fallback } +} + +// CheckForPaceAppHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func CheckForPaceAppHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(CheckForPaceAppHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/query").Handler(CheckForPaceAppHandler(service, authBackend)).Name("CheckForPaceApp") + return CheckForPaceAppHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/apps/query").Handler(router.NotFoundHandler).Name("CheckForPaceApp") + return fallback } +} + +// GetPoisDumpHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPoisDumpHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPoisDumpHandlerService); ok { - s1.Methods("GET").Path("/beta/datadumps/pois").Handler(GetPoisDumpHandler(service, authBackend)).Name("GetPoisDump") + return GetPoisDumpHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/datadumps/pois").Handler(router.NotFoundHandler).Name("GetPoisDump") + return fallback } +} + +// GetRegionalPricesHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetRegionalPricesHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetRegionalPricesHandlerService); ok { - s1.Methods("GET").Path("/beta/prices/regional").Handler(GetRegionalPricesHandler(service, authBackend)).Name("GetRegionalPrices") + return GetRegionalPricesHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/prices/regional").Handler(router.NotFoundHandler).Name("GetRegionalPrices") + return fallback } +} + +// GetTilesHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetTilesHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetTilesHandlerService); ok { - s1.Methods("POST").Path("/v1/tiles/query").Handler(GetTilesHandler(service, authBackend)).Name("GetTiles") + return GetTilesHandler(service, authBackend) } else { - s1.Methods("POST").Path("/v1/tiles/query").Handler(router.NotFoundHandler).Name("GetTiles") + return fallback } +} + +// DeleteAppHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeleteAppHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeleteAppHandlerService); ok { - s1.Methods("DELETE").Path("/beta/apps/{appID}").Handler(DeleteAppHandler(service, authBackend)).Name("DeleteApp") + return DeleteAppHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/apps/{appID}").Handler(router.NotFoundHandler).Name("DeleteApp") + return fallback } +} + +// GetAppHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetAppHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetAppHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/{appID}").Handler(GetAppHandler(service, authBackend)).Name("GetApp") + return GetAppHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/apps/{appID}").Handler(router.NotFoundHandler).Name("GetApp") + return fallback } +} + +// UpdateAppHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateAppHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(UpdateAppHandlerService); ok { - s1.Methods("PUT").Path("/beta/apps/{appID}").Handler(UpdateAppHandler(service, authBackend)).Name("UpdateApp") + return UpdateAppHandler(service, authBackend) } else { - s1.Methods("PUT").Path("/beta/apps/{appID}").Handler(router.NotFoundHandler).Name("UpdateApp") + return fallback } +} + +// GetGasStationHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetGasStationHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetGasStationHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}").Handler(GetGasStationHandler(service, authBackend)).Name("GetGasStation") + return GetGasStationHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}").Handler(router.NotFoundHandler).Name("GetGasStation") + return fallback } +} + +// GetPoiHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPoiHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPoiHandlerService); ok { - s1.Methods("GET").Path("/beta/pois/{poiId}").Handler(GetPoiHandler(service, authBackend)).Name("GetPoi") + return GetPoiHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/pois/{poiId}").Handler(router.NotFoundHandler).Name("GetPoi") + return fallback } +} + +// ChangePoiHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func ChangePoiHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(ChangePoiHandlerService); ok { - s1.Methods("PATCH").Path("/beta/pois/{poiId}").Handler(ChangePoiHandler(service, authBackend)).Name("ChangePoi") + return ChangePoiHandler(service, authBackend) } else { - s1.Methods("PATCH").Path("/beta/pois/{poiId}").Handler(router.NotFoundHandler).Name("ChangePoi") + return fallback } +} + +// GetPolicyHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPolicyHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPolicyHandlerService); ok { - s1.Methods("GET").Path("/beta/policies/{policyId}").Handler(GetPolicyHandler(service, authBackend)).Name("GetPolicy") + return GetPolicyHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/policies/{policyId}").Handler(router.NotFoundHandler).Name("GetPolicy") + return fallback } +} + +// DeleteSourceHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeleteSourceHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeleteSourceHandlerService); ok { - s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Handler(DeleteSourceHandler(service, authBackend)).Name("DeleteSource") + return DeleteSourceHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Handler(router.NotFoundHandler).Name("DeleteSource") + return fallback } +} + +// GetSourceHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetSourceHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetSourceHandlerService); ok { - s1.Methods("GET").Path("/beta/sources/{sourceId}").Handler(GetSourceHandler(service, authBackend)).Name("GetSource") + return GetSourceHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/sources/{sourceId}").Handler(router.NotFoundHandler).Name("GetSource") + return fallback } +} + +// UpdateSourceHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func UpdateSourceHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(UpdateSourceHandlerService); ok { - s1.Methods("PUT").Path("/beta/sources/{sourceId}").Handler(UpdateSourceHandler(service, authBackend)).Name("UpdateSource") + return UpdateSourceHandler(service, authBackend) } else { - s1.Methods("PUT").Path("/beta/sources/{sourceId}").Handler(router.NotFoundHandler).Name("UpdateSource") + return fallback } +} + +// DeleteSubscriptionHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func DeleteSubscriptionHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(DeleteSubscriptionHandlerService); ok { - s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Handler(DeleteSubscriptionHandler(service, authBackend)).Name("DeleteSubscription") + return DeleteSubscriptionHandler(service, authBackend) } else { - s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Handler(router.NotFoundHandler).Name("DeleteSubscription") + return fallback } +} + +// StoreSubscriptionHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func StoreSubscriptionHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(StoreSubscriptionHandlerService); ok { - s1.Methods("PUT").Path("/beta/subscriptions/{id}").Handler(StoreSubscriptionHandler(service, authBackend)).Name("StoreSubscription") + return StoreSubscriptionHandler(service, authBackend) } else { - s1.Methods("PUT").Path("/beta/subscriptions/{id}").Handler(router.NotFoundHandler).Name("StoreSubscription") + return fallback } +} + +// GetAppsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetAppsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetAppsHandlerService); ok { - s1.Methods("GET").Path("/beta/apps").Handler(GetAppsHandler(service, authBackend)).Name("GetApps") + return GetAppsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/apps").Handler(router.NotFoundHandler).Name("GetApps") + return fallback } +} + +// CreateAppHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func CreateAppHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(CreateAppHandlerService); ok { - s1.Methods("POST").Path("/beta/apps").Handler(CreateAppHandler(service, authBackend)).Name("CreateApp") + return CreateAppHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/apps").Handler(router.NotFoundHandler).Name("CreateApp") + return fallback } +} + +// GetEventsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetEventsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetEventsHandlerService); ok { - s1.Methods("GET").Path("/beta/events").Handler(GetEventsHandler(service, authBackend)).Name("GetEvents") + return GetEventsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/events").Handler(router.NotFoundHandler).Name("GetEvents") + return fallback } +} + +// GetGasStationsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetGasStationsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetGasStationsHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations").Handler(GetGasStationsHandler(service, authBackend)).Name("GetGasStations") + return GetGasStationsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/gas-stations").Handler(router.NotFoundHandler).Name("GetGasStations") + return fallback } +} + +// GetMetadataFiltersHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetMetadataFiltersHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetMetadataFiltersHandlerService); ok { - s1.Methods("GET").Path("/beta/meta").Handler(GetMetadataFiltersHandler(service, authBackend)).Name("GetMetadataFilters") + return GetMetadataFiltersHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/meta").Handler(router.NotFoundHandler).Name("GetMetadataFilters") + return fallback } +} + +// GetPoisHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPoisHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPoisHandlerService); ok { - s1.Methods("GET").Path("/beta/pois").Handler(GetPoisHandler(service, authBackend)).Name("GetPois") + return GetPoisHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/pois").Handler(router.NotFoundHandler).Name("GetPois") + return fallback } +} + +// GetPoliciesHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetPoliciesHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetPoliciesHandlerService); ok { - s1.Methods("GET").Path("/beta/policies").Handler(GetPoliciesHandler(service, authBackend)).Name("GetPolicies") + return GetPoliciesHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/policies").Handler(router.NotFoundHandler).Name("GetPolicies") + return fallback } +} + +// CreatePolicyHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func CreatePolicyHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(CreatePolicyHandlerService); ok { - s1.Methods("POST").Path("/beta/policies").Handler(CreatePolicyHandler(service, authBackend)).Name("CreatePolicy") + return CreatePolicyHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/policies").Handler(router.NotFoundHandler).Name("CreatePolicy") + return fallback } +} + +// GetSourcesHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetSourcesHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetSourcesHandlerService); ok { - s1.Methods("GET").Path("/beta/sources").Handler(GetSourcesHandler(service, authBackend)).Name("GetSources") + return GetSourcesHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/sources").Handler(router.NotFoundHandler).Name("GetSources") + return fallback } +} + +// CreateSourceHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func CreateSourceHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(CreateSourceHandlerService); ok { - s1.Methods("POST").Path("/beta/sources").Handler(CreateSourceHandler(service, authBackend)).Name("CreateSource") + return CreateSourceHandler(service, authBackend) } else { - s1.Methods("POST").Path("/beta/sources").Handler(router.NotFoundHandler).Name("CreateSource") + return fallback } +} + +// GetSubscriptionsHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetSubscriptionsHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { if service, ok := service.(GetSubscriptionsHandlerService); ok { - s1.Methods("GET").Path("/beta/subscriptions").Handler(GetSubscriptionsHandler(service, authBackend)).Name("GetSubscriptions") + return GetSubscriptionsHandler(service, authBackend) } else { - s1.Methods("GET").Path("/beta/subscriptions").Handler(router.NotFoundHandler).Name("GetSubscriptions") + return fallback } +} + +/* +Router implements: PACE POI API + +POI API +*/ +func Router(service interface{}, authBackend AuthorizationBackend) *mux.Router { + router := mux.NewRouter() + authBackend.InitDeviceID(cfgDeviceID) + authBackend.InitOAuth2(cfgOAuth2) + authBackend.InitOIDC(cfgOIDC) + // Subrouter s1 - Path: /poi + s1 := router.PathPrefix("/poi").Subrouter() + s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Name("DeleteGasStationReferenceStatus").Handler(DeleteGasStationReferenceStatusHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Name("PutGasStationReferenceStatus").Handler(PutGasStationReferenceStatusHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Name("GetAppPOIsRelationships").Handler(GetAppPOIsRelationshipsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Name("UpdateAppPOIsRelationships").Handler(UpdateAppPOIsRelationshipsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Name("GetPriceHistory").Handler(GetPriceHistoryHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Name("DeduplicatePoi").Handler(DeduplicatePoiHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PATCH").Path("/beta/admin/poi/move").Name("MovePoiAtPosition").Handler(MovePoiAtPositionHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Name("GetDuplicatesKML").Handler(GetDuplicatesKMLHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Name("GetGasStationFuelTypeNameMapping").Handler(GetGasStationFuelTypeNameMappingHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/apps/query").Name("CheckForPaceApp").Handler(CheckForPaceAppHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/datadumps/pois").Name("GetPoisDump").Handler(GetPoisDumpHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/prices/regional").Name("GetRegionalPrices").Handler(GetRegionalPricesHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/v1/tiles/query").Name("GetTiles").Handler(GetTilesHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("DELETE").Path("/beta/apps/{appID}").Name("DeleteApp").Handler(DeleteAppHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/apps/{appID}").Name("GetApp").Handler(GetAppHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PUT").Path("/beta/apps/{appID}").Name("UpdateApp").Handler(UpdateAppHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}").Name("GetGasStation").Handler(GetGasStationHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/pois/{poiId}").Name("GetPoi").Handler(GetPoiHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PATCH").Path("/beta/pois/{poiId}").Name("ChangePoi").Handler(ChangePoiHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/policies/{policyId}").Name("GetPolicy").Handler(GetPolicyHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Name("DeleteSource").Handler(DeleteSourceHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/sources/{sourceId}").Name("GetSource").Handler(GetSourceHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PUT").Path("/beta/sources/{sourceId}").Name("UpdateSource").Handler(UpdateSourceHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Name("DeleteSubscription").Handler(DeleteSubscriptionHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("PUT").Path("/beta/subscriptions/{id}").Name("StoreSubscription").Handler(StoreSubscriptionHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/apps").Name("GetApps").Handler(GetAppsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/apps").Name("CreateApp").Handler(CreateAppHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/events").Name("GetEvents").Handler(GetEventsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations").Name("GetGasStations").Handler(GetGasStationsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/meta").Name("GetMetadataFilters").Handler(GetMetadataFiltersHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/pois").Name("GetPois").Handler(GetPoisHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/policies").Name("GetPolicies").Handler(GetPoliciesHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/policies").Name("CreatePolicy").Handler(CreatePolicyHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/sources").Name("GetSources").Handler(GetSourcesHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("POST").Path("/beta/sources").Name("CreateSource").Handler(CreateSourceHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) + s1.Methods("GET").Path("/beta/subscriptions").Name("GetSubscriptions").Handler(GetSubscriptionsHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) return router } @@ -4586,185 +4766,41 @@ func RouterWithFallback(service interface{}, authBackend AuthorizationBackend, f authBackend.InitOIDC(cfgOIDC) // Subrouter s1 - Path: /poi s1 := router.PathPrefix("/poi").Subrouter() - if service, ok := service.(DeleteGasStationReferenceStatusHandlerService); ok { - s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(DeleteGasStationReferenceStatusHandler(service, authBackend)).Name("DeleteGasStationReferenceStatus") - } else { - s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(fallback).Name("DeleteGasStationReferenceStatus") - } - if service, ok := service.(PutGasStationReferenceStatusHandlerService); ok { - s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(PutGasStationReferenceStatusHandler(service, authBackend)).Name("PutGasStationReferenceStatus") - } else { - s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Handler(fallback).Name("PutGasStationReferenceStatus") - } - if service, ok := service.(GetAppPOIsRelationshipsHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Handler(GetAppPOIsRelationshipsHandler(service, authBackend)).Name("GetAppPOIsRelationships") - } else { - s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Handler(fallback).Name("GetAppPOIsRelationships") - } - if service, ok := service.(UpdateAppPOIsRelationshipsHandlerService); ok { - s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Handler(UpdateAppPOIsRelationshipsHandler(service, authBackend)).Name("UpdateAppPOIsRelationships") - } else { - s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Handler(fallback).Name("UpdateAppPOIsRelationships") - } - if service, ok := service.(GetPriceHistoryHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Handler(GetPriceHistoryHandler(service, authBackend)).Name("GetPriceHistory") - } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Handler(fallback).Name("GetPriceHistory") - } - if service, ok := service.(DeduplicatePoiHandlerService); ok { - s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Handler(DeduplicatePoiHandler(service, authBackend)).Name("DeduplicatePoi") - } else { - s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Handler(fallback).Name("DeduplicatePoi") - } - if service, ok := service.(MovePoiAtPositionHandlerService); ok { - s1.Methods("PATCH").Path("/beta/admin/poi/move").Handler(MovePoiAtPositionHandler(service, authBackend)).Name("MovePoiAtPosition") - } else { - s1.Methods("PATCH").Path("/beta/admin/poi/move").Handler(fallback).Name("MovePoiAtPosition") - } - if service, ok := service.(GetDuplicatesKMLHandlerService); ok { - s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Handler(GetDuplicatesKMLHandler(service, authBackend)).Name("GetDuplicatesKML") - } else { - s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Handler(fallback).Name("GetDuplicatesKML") - } - if service, ok := service.(GetGasStationFuelTypeNameMappingHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Handler(GetGasStationFuelTypeNameMappingHandler(service, authBackend)).Name("GetGasStationFuelTypeNameMapping") - } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Handler(fallback).Name("GetGasStationFuelTypeNameMapping") - } - if service, ok := service.(CheckForPaceAppHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/query").Handler(CheckForPaceAppHandler(service, authBackend)).Name("CheckForPaceApp") - } else { - s1.Methods("GET").Path("/beta/apps/query").Handler(fallback).Name("CheckForPaceApp") - } - if service, ok := service.(GetPoisDumpHandlerService); ok { - s1.Methods("GET").Path("/beta/datadumps/pois").Handler(GetPoisDumpHandler(service, authBackend)).Name("GetPoisDump") - } else { - s1.Methods("GET").Path("/beta/datadumps/pois").Handler(fallback).Name("GetPoisDump") - } - if service, ok := service.(GetRegionalPricesHandlerService); ok { - s1.Methods("GET").Path("/beta/prices/regional").Handler(GetRegionalPricesHandler(service, authBackend)).Name("GetRegionalPrices") - } else { - s1.Methods("GET").Path("/beta/prices/regional").Handler(fallback).Name("GetRegionalPrices") - } - if service, ok := service.(GetTilesHandlerService); ok { - s1.Methods("POST").Path("/v1/tiles/query").Handler(GetTilesHandler(service, authBackend)).Name("GetTiles") - } else { - s1.Methods("POST").Path("/v1/tiles/query").Handler(fallback).Name("GetTiles") - } - if service, ok := service.(DeleteAppHandlerService); ok { - s1.Methods("DELETE").Path("/beta/apps/{appID}").Handler(DeleteAppHandler(service, authBackend)).Name("DeleteApp") - } else { - s1.Methods("DELETE").Path("/beta/apps/{appID}").Handler(fallback).Name("DeleteApp") - } - if service, ok := service.(GetAppHandlerService); ok { - s1.Methods("GET").Path("/beta/apps/{appID}").Handler(GetAppHandler(service, authBackend)).Name("GetApp") - } else { - s1.Methods("GET").Path("/beta/apps/{appID}").Handler(fallback).Name("GetApp") - } - if service, ok := service.(UpdateAppHandlerService); ok { - s1.Methods("PUT").Path("/beta/apps/{appID}").Handler(UpdateAppHandler(service, authBackend)).Name("UpdateApp") - } else { - s1.Methods("PUT").Path("/beta/apps/{appID}").Handler(fallback).Name("UpdateApp") - } - if service, ok := service.(GetGasStationHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations/{id}").Handler(GetGasStationHandler(service, authBackend)).Name("GetGasStation") - } else { - s1.Methods("GET").Path("/beta/gas-stations/{id}").Handler(fallback).Name("GetGasStation") - } - if service, ok := service.(GetPoiHandlerService); ok { - s1.Methods("GET").Path("/beta/pois/{poiId}").Handler(GetPoiHandler(service, authBackend)).Name("GetPoi") - } else { - s1.Methods("GET").Path("/beta/pois/{poiId}").Handler(fallback).Name("GetPoi") - } - if service, ok := service.(ChangePoiHandlerService); ok { - s1.Methods("PATCH").Path("/beta/pois/{poiId}").Handler(ChangePoiHandler(service, authBackend)).Name("ChangePoi") - } else { - s1.Methods("PATCH").Path("/beta/pois/{poiId}").Handler(fallback).Name("ChangePoi") - } - if service, ok := service.(GetPolicyHandlerService); ok { - s1.Methods("GET").Path("/beta/policies/{policyId}").Handler(GetPolicyHandler(service, authBackend)).Name("GetPolicy") - } else { - s1.Methods("GET").Path("/beta/policies/{policyId}").Handler(fallback).Name("GetPolicy") - } - if service, ok := service.(DeleteSourceHandlerService); ok { - s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Handler(DeleteSourceHandler(service, authBackend)).Name("DeleteSource") - } else { - s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Handler(fallback).Name("DeleteSource") - } - if service, ok := service.(GetSourceHandlerService); ok { - s1.Methods("GET").Path("/beta/sources/{sourceId}").Handler(GetSourceHandler(service, authBackend)).Name("GetSource") - } else { - s1.Methods("GET").Path("/beta/sources/{sourceId}").Handler(fallback).Name("GetSource") - } - if service, ok := service.(UpdateSourceHandlerService); ok { - s1.Methods("PUT").Path("/beta/sources/{sourceId}").Handler(UpdateSourceHandler(service, authBackend)).Name("UpdateSource") - } else { - s1.Methods("PUT").Path("/beta/sources/{sourceId}").Handler(fallback).Name("UpdateSource") - } - if service, ok := service.(DeleteSubscriptionHandlerService); ok { - s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Handler(DeleteSubscriptionHandler(service, authBackend)).Name("DeleteSubscription") - } else { - s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Handler(fallback).Name("DeleteSubscription") - } - if service, ok := service.(StoreSubscriptionHandlerService); ok { - s1.Methods("PUT").Path("/beta/subscriptions/{id}").Handler(StoreSubscriptionHandler(service, authBackend)).Name("StoreSubscription") - } else { - s1.Methods("PUT").Path("/beta/subscriptions/{id}").Handler(fallback).Name("StoreSubscription") - } - if service, ok := service.(GetAppsHandlerService); ok { - s1.Methods("GET").Path("/beta/apps").Handler(GetAppsHandler(service, authBackend)).Name("GetApps") - } else { - s1.Methods("GET").Path("/beta/apps").Handler(fallback).Name("GetApps") - } - if service, ok := service.(CreateAppHandlerService); ok { - s1.Methods("POST").Path("/beta/apps").Handler(CreateAppHandler(service, authBackend)).Name("CreateApp") - } else { - s1.Methods("POST").Path("/beta/apps").Handler(fallback).Name("CreateApp") - } - if service, ok := service.(GetEventsHandlerService); ok { - s1.Methods("GET").Path("/beta/events").Handler(GetEventsHandler(service, authBackend)).Name("GetEvents") - } else { - s1.Methods("GET").Path("/beta/events").Handler(fallback).Name("GetEvents") - } - if service, ok := service.(GetGasStationsHandlerService); ok { - s1.Methods("GET").Path("/beta/gas-stations").Handler(GetGasStationsHandler(service, authBackend)).Name("GetGasStations") - } else { - s1.Methods("GET").Path("/beta/gas-stations").Handler(fallback).Name("GetGasStations") - } - if service, ok := service.(GetMetadataFiltersHandlerService); ok { - s1.Methods("GET").Path("/beta/meta").Handler(GetMetadataFiltersHandler(service, authBackend)).Name("GetMetadataFilters") - } else { - s1.Methods("GET").Path("/beta/meta").Handler(fallback).Name("GetMetadataFilters") - } - if service, ok := service.(GetPoisHandlerService); ok { - s1.Methods("GET").Path("/beta/pois").Handler(GetPoisHandler(service, authBackend)).Name("GetPois") - } else { - s1.Methods("GET").Path("/beta/pois").Handler(fallback).Name("GetPois") - } - if service, ok := service.(GetPoliciesHandlerService); ok { - s1.Methods("GET").Path("/beta/policies").Handler(GetPoliciesHandler(service, authBackend)).Name("GetPolicies") - } else { - s1.Methods("GET").Path("/beta/policies").Handler(fallback).Name("GetPolicies") - } - if service, ok := service.(CreatePolicyHandlerService); ok { - s1.Methods("POST").Path("/beta/policies").Handler(CreatePolicyHandler(service, authBackend)).Name("CreatePolicy") - } else { - s1.Methods("POST").Path("/beta/policies").Handler(fallback).Name("CreatePolicy") - } - if service, ok := service.(GetSourcesHandlerService); ok { - s1.Methods("GET").Path("/beta/sources").Handler(GetSourcesHandler(service, authBackend)).Name("GetSources") - } else { - s1.Methods("GET").Path("/beta/sources").Handler(fallback).Name("GetSources") - } - if service, ok := service.(CreateSourceHandlerService); ok { - s1.Methods("POST").Path("/beta/sources").Handler(CreateSourceHandler(service, authBackend)).Name("CreateSource") - } else { - s1.Methods("POST").Path("/beta/sources").Handler(fallback).Name("CreateSource") - } - if service, ok := service.(GetSubscriptionsHandlerService); ok { - s1.Methods("GET").Path("/beta/subscriptions").Handler(GetSubscriptionsHandler(service, authBackend)).Name("GetSubscriptions") - } else { - s1.Methods("GET").Path("/beta/subscriptions").Handler(fallback).Name("GetSubscriptions") - } + s1.Methods("DELETE").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Name("DeleteGasStationReferenceStatus").Handler(DeleteGasStationReferenceStatusHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PUT").Path("/beta/delivery/gas-stations/{gasStationId}/reference-status/{reference}").Name("PutGasStationReferenceStatus").Handler(PutGasStationReferenceStatusHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/apps/{appID}/relationships/pois").Name("GetAppPOIsRelationships").Handler(GetAppPOIsRelationshipsHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PATCH").Path("/beta/apps/{appID}/relationships/pois").Name("UpdateAppPOIsRelationships").Handler(UpdateAppPOIsRelationshipsHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}/fuel-price-histories/{fuel_type}").Name("GetPriceHistory").Handler(GetPriceHistoryHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PATCH").Path("/beta/admin/poi/dedupe").Name("DeduplicatePoi").Handler(DeduplicatePoiHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PATCH").Path("/beta/admin/poi/move").Name("MovePoiAtPosition").Handler(MovePoiAtPositionHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/datadumps/duplicatemap/{countryCode}").Name("GetDuplicatesKML").Handler(GetDuplicatesKMLHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}/fueltype").Name("GetGasStationFuelTypeNameMapping").Handler(GetGasStationFuelTypeNameMappingHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/apps/query").Name("CheckForPaceApp").Handler(CheckForPaceAppHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/datadumps/pois").Name("GetPoisDump").Handler(GetPoisDumpHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/prices/regional").Name("GetRegionalPrices").Handler(GetRegionalPricesHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/v1/tiles/query").Name("GetTiles").Handler(GetTilesHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("DELETE").Path("/beta/apps/{appID}").Name("DeleteApp").Handler(DeleteAppHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/apps/{appID}").Name("GetApp").Handler(GetAppHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PUT").Path("/beta/apps/{appID}").Name("UpdateApp").Handler(UpdateAppHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations/{id}").Name("GetGasStation").Handler(GetGasStationHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/pois/{poiId}").Name("GetPoi").Handler(GetPoiHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PATCH").Path("/beta/pois/{poiId}").Name("ChangePoi").Handler(ChangePoiHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/policies/{policyId}").Name("GetPolicy").Handler(GetPolicyHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("DELETE").Path("/beta/sources/{sourceId}").Name("DeleteSource").Handler(DeleteSourceHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/sources/{sourceId}").Name("GetSource").Handler(GetSourceHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PUT").Path("/beta/sources/{sourceId}").Name("UpdateSource").Handler(UpdateSourceHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("DELETE").Path("/beta/subscriptions/{id}").Name("DeleteSubscription").Handler(DeleteSubscriptionHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("PUT").Path("/beta/subscriptions/{id}").Name("StoreSubscription").Handler(StoreSubscriptionHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/apps").Name("GetApps").Handler(GetAppsHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/apps").Name("CreateApp").Handler(CreateAppHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/events").Name("GetEvents").Handler(GetEventsHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/gas-stations").Name("GetGasStations").Handler(GetGasStationsHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/meta").Name("GetMetadataFilters").Handler(GetMetadataFiltersHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/pois").Name("GetPois").Handler(GetPoisHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/policies").Name("GetPolicies").Handler(GetPoliciesHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/policies").Name("CreatePolicy").Handler(CreatePolicyHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/sources").Name("GetSources").Handler(GetSourcesHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("POST").Path("/beta/sources").Name("CreateSource").Handler(CreateSourceHandlerWithFallbackHelper(service, fallback, authBackend)) + s1.Methods("GET").Path("/beta/subscriptions").Name("GetSubscriptions").Handler(GetSubscriptionsHandlerWithFallbackHelper(service, fallback, authBackend)) return router } diff --git a/http/jsonapi/generator/internal/securitytest/open-api_test.go b/http/jsonapi/generator/internal/securitytest/open-api_test.go index a02fa981..76872c07 100644 --- a/http/jsonapi/generator/internal/securitytest/open-api_test.go +++ b/http/jsonapi/generator/internal/securitytest/open-api_test.go @@ -137,6 +137,15 @@ type Service interface { GetTestHandlerService } +// GetTestHandlerWithFallbackHelper helper that checks if the given service fulfills the interface. Returns fallback handler if not, otherwise returns matching handler. +func GetTestHandlerWithFallbackHelper(service interface{}, fallback http.Handler, authBackend AuthorizationBackend) http.Handler { + if service, ok := service.(GetTestHandlerService); ok { + return GetTestHandler(service, authBackend) + } else { + return fallback + } +} + /* Router implements: PACE Payment API @@ -149,11 +158,7 @@ func Router(service interface{}, authBackend AuthorizationBackend) *mux.Router { authBackend.InitProfileKey(cfgProfileKey) // Subrouter s1 - Path: /pay s1 := router.PathPrefix("/pay").Subrouter() - if service, ok := service.(GetTestHandlerService); ok { - s1.Methods("GET").Path("/beta/test").Handler(GetTestHandler(service, authBackend)).Name("GetTest") - } else { - s1.Methods("GET").Path("/beta/test").Handler(router.NotFoundHandler).Name("GetTest") - } + s1.Methods("GET").Path("/beta/test").Name("GetTest").Handler(GetTestHandlerWithFallbackHelper(service, router.NotFoundHandler, authBackend)) return router } @@ -169,10 +174,6 @@ func RouterWithFallback(service interface{}, authBackend AuthorizationBackend, f authBackend.InitProfileKey(cfgProfileKey) // Subrouter s1 - Path: /pay s1 := router.PathPrefix("/pay").Subrouter() - if service, ok := service.(GetTestHandlerService); ok { - s1.Methods("GET").Path("/beta/test").Handler(GetTestHandler(service, authBackend)).Name("GetTest") - } else { - s1.Methods("GET").Path("/beta/test").Handler(fallback).Name("GetTest") - } + s1.Methods("GET").Path("/beta/test").Name("GetTest").Handler(GetTestHandlerWithFallbackHelper(service, fallback, authBackend)) return router }