summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/client.go30
-rw-r--r--client/client_test.go22
2 files changed, 52 insertions, 0 deletions
diff --git a/client/client.go b/client/client.go
new file mode 100644
index 0000000..e5678c7
--- /dev/null
+++ b/client/client.go
@@ -0,0 +1,30 @@
+// Copyright © 2022 siddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+// Thin HTTP client wrapper.
+package client
+
+import (
+ "net/http"
+
+ "ricketyspace.net/peach/version"
+)
+
+// HTTP client.
+var client = http.Client{}
+
+// Make a HTTP GET request.
+func Get(url string) (*http.Response, error) {
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, err
+ }
+ return client.Do(buildHeaders(req))
+}
+
+// Add default headers for the peach http client.
+func buildHeaders(req *http.Request) *http.Request {
+ req.Header.Set("User-Agent", "peach/"+version.Version+
+ " ricketyspace.net/peach")
+ return req
+}
diff --git a/client/client_test.go b/client/client_test.go
new file mode 100644
index 0000000..56a79c0
--- /dev/null
+++ b/client/client_test.go
@@ -0,0 +1,22 @@
+// Copyright © 2022 siddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package client
+
+import (
+ "io"
+ "testing"
+)
+
+func TestGet(t *testing.T) {
+ res, err := Get("https://plan.cat/~s")
+ if err != nil {
+ t.Errorf("get failed: %v", err)
+ return
+ }
+ _, err = io.ReadAll(res.Body)
+ if err != nil {
+ t.Errorf("response read failed: %v", err)
+ return
+ }
+}