From 916e778dfccc2aa5ce95056b97e11f134bc941f7 Mon Sep 17 00:00:00 2001 From: demo Date: Sun, 10 May 2026 21:56:24 -0400 Subject: refactor: move 'fetch' to its own package --- internal/fetch/doc.go | 3 +++ internal/fetch/fetch.go | 32 ++++++++++++++++++++++++++++++++ internal/fetch/fetch_test.go | 10 ++++++++++ internal/links/fetch.go | 32 -------------------------------- internal/links/fetch_test.go | 10 ---------- 5 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 internal/fetch/doc.go create mode 100644 internal/fetch/fetch.go create mode 100644 internal/fetch/fetch_test.go delete mode 100644 internal/links/fetch.go delete mode 100644 internal/links/fetch_test.go diff --git a/internal/fetch/doc.go b/internal/fetch/doc.go new file mode 100644 index 0000000..376a4d3 --- /dev/null +++ b/internal/fetch/doc.go @@ -0,0 +1,3 @@ +// Package fetch makes a GET request to a given URL, retrieving the +// HTML contents of that webpage. +package fetch 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 +} diff --git a/internal/fetch/fetch_test.go b/internal/fetch/fetch_test.go new file mode 100644 index 0000000..427406d --- /dev/null +++ b/internal/fetch/fetch_test.go @@ -0,0 +1,10 @@ +package fetch + +import "testing" + +func TestFetch(t *testing.T) { + _, err := fetch("http://example.com", 2) + if err != nil { + t.Error(err) + } +} diff --git a/internal/links/fetch.go b/internal/links/fetch.go deleted file mode 100644 index 53cc692..0000000 --- a/internal/links/fetch.go +++ /dev/null @@ -1,32 +0,0 @@ -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 -} diff --git a/internal/links/fetch_test.go b/internal/links/fetch_test.go deleted file mode 100644 index 5d873a6..0000000 --- a/internal/links/fetch_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package links - -import "testing" - -func TestFetch(t *testing.T) { - _, err := fetch("http://example.com", 2) - if err != nil { - t.Error(err) - } -} -- cgit v1.2.3