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
|
package links
import (
"bufio"
"fmt"
"io"
"net/http"
"time"
)
func fetch(rawURL string, timeoutSecs int) (io.Reader, error) {
client := http.Client{
Timeout: time.Duration(timeoutSecs) * time.Second,
}
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
if err != nil {
return nil, fmt.Errorf("can't create %s request for %s", http.MethodGet, rawURL)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("client failed to perform %s request for %s", http.MethodGet, rawURL)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status for %s for %s: %s", http.MethodGet, rawURL, resp.Status)
}
return bufio.NewReader(resp.Body), nil
}
|