-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
86 lines (71 loc) · 2.14 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package jsonrpc
import (
"encoding/json"
"io"
"net/http"
"github.com/smallsung/gopkg/rpc"
)
type clientCodec struct {
rwc io.ReadWriteCloser
decoder *json.Decoder
}
func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec {
return &clientCodec{rwc: conn, decoder: json.NewDecoder(conn)}
}
func (codec *clientCodec) WriteRequest(rawMessage rpc.RawMessage) error {
_, err := codec.rwc.Write(rawMessage)
return err
}
func (codec *clientCodec) MarshalRequest(requests *rpc.RequestMessages) (rpc.RawMessage, error) {
s := make([]*RequestMessage, 0, len(requests.Elems))
for _, m := range requests.Elems {
s = append(s, toRequestMessage(m))
}
if requests.Batch {
return json.Marshal(s)
} else {
return json.Marshal(s[0])
}
}
func (codec *clientCodec) MarshalRequestParams(i ...interface{}) (rpc.MessageParams, error) {
return json.Marshal(i)
}
func (codec *clientCodec) UnmarshalResponse(rawMessage rpc.RawMessage) (*rpc.ResponseMessages, error) {
if responses, err := parseResponseRawMessage(rawMessage); err != nil {
return nil, err
} else {
return responses, nil
}
}
func (codec *clientCodec) UnmarshalResponseResult(rawMessage rpc.MessageResult, i interface{}) error {
return json.Unmarshal(rawMessage, i)
}
func (codec *clientCodec) ReadResponse() (rpc.RawMessage, error) {
var rawMessage json.RawMessage
if err := codec.decoder.Decode(&rawMessage); err != nil {
return nil, err
}
return rawMessage, nil
}
func (codec *clientCodec) Close() error {
return codec.rwc.Close()
}
type httpRoundTripperFunc func(request *http.Request) (*http.Response, error)
func (f httpRoundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error) {
return f(request)
}
var HttpRoundTripper = struct {
ValidHeader http.RoundTripper
}{
ValidHeader: httpRoundTripperFunc(validateResponseHeader),
}
func validateResponseHeader(request *http.Request) (*http.Response, error) {
request.Header.Set("accept", contentType)
request.Header.Set("content-type", contentType)
if response, err := http.DefaultTransport.RoundTrip(request); err != nil {
return response, err
} else {
//todo 判断响应头context-type
return response, err
}
}