-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.go
36 lines (32 loc) · 930 Bytes
/
client.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
package marvin
import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/golang/protobuf/proto"
)
// EncodeProtoRequest is an httptransport.EncodeRequestFunc that serializes the request
// as Protobuf. If the request implements Headerer, the provided headers will be applied
// to the request.
func EncodeProtoRequest(_ context.Context, r *http.Request, preq interface{}) error {
r.Header.Set("Content-Type", "application/octet-stream")
if headerer, ok := preq.(httptransport.Headerer); ok {
for k := range headerer.Headers() {
r.Header.Set(k, headerer.Headers().Get(k))
}
}
req, ok := preq.(proto.Message)
if !ok {
return errors.New("response does not implement proto.Message")
}
b, err := proto.Marshal(req)
if err != nil {
return err
}
r.ContentLength = int64(len(b))
r.Body = ioutil.NopCloser(bytes.NewReader(b))
return nil
}