fleet/server/mdm/android/service/endpoint_utils.go
Victor Lyuboslavsky 386ce37168
Refactoring endpoint_utils (#26342)
For #26218 

Refactoring service/android endpoint_utils to remove duplication.
No functional changes.

- [x] Manual QA for all new/changed functionality
2025-02-18 11:09:43 -06:00

78 lines
2.2 KiB
Go

package service
import (
"context"
"io"
"net/http"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mdm/android"
"github.com/fleetdm/fleet/v4/server/service/middleware/auth"
eu "github.com/fleetdm/fleet/v4/server/service/middleware/endpoint_utils"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
)
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return eu.EncodeCommonResponse(ctx, w, response,
func(w http.ResponseWriter, response interface{}) error {
return json.MarshalWrite(w, response, jsontext.WithIndent(" "))
},
)
}
func makeDecoder(iface interface{}) kithttp.DecodeRequestFunc {
return eu.MakeDecoder(iface, func(body io.Reader, req any) error {
return json.UnmarshalRead(body, req)
}, nil, nil, nil)
}
// Compile-time check to ensure that endpointer implements Endpointer.
var _ eu.Endpointer[eu.AndroidFunc] = &endpointer{}
type endpointer struct {
svc android.Service
}
func (e *endpointer) CallHandlerFunc(f eu.AndroidFunc, ctx context.Context, request interface{},
svc interface{}) (fleet.Errorer, error) {
return f(ctx, request, svc.(android.Service)), nil
}
func (e *endpointer) Service() interface{} {
return e.svc
}
func newUserAuthenticatedEndpointer(fleetSvc fleet.Service, svc android.Service, opts []kithttp.ServerOption, r *mux.Router,
versions ...string) *eu.CommonEndpointer[eu.AndroidFunc] {
return &eu.CommonEndpointer[eu.AndroidFunc]{
EP: &endpointer{
svc: svc,
},
MakeDecoderFn: makeDecoder,
EncodeFn: encodeResponse,
Opts: opts,
AuthFunc: auth.AuthenticatedUser,
FleetService: fleetSvc,
Router: r,
Versions: versions,
}
}
func newNoAuthEndpointer(fleetSvc fleet.Service, svc android.Service, opts []kithttp.ServerOption, r *mux.Router,
versions ...string) *eu.CommonEndpointer[eu.AndroidFunc] {
return &eu.CommonEndpointer[eu.AndroidFunc]{
EP: &endpointer{
svc: svc,
},
MakeDecoderFn: makeDecoder,
EncodeFn: encodeResponse,
Opts: opts,
AuthFunc: auth.UnauthenticatedRequest,
FleetService: fleetSvc,
Router: r,
Versions: versions,
}
}