mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-09 02:55:59 +08:00
add common http functions to httputil pkg (#350)
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
package remove
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
type Endpoints struct {
|
||||
@@ -24,11 +22,7 @@ func MakeServerEndpoints(s Service) Endpoints {
|
||||
}
|
||||
|
||||
func MakeHTTPHandler(e Endpoints, logger log.Logger) http.Handler {
|
||||
options := []httptransport.ServerOption{
|
||||
httptransport.ServerErrorLogger(logger),
|
||||
}
|
||||
|
||||
r := mux.NewRouter()
|
||||
r, options := httputil.NewRouter(logger)
|
||||
|
||||
// POST /v1/devices/:udid/block force a device to unenroll next time it connects
|
||||
// POST /v1/devices/:udid/unblock allow a blocked device to enroll again
|
||||
@@ -36,33 +30,17 @@ func MakeHTTPHandler(e Endpoints, logger log.Logger) http.Handler {
|
||||
r.Methods("POST").Path("/v1/devices/{udid}/block").Handler(httptransport.NewServer(
|
||||
e.BlockDeviceEndpoint,
|
||||
decodeBlockDeviceRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
r.Methods("POST").Path("/v1/devices/{udid}/unblock").Handler(httptransport.NewServer(
|
||||
e.UnblockDeviceEndpoint,
|
||||
decodeUnblockDeviceRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
return r
|
||||
|
||||
}
|
||||
|
||||
type errorWrapper struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type errorer interface {
|
||||
error() error
|
||||
}
|
||||
|
||||
func errorDecoder(r *http.Response) error {
|
||||
var w errorWrapper
|
||||
if err := json.NewDecoder(r.Body).Decode(&w); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New(w.Error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user