mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-10 19:46:07 +08:00
add common http functions to httputil pkg (#350)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user