diff options
| author | demo <demo@antix1> | 2026-05-10 21:56:24 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-10 21:56:24 -0400 |
| commit | 916e778dfccc2aa5ce95056b97e11f134bc941f7 (patch) | |
| tree | e3e7036a4a727f3231d395cc74be917cac0f5b8c /internal/fetch/fetch.go | |
| parent | 79577b9024dea082c9cc8380997a224e3934df27 (diff) | |
refactor: move 'fetch' to its own package
Diffstat (limited to 'internal/fetch/fetch.go')
| -rw-r--r-- | internal/fetch/fetch.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/internal/fetch/fetch.go b/internal/fetch/fetch.go new file mode 100644 index 0000000..f508922 --- /dev/null +++ b/internal/fetch/fetch.go @@ -0,0 +1,32 @@ +package fetch + +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 +} |
