diff options
Diffstat (limited to 'cmd/generate/httpcache.go')
-rw-r--r-- | cmd/generate/httpcache.go | 34 |
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) +} |