-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
69 lines (60 loc) · 1.81 KB
/
response.go
File metadata and controls
69 lines (60 loc) · 1.81 KB
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
64
65
66
67
68
69
package ccloud
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
)
// JsonDecoder decodes http response JSON into a JSON-tagged struct value.
//
// This is copied from sling because they didn't export it and we need to "reset"
// the decoder to use basic json encoding sometimes (from a base sling instance)
type JsonDecoder struct {
}
// Decode decodes the Response Body into the value pointed to by v.
// Caller must provide a non-nil v and close the resp.Body.
func (d JsonDecoder) Decode(resp *http.Response, v interface{}) error {
return json.NewDecoder(resp.Body).Decode(v)
}
// JSONPBDecoder deserializes response bodies as protobuf JSON messages by implementing sling.Decoder.
type JSONPBDecoder struct {
jsonpb.Unmarshaler
}
// NewJSONPBDecoder returns a new JSONPBDecoder that's lenient for error responses.
func NewJSONPBDecoder() JSONPBDecoder {
return JSONPBDecoder{
jsonpb.Unmarshaler{
// This is required to handle malformed tokens on regular (non-auth) requests
AllowUnknownFields: true,
},
}
}
// Decode reads the next value from the reader and stores it in the value pointed to by v.
func (d JSONPBDecoder) Decode(resp *http.Response, v interface{}) error {
if msg, ok := v.(proto.Message); ok {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
buf := bytes.NewBuffer(body)
err = d.Unmarshal(buf, msg)
if err != nil {
err = fmt.Errorf("%s\n%s", err.Error(), string(body))
}
return err
} else {
fmt.Sprintln("non-protobuf interface v given. Decoding with json instead of jsonpb.")
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &v)
if err != nil {
err = fmt.Errorf("%s\n%s", err, string(body))
}
return err
}
}