mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-08 10:45:34 +08:00
add common http functions to httputil pkg (#350)
This commit is contained in:
@@ -2,10 +2,10 @@ package blueprint
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *BlueprintService) ApplyBlueprint(ctx context.Context, bp *Blueprint) error {
|
||||
@@ -20,22 +20,17 @@ type applyBlueprintResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r applyBlueprintResponse) error() error { return r.Err }
|
||||
func (r applyBlueprintResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeApplyBlueprintRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var bpReq applyBlueprintRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&bpReq); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bpReq, nil
|
||||
var req applyBlueprintRequest
|
||||
err := httputil.DecodeJSONRequest(r, &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func decodeApplyBlueprintResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp applyBlueprintResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransport.ClientOption) (Service, error) {
|
||||
@@ -20,8 +19,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
applyBlueprintEndpoint = httptransport.NewClient(
|
||||
"PUT",
|
||||
copyURL(u, "/v1/blueprints"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/blueprints"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeApplyBlueprintResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -31,8 +30,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
getBlueprintsEndpoint = httptransport.NewClient(
|
||||
"GET",
|
||||
copyURL(u, "/v1/blueprints"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/blueprints"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeGetBlueprintsResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -42,8 +41,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
removeBlueprintsEndpoint = httptransport.NewClient(
|
||||
"DELETE",
|
||||
copyURL(u, "/v1/blueprints"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/blueprints"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeRemoveBlueprintsResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -55,16 +54,3 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
RemoveBlueprintsEndpoint: removeBlueprintsEndpoint,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func encodeRequestWithToken(token string, next httptransport.EncodeRequestFunc) httptransport.EncodeRequestFunc {
|
||||
return func(ctx context.Context, r *http.Request, request interface{}) error {
|
||||
r.SetBasicAuth("micromdm", token)
|
||||
return next(ctx, r, request)
|
||||
}
|
||||
}
|
||||
|
||||
func copyURL(base *url.URL, path string) *url.URL {
|
||||
next := *base
|
||||
next.Path = path
|
||||
return &next
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *BlueprintService) GetBlueprints(ctx context.Context, opt GetBlueprintsOption) ([]Blueprint, error) {
|
||||
@@ -30,7 +31,7 @@ type getBlueprintsResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r getBlueprintsResponse) error() error { return r.Err }
|
||||
func (r getBlueprintsResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeGetBlueprintsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var opts GetBlueprintsOption
|
||||
@@ -44,11 +45,8 @@ func decodeGetBlueprintsRequest(ctx context.Context, r *http.Request) (interface
|
||||
}
|
||||
|
||||
func decodeGetBlueprintsResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp getBlueprintsResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ package blueprint
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *BlueprintService) RemoveBlueprints(ctx context.Context, names []string) error {
|
||||
@@ -26,22 +26,17 @@ type removeBlueprintsResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r removeBlueprintsResponse) error() error { return r.Err }
|
||||
func (r removeBlueprintsResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeRemoveBlueprintsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req removeBlueprintsRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return req, nil
|
||||
err := httputil.DecodeJSONRequest(r, &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func decodeRemoveBlueprintsResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp removeBlueprintsResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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/pkg/errors"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
type Endpoints struct {
|
||||
@@ -19,17 +17,14 @@ type Endpoints struct {
|
||||
|
||||
func MakeServerEndpoints(s Service) Endpoints {
|
||||
return Endpoints{
|
||||
GetBlueprintsEndpoint: MakeGetBlueprintsEndpoint(s),
|
||||
ApplyBlueprintEndpoint: MakeApplyBlueprintEndpoint(s),
|
||||
GetBlueprintsEndpoint: MakeGetBlueprintsEndpoint(s),
|
||||
ApplyBlueprintEndpoint: MakeApplyBlueprintEndpoint(s),
|
||||
RemoveBlueprintsEndpoint: MakeRemoveBlueprintsEndpoint(s),
|
||||
}
|
||||
}
|
||||
|
||||
func MakeHTTPHandler(e Endpoints, logger log.Logger) http.Handler {
|
||||
options := []httptransport.ServerOption{
|
||||
httptransport.ServerErrorLogger(logger),
|
||||
}
|
||||
|
||||
r := mux.NewRouter()
|
||||
r, options := httputil.NewRouter(logger)
|
||||
|
||||
// PUT /v1/blueprints create or replace a blueprint on the server
|
||||
// GET /v1/blueprints get a list of blueprints managed by the server
|
||||
@@ -38,39 +33,23 @@ func MakeHTTPHandler(e Endpoints, logger log.Logger) http.Handler {
|
||||
r.Methods("PUT").Path("/v1/blueprints").Handler(httptransport.NewServer(
|
||||
e.ApplyBlueprintEndpoint,
|
||||
decodeApplyBlueprintRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
r.Methods("GET").Path("/v1/blueprints").Handler(httptransport.NewServer(
|
||||
e.GetBlueprintsEndpoint,
|
||||
decodeGetBlueprintsRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
r.Methods("DELETE").Path("/v1/blueprints").Handler(httptransport.NewServer(
|
||||
e.RemoveBlueprintsEndpoint,
|
||||
decodeRemoveBlueprintsRequest,
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ package profile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *ProfileService) ApplyProfile(ctx context.Context, p *Profile) error {
|
||||
@@ -20,22 +20,17 @@ type applyProfileResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r applyProfileResponse) error() error { return r.Err }
|
||||
func (r applyProfileResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeApplyProfileRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req applyProfileRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return req, nil
|
||||
err := httputil.DecodeJSONRequest(r, &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func decodeApplyProfileResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp applyProfileResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package profile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransport.ClientOption) (Service, error) {
|
||||
@@ -20,8 +19,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
applyProfileEndpoint = httptransport.NewClient(
|
||||
"PUT",
|
||||
copyURL(u, "/v1/profiles"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/profiles"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeApplyProfileResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -31,8 +30,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
getProfilesEndpoint = httptransport.NewClient(
|
||||
"GET",
|
||||
copyURL(u, "/v1/profiles"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/profiles"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeGetProfilesResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -42,8 +41,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
removeProfilesEndpoint = httptransport.NewClient(
|
||||
"DELETE",
|
||||
copyURL(u, "/v1/profiles"),
|
||||
encodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
httputil.CopyURL(u, "/v1/profiles"),
|
||||
httputil.EncodeRequestWithToken(token, httptransport.EncodeJSONRequest),
|
||||
decodeRemoveProfileResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -55,16 +54,3 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
RemoveProfilesEndpoint: removeProfilesEndpoint,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func encodeRequestWithToken(token string, next httptransport.EncodeRequestFunc) httptransport.EncodeRequestFunc {
|
||||
return func(ctx context.Context, r *http.Request, request interface{}) error {
|
||||
r.SetBasicAuth("micromdm", token)
|
||||
return next(ctx, r, request)
|
||||
}
|
||||
}
|
||||
|
||||
func copyURL(base *url.URL, path string) *url.URL {
|
||||
next := *base
|
||||
next.Path = path
|
||||
return &next
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *ProfileService) GetProfiles(ctx context.Context, opt GetProfilesOption) ([]Profile, error) {
|
||||
@@ -27,7 +28,7 @@ type getProfilesResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r getProfilesResponse) error() error { return r.Err }
|
||||
func (r getProfilesResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeGetProfilesRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var opts GetProfilesOption
|
||||
@@ -41,11 +42,8 @@ func decodeGetProfilesRequest(ctx context.Context, r *http.Request) (interface{}
|
||||
}
|
||||
|
||||
func decodeGetProfilesResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp getProfilesResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ package profile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *ProfileService) RemoveProfiles(ctx context.Context, ids []string) error {
|
||||
@@ -26,22 +26,17 @@ type removeProfileResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r removeProfileResponse) error() error { return r.Err }
|
||||
func (r removeProfileResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeRemoveProfilesRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req removeProfileRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return req, nil
|
||||
err := httputil.DecodeJSONRequest(r, &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func decodeRemoveProfileResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp removeProfileResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package profile
|
||||
|
||||
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 {
|
||||
@@ -26,11 +24,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)
|
||||
|
||||
// GET /v1/profiles get a list of profiles managed by the server
|
||||
// PUT /v1/profiles create or replace a profile on the server
|
||||
@@ -39,39 +33,23 @@ func MakeHTTPHandler(e Endpoints, logger log.Logger) http.Handler {
|
||||
r.Methods("GET").Path("/v1/profiles").Handler(httptransport.NewServer(
|
||||
e.GetProfilesEndpoint,
|
||||
decodeGetProfilesRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
r.Methods("PUT").Path("/v1/profiles").Handler(httptransport.NewServer(
|
||||
e.ApplyProfileEndpoint,
|
||||
decodeApplyProfileRequest,
|
||||
httptransport.EncodeJSONResponse,
|
||||
httputil.EncodeJSONResponse,
|
||||
options...,
|
||||
))
|
||||
|
||||
r.Methods("DELETE").Path("/v1/profiles").Handler(httptransport.NewServer(
|
||||
e.RemoveProfilesEndpoint,
|
||||
decodeRemoveProfilesRequest,
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package remove
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ type blockDeviceResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r blockDeviceResponse) error() error { return r.Err }
|
||||
func (r blockDeviceResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeBlockDeviceRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var errBadRoute = errors.New("bad route")
|
||||
@@ -45,11 +45,8 @@ func encodeBlockDeviceRequest(_ context.Context, r *http.Request, request interf
|
||||
}
|
||||
|
||||
func decodeBlockDeviceResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp blockDeviceResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package remove
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransport.ClientOption) (Service, error) {
|
||||
@@ -20,8 +19,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
blockDeviceEndpoint = httptransport.NewClient(
|
||||
"POST",
|
||||
copyURL(u, ""), // empty path, modified by the encodeRequest func
|
||||
encodeRequestWithToken(token, encodeBlockDeviceRequest),
|
||||
httputil.CopyURL(u, ""), // empty path, modified by the encodeRequest func
|
||||
httputil.EncodeRequestWithToken(token, encodeBlockDeviceRequest),
|
||||
decodeBlockDeviceResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -31,8 +30,8 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
{
|
||||
unblockDeviceEndpoint = httptransport.NewClient(
|
||||
"POST",
|
||||
copyURL(u, ""), //modified by encodeRequestFunc
|
||||
encodeRequestWithToken(token, encodeUnblockDeviceRequest),
|
||||
httputil.CopyURL(u, ""), //modified by encodeRequestFunc
|
||||
httputil.EncodeRequestWithToken(token, encodeUnblockDeviceRequest),
|
||||
decodeUnblockDeviceResponse,
|
||||
opts...,
|
||||
).Endpoint()
|
||||
@@ -43,16 +42,3 @@ func NewHTTPClient(instance, token string, logger log.Logger, opts ...httptransp
|
||||
UnblockDeviceEndpoint: unblockDeviceEndpoint,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func encodeRequestWithToken(token string, next httptransport.EncodeRequestFunc) httptransport.EncodeRequestFunc {
|
||||
return func(ctx context.Context, r *http.Request, request interface{}) error {
|
||||
r.SetBasicAuth("micromdm", token)
|
||||
return next(ctx, r, request)
|
||||
}
|
||||
}
|
||||
|
||||
func copyURL(base *url.URL, path string) *url.URL {
|
||||
next := *base
|
||||
next.Path = path
|
||||
return &next
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package remove
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/micromdm/micromdm/pkg/httputil"
|
||||
)
|
||||
|
||||
func (svc *RemoveService) UnblockDevice(ctx context.Context, udid string) error {
|
||||
@@ -23,7 +23,7 @@ type unblockDeviceResponse struct {
|
||||
Err error `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (r unblockDeviceResponse) error() error { return r.Err }
|
||||
func (r unblockDeviceResponse) Failed() error { return r.Err }
|
||||
|
||||
func decodeUnblockDeviceRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var errBadRoute = errors.New("bad route")
|
||||
@@ -45,11 +45,8 @@ func encodeUnblockDeviceRequest(_ context.Context, r *http.Request, request inte
|
||||
}
|
||||
|
||||
func decodeUnblockDeviceResponse(_ context.Context, r *http.Response) (interface{}, error) {
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, errorDecoder(r)
|
||||
}
|
||||
var resp unblockDeviceResponse
|
||||
err := json.NewDecoder(r.Body).Decode(&resp)
|
||||
err := httputil.DecodeJSONResponse(r, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user