core/zero: add report-usage API (#5276)

This commit is contained in:
Caleb Doxsey 2024-09-11 08:52:56 -06:00 committed by GitHub
parent 790c11b368
commit 1e5f623c0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 276 additions and 0 deletions

View file

@ -31,6 +31,9 @@ type ServerInterface interface {
// (POST /exchangeToken)
ExchangeClusterIdentityToken(w http.ResponseWriter, r *http.Request)
// (POST /reportUsage)
ReportUsage(w http.ResponseWriter, r *http.Request)
}
// Unimplemented server implementation that returns http.StatusNotImplemented for each endpoint.
@ -62,6 +65,11 @@ func (_ Unimplemented) ExchangeClusterIdentityToken(w http.ResponseWriter, r *ht
w.WriteHeader(http.StatusNotImplemented)
}
// (POST /reportUsage)
func (_ Unimplemented) ReportUsage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}
// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
@ -176,6 +184,23 @@ func (siw *ServerInterfaceWrapper) ExchangeClusterIdentityToken(w http.ResponseW
handler.ServeHTTP(w, r.WithContext(ctx))
}
// ReportUsage operation middleware
func (siw *ServerInterfaceWrapper) ReportUsage(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, BearerAuthScopes, []string{})
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.ReportUsage(w, r)
}))
for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- {
handler = siw.HandlerMiddlewares[i](handler)
}
handler.ServeHTTP(w, r.WithContext(ctx))
}
type UnescapedCookieParamError struct {
ParamName string
Err error
@ -304,6 +329,9 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/exchangeToken", wrapper.ExchangeClusterIdentityToken)
})
r.Group(func(r chi.Router) {
r.Post(options.BaseURL+"/reportUsage", wrapper.ReportUsage)
})
return r
}
@ -490,6 +518,22 @@ func (response ExchangeClusterIdentityToken500JSONResponse) VisitExchangeCluster
return json.NewEncoder(w).Encode(response)
}
type ReportUsageRequestObject struct {
Body *ReportUsageJSONRequestBody
}
type ReportUsageResponseObject interface {
VisitReportUsageResponse(w http.ResponseWriter) error
}
type ReportUsage204Response struct {
}
func (response ReportUsage204Response) VisitReportUsageResponse(w http.ResponseWriter) error {
w.WriteHeader(204)
return nil
}
// StrictServerInterface represents all server handlers.
type StrictServerInterface interface {
@ -507,6 +551,9 @@ type StrictServerInterface interface {
// (POST /exchangeToken)
ExchangeClusterIdentityToken(ctx context.Context, request ExchangeClusterIdentityTokenRequestObject) (ExchangeClusterIdentityTokenResponseObject, error)
// (POST /reportUsage)
ReportUsage(ctx context.Context, request ReportUsageRequestObject) (ReportUsageResponseObject, error)
}
type StrictHandlerFunc = strictnethttp.StrictHTTPHandlerFunc
@ -675,3 +722,34 @@ func (sh *strictHandler) ExchangeClusterIdentityToken(w http.ResponseWriter, r *
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
}
}
// ReportUsage operation middleware
func (sh *strictHandler) ReportUsage(w http.ResponseWriter, r *http.Request) {
var request ReportUsageRequestObject
var body ReportUsageJSONRequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
sh.options.RequestErrorHandlerFunc(w, r, fmt.Errorf("can't decode JSON body: %w", err))
return
}
request.Body = &body
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
return sh.ssi.ReportUsage(ctx, request.(ReportUsageRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "ReportUsage")
}
response, err := handler(r.Context(), w, r, request)
if err != nil {
sh.options.ResponseErrorHandlerFunc(w, r, err)
} else if validResponse, ok := response.(ReportUsageResponseObject); ok {
if err := validResponse.VisitReportUsageResponse(w); err != nil {
sh.options.ResponseErrorHandlerFunc(w, r, err)
}
} else if response != nil {
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
}
}