@@ -6,70 +6,56 @@ import (
6
6
"errors"
7
7
"io/ioutil"
8
8
"net/http"
9
+ "net/url"
9
10
"strings"
10
11
"time"
11
12
)
12
13
14
+ // Client executes JSON RPC calls to remote servers.
13
15
type Client struct {
14
- Url string
15
- Timeout int
16
+ serverURL string
17
+ httpClient * http. Client
16
18
}
17
19
20
+ // NewClient returns a newly istantiated Client pointing to the given url.
18
21
func NewClient (url string ) * Client {
19
- client := new (Client )
20
-
21
- client .Url = url
22
- client .Timeout = DEFAULT_TIMEOUT
23
-
24
- return client
25
- }
26
-
27
- func (this * Client ) sendJsonRequest (jsonRequest []byte ) ([]byte , error ) {
28
- var jsonResponse []byte
29
-
30
22
httpClient := & http.Client {
31
- Timeout : time .Duration (time .Duration (this . Timeout ) * time .Second ),
23
+ Timeout : time .Duration (time .Duration (defaultTimeout ) * time .Second ),
32
24
Transport : & http.Transport {
33
25
TLSClientConfig : & tls.Config {
34
26
InsecureSkipVerify : true ,
35
27
},
28
+ Proxy : http .ProxyFromEnvironment ,
36
29
},
37
30
}
38
31
39
- httpRequest , err := http .NewRequest ("POST" , this .Url , strings .NewReader (string (jsonRequest )))
40
- httpRequest .Header .Set ("Content-Type" , "application/json" )
41
- httpRequest .Header .Set ("Content-Length" , "" )
42
- httpRequest .Header .Set ("Accept" , "application/json" )
43
- httpRequest .Header .Set ("Connection" , "close" )
44
-
45
- httpResponse , err := httpClient .Do (httpRequest )
46
- if err != nil {
47
- return jsonResponse , err
48
- }
49
-
50
- defer httpResponse .Body .Close ()
51
-
52
- jsonResponse , err = ioutil .ReadAll (httpResponse .Body )
53
- if err != nil {
54
- return jsonResponse , err
32
+ return & Client {
33
+ serverURL : url ,
34
+ httpClient : httpClient ,
55
35
}
36
+ }
56
37
57
- return jsonResponse , nil
38
+ // SetTimeout sets the client timeout to the given value.
39
+ func (c * Client ) SetTimeout (timeout int ) {
40
+ c .httpClient .Timeout = time .Duration (timeout ) * time .Second
58
41
}
59
42
60
- func (this * Client ) SetTimeout (timeout int ) {
61
- this .Timeout = timeout
43
+ // SetHTTPProxyURL tells the client to use the given proxyURL as proxy address.
44
+ func (c * Client ) SetHTTPProxyURL (proxyURL * url.URL ) {
45
+ c .httpClient .Transport .(* http.Transport ).Proxy = http .ProxyURL (proxyURL )
62
46
}
63
47
64
- func (this * Client ) Run (method string , params interface {}, result interface {}) error {
48
+ // Run executes the given method having the given params setting the response
49
+ // value in the given result interface.
50
+ func (c * Client ) Run (method string , params interface {}, result interface {}) error {
65
51
request := NewRequest (method , params , RandInt (10000000 , 99999999 ))
66
52
67
53
jsonRequest , err := json .Marshal (request )
68
54
if err != nil {
69
55
return err
70
56
}
71
57
72
- jsonResponse , err := this . sendJsonRequest (jsonRequest )
58
+ jsonResponse , err := c . sendJSONRequest (jsonRequest )
73
59
if err != nil {
74
60
return err
75
61
}
@@ -89,18 +75,44 @@ func (this *Client) Run(method string, params interface{}, result interface{}) e
89
75
return nil
90
76
}
91
77
92
- func (this * Client ) Notify (method string , params interface {}) error {
78
+ // Notify executes the given method with the given parameters.
79
+ // Doesn't expect any result.
80
+ func (c * Client ) Notify (method string , params interface {}) error {
93
81
request := NewRequest (method , params , 0 )
94
82
95
- requestJson , err := json .Marshal (request )
83
+ jsonRequest , err := json .Marshal (request )
96
84
if err != nil {
97
85
return err
98
86
}
99
87
100
- _ , err = this . sendJsonRequest ( requestJson )
88
+ _ , err = c . sendJSONRequest ( jsonRequest )
101
89
if err != nil {
102
90
return err
103
91
}
104
92
105
93
return nil
106
94
}
95
+
96
+ func (c * Client ) sendJSONRequest (jsonRequest []byte ) ([]byte , error ) {
97
+ var jsonResponse []byte
98
+
99
+ httpRequest , err := http .NewRequest ("POST" , c .serverURL , strings .NewReader (string (jsonRequest )))
100
+ httpRequest .Header .Set ("Content-Type" , "application/json" )
101
+ httpRequest .Header .Set ("Content-Length" , "" )
102
+ httpRequest .Header .Set ("Accept" , "application/json" )
103
+ httpRequest .Header .Set ("Connection" , "close" )
104
+
105
+ httpResponse , err := c .httpClient .Do (httpRequest )
106
+ if err != nil {
107
+ return jsonResponse , err
108
+ }
109
+
110
+ defer httpResponse .Body .Close ()
111
+
112
+ jsonResponse , err = ioutil .ReadAll (httpResponse .Body )
113
+ if err != nil {
114
+ return jsonResponse , err
115
+ }
116
+
117
+ return jsonResponse , nil
118
+ }
0 commit comments