@@ -13,35 +13,36 @@ import (
13
13
14
14
// Client executes JSON RPC calls to remote servers.
15
15
type Client struct {
16
- URL string
17
-
18
- timeout int
19
- proxyURL string
16
+ serverURL string
20
17
httpClient * http.Client
21
18
}
22
19
23
20
// NewClient returns a newly istantiated Client pointing to the given url.
24
21
func NewClient (url string ) * Client {
25
- client := & Client {
26
- URL : url ,
27
- timeout : defaultTimeout ,
28
- proxyURL : "" ,
22
+ httpClient := & http.Client {
23
+ Timeout : time .Duration (time .Duration (defaultTimeout ) * time .Second ),
24
+ Transport : & http.Transport {
25
+ TLSClientConfig : & tls.Config {
26
+ InsecureSkipVerify : true ,
27
+ },
28
+ Proxy : http .ProxyFromEnvironment ,
29
+ },
29
30
}
30
- client .setHTTPClient ()
31
31
32
- return client
32
+ return & Client {
33
+ serverURL : url ,
34
+ httpClient : httpClient ,
35
+ }
33
36
}
34
37
35
38
// SetTimeout sets the client timeout to the given value.
36
39
func (c * Client ) SetTimeout (timeout int ) {
37
- c .timeout = timeout
38
- c .setHTTPClient ()
40
+ c .httpClient .Timeout = time .Duration (timeout ) * time .Second
39
41
}
40
42
41
- // SetHTTPProxy tells the client to use the given httpProxyURL as proxy address.
42
- func (c * Client ) SetHTTPProxy (httpProxyURL string ) {
43
- c .proxyURL = httpProxyURL
44
- c .setHTTPClient ()
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 )
45
46
}
46
47
47
48
// Run executes the given method having the given params setting the response
@@ -92,30 +93,10 @@ func (c *Client) Notify(method string, params interface{}) error {
92
93
return nil
93
94
}
94
95
95
- func (c * Client ) setHTTPClient () {
96
- transport := & http.Transport {
97
- TLSClientConfig : & tls.Config {
98
- InsecureSkipVerify : true ,
99
- },
100
- Proxy : http .ProxyFromEnvironment ,
101
- }
102
-
103
- if parsedProxyURL , err := url .Parse (c .proxyURL ); c .proxyURL != "" && err == nil {
104
- transport .Proxy = http .ProxyURL (parsedProxyURL )
105
- }
106
-
107
- newHTTPClient := & http.Client {
108
- Timeout : time .Duration (time .Duration (c .timeout ) * time .Second ),
109
- Transport : transport ,
110
- }
111
-
112
- c .httpClient = newHTTPClient
113
- }
114
-
115
96
func (c * Client ) sendJSONRequest (jsonRequest []byte ) ([]byte , error ) {
116
97
var jsonResponse []byte
117
98
118
- httpRequest , err := http .NewRequest ("POST" , c .URL , strings .NewReader (string (jsonRequest )))
99
+ httpRequest , err := http .NewRequest ("POST" , c .serverURL , strings .NewReader (string (jsonRequest )))
119
100
httpRequest .Header .Set ("Content-Type" , "application/json" )
120
101
httpRequest .Header .Set ("Content-Length" , "" )
121
102
httpRequest .Header .Set ("Accept" , "application/json" )
0 commit comments