summaryrefslogtreecommitdiff
path: root/internal/fetch
diff options
context:
space:
mode:
Diffstat (limited to 'internal/fetch')
-rw-r--r--internal/fetch/doc.go3
-rw-r--r--internal/fetch/fetch.go32
-rw-r--r--internal/fetch/fetch_test.go10
3 files changed, 45 insertions, 0 deletions
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)
+ }
+}