-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapi_17.go
42 lines (37 loc) · 955 Bytes
/
api_17.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
// +build go1.7
package telegram
import (
"net/http"
"net/url"
"golang.org/x/net/context"
)
// these errors are from net/http for go 1.7
const (
errRequestCanceled = "net/http: request canceled"
errRequestCanceledConn = "net/http: request canceled while waiting for connection"
)
func makeRequest(ctx context.Context, client HTTPDoer, req *http.Request) (*http.Response, error) {
var (
resp *http.Response
err error
)
if httpClient, ok := client.(*http.Client); ok {
resp, err = httpClient.Do(req.WithContext(ctx))
} else {
// TODO: implement cancel logic for non http.Client
resp, err = client.Do(req)
}
if err != nil {
if urlErr, casted := err.(*url.Error); casted {
if urlErr.Err == context.Canceled {
return resp, context.Canceled
}
errMsg := urlErr.Err.Error()
if errMsg == errRequestCanceled ||
errMsg == errRequestCanceledConn {
return resp, context.Canceled
}
}
}
return resp, err
}