49 lines
736 B
Go
49 lines
736 B
Go
package fetcher
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Fetcher struct {
|
|
client *http.Client
|
|
}
|
|
|
|
func New() *Fetcher {
|
|
return &Fetcher{
|
|
client: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (f *Fetcher) FetchContent(url string) (string, error) {
|
|
resp, err := f.client.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(body), nil
|
|
}
|
|
|
|
func (f *Fetcher) FetchAllURLs(urls []string) (map[string]string, error) {
|
|
results := make(map[string]string)
|
|
|
|
for _, url := range urls {
|
|
content, err := f.FetchContent(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
results[url] = content
|
|
}
|
|
|
|
return results, nil
|
|
}
|