summaryrefslogtreecommitdiff
path: root/cmd/generate/httpcache.go
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-13 17:51:38 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-13 17:51:38 -0600
commit95ebab0baff6fc94ed6214b9603df9c552228b31 (patch)
tree8a14dfa1a7c5ebc83e5ca90c91668035f7b61e79 /cmd/generate/httpcache.go
parent816ace2436b57a709a569bfb03a17236db28f1bf (diff)
wip
Diffstat (limited to 'cmd/generate/httpcache.go')
-rw-r--r--cmd/generate/httpcache.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/cmd/generate/httpcache.go b/cmd/generate/httpcache.go
new file mode 100644
index 0000000..7debd0a
--- /dev/null
+++ b/cmd/generate/httpcache.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+)
+
+var httpCache = map[string]string{}
+
+func httpGet(u string) (string, error) {
+ resp, err := http.Get(u)
+ if err != nil {
+ return "", err
+ }
+ if resp.StatusCode != http.StatusOK {
+ return "", fmt.Errorf("unexpected HTTP status: %v", resp.Status)
+ }
+ bs, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return "", err
+ }
+ httpCache[u] = string(bs)
+ return httpCache[u], nil
+}
+
+func httpGetJSON(u string, out any) error {
+ str, err := httpGet(u)
+ if err != nil {
+ return err
+ }
+ return json.Unmarshal([]byte(str), out)
+}