@@ -27,38 +27,29 @@ func buildURL(baseStr string, endpoint string) *url.URL {
2727 return base .ResolveReference (u )
2828}
2929
30- // Request sends a HTTP request to the server.
31- func Request (base , endpoint , method , proxy , username , password string , hasBody bool , data interface {}) (interface {}, error ) {
32- transport := http .DefaultTransport
33-
34- if len (proxy ) > 0 {
35- log .Printf ("Using proxy '%s'" , proxy )
36-
37- proxyURL , err := url .Parse (proxy )
38- if err != nil {
39- return nil , err
40- }
41-
42- transport = & http.Transport {
43- Proxy : http .ProxyURL (proxyURL ),
44- }
30+ func createTransport (proxy string ) (http.RoundTripper , error ) {
31+ if len (proxy ) == 0 {
32+ return http .DefaultTransport , nil
4533 }
4634
47- client := & http.Client {
48- Transport : transport ,
35+ log .Printf ("Using proxy '%s'" , proxy )
36+ proxyURL , err := url .Parse (proxy )
37+ if err != nil {
38+ return nil , err
4939 }
5040
51- url := buildURL (base , endpoint )
41+ return & http.Transport {
42+ Proxy : http .ProxyURL (proxyURL ),
43+ }, nil
44+ }
5245
46+ func createRequest (url * url.URL , method , username , password string , hasBody bool , data interface {}) (* http.Request , error ) {
5347 var reqBodyReader io.Reader
54- reqBodyReader = nil
55-
5648 if hasBody {
5749 reqBody , err := json .Marshal (data )
5850 if err != nil {
5951 return nil , err
6052 }
61-
6253 reqBodyReader = strings .NewReader (string (reqBody ))
6354 }
6455
@@ -69,12 +60,14 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
6960
7061 req .Header .Set ("Accept" , "application/json" )
7162 req .Header .Set ("Content-Type" , "application/json" )
72-
7363 req .SetBasicAuth (username , password )
7464
75- resp , err := client .Do (req )
76- if err != nil {
77- log .Fatal (err )
65+ return req , nil
66+ }
67+
68+ func handleResponse (resp * http.Response ) (interface {}, error ) {
69+ if resp == nil {
70+ return nil , fmt .Errorf ("received nil response" )
7871 }
7972 defer handling .Close (resp .Body )
8073
@@ -88,7 +81,6 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
8881 }
8982
9083 var obj interface {}
91-
9284 err = json .Unmarshal (bodyText , & obj )
9385 if err != nil {
9486 return nil , err
@@ -97,6 +89,29 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
9789 return obj , nil
9890}
9991
92+ // Request sends a HTTP request to the server.
93+ func Request (base , endpoint , method , proxy , username , password string , hasBody bool , data interface {}) (interface {}, error ) {
94+ transport , err := createTransport (proxy )
95+ if err != nil {
96+ return nil , err
97+ }
98+
99+ client := & http.Client {Transport : transport }
100+ url := buildURL (base , endpoint )
101+
102+ req , err := createRequest (url , method , username , password , hasBody , data )
103+ if err != nil {
104+ return nil , err
105+ }
106+
107+ resp , err := client .Do (req )
108+ if err != nil {
109+ return nil , err
110+ }
111+
112+ return handleResponse (resp )
113+ }
114+
100115// Delete sends a HTTP DELETE request to the server.
101116func Delete (base , endpoint , proxy , username , password string ) (interface {}, error ) {
102117 return Request (base , endpoint , "DELETE" , proxy , username , password , false , nil )
0 commit comments