forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_response.go
36 lines (29 loc) · 914 Bytes
/
client_response.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 porthos
import (
"encoding/json"
"errors"
)
// ClientResponse represents the response object of a RPC call.
type ClientResponse struct {
StatusCode int32
Headers Headers
Content []byte
ContentType string
}
// UnmarshalJSONTo outputs the response content to the argument pointer.
func (r *ClientResponse) UnmarshalJSONTo(v interface{}) error {
if r.ContentType != "application/json" {
return errors.New("The content type of the response is not 'application/json'")
}
err := json.Unmarshal(r.Content, v)
return err
}
// UnmarshalJSON outputs the response content to the argument pointer.
func (r *ClientResponse) UnmarshalJSON() (map[string]interface{}, error) {
if r.ContentType != "application/json" {
return nil, errors.New("The content type of the response is not 'application/json'")
}
var v map[string]interface{}
err := json.Unmarshal(r.Content, &v)
return v, err
}