This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransport_http.go
63 lines (53 loc) · 1.62 KB
/
transport_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package checkin
import (
"io"
"net/http"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/groob/plist"
"golang.org/x/net/context"
)
type HTTPHandlers struct {
// The CheckinHandler should accept PUT requests
CheckinHandler http.Handler
}
func MakeHTTPHandlers(ctx context.Context, endpoints Endpoints, opts ...httptransport.ServerOption) HTTPHandlers {
h := HTTPHandlers{
CheckinHandler: httptransport.NewServer(
ctx,
endpoints.CheckinEndpoint,
decodeRequest,
encodeResponse,
opts...,
),
}
return h
}
type errorer interface {
error() error
}
type errorWrapper struct {
Error string `json:"error"`
}
func decodeRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var req checkinRequest
err := plist.NewDecoder(io.LimitReader(r.Body, 10000)).Decode(&req)
return req, err
}
// According to the MDM Check-in protocol, the server must respond with 200 OK
// to successful Check-in requests.
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
if e, ok := response.(errorer); ok && e.error() != nil {
EncodeError(ctx, e.error(), w)
return nil
}
w.WriteHeader(http.StatusOK)
return nil
}
// EncodeError is used by the HTTP transport to encode service errors in HTTP.
// The EncodeError should be passed to the Go-Kit httptransport as the
// ServerErrorEncoder to encode error responses.
// According to the MDM Check-in protocol specification, the device only needs
// a 401 (Unauthorized) response in case of failure.
func EncodeError(ctx context.Context, err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
}