Alerting: Refactor API handlers to use web.Bind (#42600)

* Alerting: Refactor API handlers to use web.Bind

* lint
This commit is contained in:
Sofia Papagiannaki
2021-12-13 09:22:57 +01:00
committed by GitHub
parent fd48aee61e
commit c6483cd8ed
13 changed files with 367 additions and 113 deletions
@@ -13,15 +13,33 @@ import (
"github.com/grafana/grafana/pkg/middleware"
)
type {{classname}}ForkingService interface { {{#operation}}
{{nickname}}(*models.ReqContext) response.Response{{/operation}}
}
type {{classname}}Service interface { {{#operation}}
{{nickname}}(*models.ReqContext{{#bodyParams}}, apimodels.{{dataType}}{{/bodyParams}}) response.Response{{/operation}}
}
func (api *API) Register{{classname}}Endpoints(srv {{classname}}Service, m *metrics.API) {
{{#operations}}{{#operation}}
func (f *Forked{{classname}}) {{nickname}}(ctx *models.ReqContext) response.Response {
{{#bodyParams}}
conf := apimodels.{{dataType}}{}
if err := web.Bind(ctx.Req, &conf); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
return f.fork{{nickname}}(ctx, conf)
{{/bodyParams}}
{{^bodyParams}}
return f.fork{{nickname}}(ctx)
{{/bodyParams}}
}
{{/operation}}{{/operations}}
func (api *API) Register{{classname}}Endpoints(srv {{classname}}ForkingService, m *metrics.API) {
api.RouteRegister.Group("", func(group routing.RouteRegister){ {{#operations}}{{#operation}}
group.{{httpMethod}}(
toMacaronPath("{{{path}}}"){{#bodyParams}},
binding.Bind(apimodels.{{dataType}}{}){{/bodyParams}},
toMacaronPath("{{{path}}}"),
metrics.Instrument(
http.Method{{httpMethod}},
"{{{path}}}",
@@ -31,4 +49,4 @@ func (api *API) Register{{classname}}Endpoints(srv {{classname}}Service, m *metr
){{/operation}}{{/operations}}
}, middleware.ReqSignedIn)
}{{#operation}}
{{/operation}}{{/operations}}
{{/operation}}{{/operations}}