-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpreq.go
42 lines (37 loc) · 949 Bytes
/
httpreq.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
package quotegetter
import (
"fmt"
"net/http"
"net/url"
"time"
)
// DefaultClient xxx
func DefaultClient(proxy string) (*http.Client, error) {
// tr := &http.Transport{}
tr := http.DefaultTransport.(*http.Transport).Clone()
if len(proxy) > 0 {
// Parse proxy URL string to a URL type
proxyURL, err := url.Parse(proxy)
if err != nil {
return nil, err
// panic(fmt.Sprintf("Error parsing proxy URL: %q. %v", proxy, err))
}
tr.Proxy = http.ProxyURL(proxyURL)
}
client := &http.Client{
Transport: tr,
Timeout: 10 * time.Second,
}
return client, nil
}
// DoHTTPRequest executes the http request.
func DoHTTPRequest(client *http.Client, req *http.Request) (*http.Response, error) {
if client == nil {
client, _ = DefaultClient("")
}
resp, err := client.Do(req)
if (err == nil) && (resp.StatusCode != http.StatusOK) {
err = fmt.Errorf("%s response status = %v", req.Method, resp.Status)
}
return resp, err
}