summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-05-20 16:04:36 -0400
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-05-21 00:04:19 -0400
commite4e4420c690c6888fd00997203d4c610466c2eb6 (patch)
treec2f5dfe4ea1207e7ac2f457b0bf5aaebb25c098a
parent731bde4f68014caeca82d72e43c04a7b755148f8 (diff)
mv cmd/generate/httpcache.go lib/httpcache/ # and supporting changes
-rw-r--r--cmd/generate/forge_forgejo.go14
-rw-r--r--cmd/generate/forge_gerrit.go6
-rw-r--r--cmd/generate/forge_github.go14
-rw-r--r--cmd/generate/forge_gitlab.go10
-rw-r--r--cmd/generate/forge_part_git.go4
-rw-r--r--cmd/generate/forge_part_pipermail.go7
-rw-r--r--cmd/generate/src_mastodon.go6
-rw-r--r--lib/httpcache/httpcache.go (renamed from cmd/generate/httpcache.go)12
8 files changed, 43 insertions, 30 deletions
diff --git a/cmd/generate/forge_forgejo.go b/cmd/generate/forge_forgejo.go
index 84988f2..34ec767 100644
--- a/cmd/generate/forge_forgejo.go
+++ b/cmd/generate/forge_forgejo.go
@@ -4,6 +4,8 @@ import (
"fmt"
"regexp"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
var reForgejoPR = regexp.MustCompile(`^https://([^/]+)/([^/?#]+)/([^/?#]+)/pulls/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
@@ -33,7 +35,7 @@ func (f Forgejo) FetchStatus(urls []string) (string, error) {
Merged bool `json:"merged"`
MergeCommitSha string `json:"merge_commit_sha"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return "", err
}
ret := obj.State
@@ -68,7 +70,7 @@ func (f Forgejo) FetchSubmittedAt(urls []string) (time.Time, error) {
var obj struct {
CreatedAt time.Time `json:"created_at"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, err
}
return obj.CreatedAt, nil
@@ -103,7 +105,7 @@ func (f Forgejo) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"merged_by"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, User{}, err
}
@@ -127,7 +129,7 @@ func (f Forgejo) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"user"`
}
- if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", nil, &comments, githubPagination); err != nil {
+ if err := httpcache.GetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", nil, &comments, githubPagination); err != nil {
return time.Time{}, User{}, err
}
for _, comment := range comments {
@@ -147,7 +149,7 @@ func (f Forgejo) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"user"`
}
- if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", nil, &reviewComments, githubPagination); err != nil {
+ if err := httpcache.GetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", nil, &reviewComments, githubPagination); err != nil {
return time.Time{}, User{}, err
}
for _, comment := range reviewComments {
@@ -166,7 +168,7 @@ func (f Forgejo) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"actor"`
}
- if err := httpGetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", nil, &events); err != nil {
+ if err := httpcache.GetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", nil, &events); err != nil {
return time.Time{}, User{}, err
}
for _, event := range events {
diff --git a/cmd/generate/forge_gerrit.go b/cmd/generate/forge_gerrit.go
index 31f2256..05f0386 100644
--- a/cmd/generate/forge_gerrit.go
+++ b/cmd/generate/forge_gerrit.go
@@ -8,12 +8,14 @@ import (
"regexp"
"strings"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
-// httpGetGerritJSON is like [httpGetJSON], but
+// httpGetGerritJSON is like [httpcache.GetJSON], but
// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
func httpGetGerritJSON(u string, hdr map[string]string, out any) error {
- str, err := httpGet(u, hdr)
+ str, err := httpcache.Get(u, hdr)
if err != nil {
return err
}
diff --git a/cmd/generate/forge_github.go b/cmd/generate/forge_github.go
index 5092e97..b657ad7 100644
--- a/cmd/generate/forge_github.go
+++ b/cmd/generate/forge_github.go
@@ -5,6 +5,8 @@ import (
"net/url"
"regexp"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
var reGitHubPR = regexp.MustCompile(`^https://github\.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
@@ -42,7 +44,7 @@ func (GitHub) FetchStatus(urls []string) (string, error) {
Merged bool `json:"merged"`
MergeCommitSha string `json:"merge_commit_sha"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return "", err
}
ret := obj.State
@@ -87,7 +89,7 @@ func (GitHub) FetchSubmittedAt(urls []string) (time.Time, error) {
var obj struct {
CreatedAt time.Time `json:"created_at"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, err
}
return obj.CreatedAt, nil
@@ -126,7 +128,7 @@ func (GitHub) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"merged_by"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, User{}, err
}
@@ -150,7 +152,7 @@ func (GitHub) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"user"`
}
- if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", nil, &comments, githubPagination); err != nil {
+ if err := httpcache.GetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", nil, &comments, githubPagination); err != nil {
return time.Time{}, User{}, err
}
for _, comment := range comments {
@@ -170,7 +172,7 @@ func (GitHub) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"user"`
}
- if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", nil, &reviewComments, githubPagination); err != nil {
+ if err := httpcache.GetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", nil, &reviewComments, githubPagination); err != nil {
return time.Time{}, User{}, err
}
for _, comment := range reviewComments {
@@ -189,7 +191,7 @@ func (GitHub) FetchLastUpdated(urls []string) (time.Time, User, error) {
HTMLURL string `json:"html_url"`
} `json:"actor"`
}
- if err := httpGetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", nil, &events); err != nil {
+ if err := httpcache.GetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", nil, &events); err != nil {
return time.Time{}, User{}, err
}
for _, event := range events {
diff --git a/cmd/generate/forge_gitlab.go b/cmd/generate/forge_gitlab.go
index a1ea7c0..84a2285 100644
--- a/cmd/generate/forge_gitlab.go
+++ b/cmd/generate/forge_gitlab.go
@@ -5,6 +5,8 @@ import (
"net/url"
"regexp"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
var reGitLabMR = regexp.MustCompile(`^https://([^/]+)/([^?#]+)/-/merge_requests/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
@@ -31,7 +33,7 @@ func (GitLab) FetchStatus(urls []string) (string, error) {
MergeCommitSha string `json:"merge_commit_sha"`
SquashCommitSha string `json:"squash_commit_sha"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return "", err
}
@@ -80,7 +82,7 @@ func (GitLab) FetchSubmittedAt(urls []string) (time.Time, error) {
var obj struct {
CreatedAt time.Time `json:"created_at"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, err
}
return obj.CreatedAt, nil
@@ -117,7 +119,7 @@ func (GitLab) FetchLastUpdated(urls []string) (time.Time, User, error) {
WebURL string `json:"web_url"`
} `json:"merged_by"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, User{}, err
}
@@ -148,7 +150,7 @@ func (GitLab) FetchLastUpdated(urls []string) (time.Time, User, error) {
} `json:"resolved_by"`
} `json:"notes"`
}
- if err := httpGetJSON(fmt.Sprintf("https://%s/%s/noteable/merge_request/%d/notes", authority, projectID, obj.ID), map[string]string{"X-Last-Fetched-At": "0"}, &notes); err != nil {
+ if err := httpcache.GetJSON(fmt.Sprintf("https://%s/%s/noteable/merge_request/%d/notes", authority, projectID, obj.ID), map[string]string{"X-Last-Fetched-At": "0"}, &notes); err != nil {
return time.Time{}, User{}, err
}
for _, note := range notes.Notes {
diff --git a/cmd/generate/forge_part_git.go b/cmd/generate/forge_part_git.go
index 5288286..5175750 100644
--- a/cmd/generate/forge_part_git.go
+++ b/cmd/generate/forge_part_git.go
@@ -4,6 +4,8 @@ import (
"fmt"
"regexp"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
var reGitHubCommit = regexp.MustCompile(`^https://github\.com/([^/?#]+)/([^/?#]+)/commit/([0-9a-f]+)(?:\?[^#]*)?(?:#.*)?$`)
@@ -62,7 +64,7 @@ func (PartGit) FetchLastUpdated(urls []string) (time.Time, User, error) {
} `json:"committer"`
} `json:"commit"`
}
- if err := httpGetJSON(urlStr, nil, &obj); err != nil {
+ if err := httpcache.GetJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, User{}, err
}
if obj.Commit.Author.Date.After(ret) {
diff --git a/cmd/generate/forge_part_pipermail.go b/cmd/generate/forge_part_pipermail.go
index e7c9e4c..9db498b 100644
--- a/cmd/generate/forge_part_pipermail.go
+++ b/cmd/generate/forge_part_pipermail.go
@@ -12,6 +12,7 @@ import (
"strings"
"time"
+ "git.lukeshu.com/www/lib/httpcache"
"git.lukeshu.com/www/lib/mailstuff"
)
@@ -34,7 +35,7 @@ func (PartPiperMail) FetchSubmittedAt(urls []string) (time.Time, error) {
if !rePiperMailMessage.MatchString(u) {
continue
}
- htmlStr, err := httpGet(u, nil)
+ htmlStr, err := httpcache.Get(u, nil)
if err != nil {
return time.Time{}, err
}
@@ -108,7 +109,7 @@ func (p PartPiperMail) FetchLastUpdated(urls []string) (time.Time, User, error)
uYM := m[2]
//uInt := m[3]
- htmlStr, err := httpGet(u, nil)
+ htmlStr, err := httpcache.Get(u, nil)
if err != nil {
return time.Time{}, User{}, fmt.Errorf("could not fetch message: %w", err)
}
@@ -132,7 +133,7 @@ func (p PartPiperMail) FetchLastUpdated(urls []string) (time.Time, User, error)
for ym, mbox := uYM, []*mail.Message(nil); true; ym = p.nextMonth(ym) {
lenBefore := p.threadLen(thread)
- mboxGzStr, err := httpGet(uBase+ym+".txt.gz", nil)
+ mboxGzStr, err := httpcache.Get(uBase+ym+".txt.gz", nil)
if err != nil {
if ym == uYM || !errors.Is(err, os.ErrNotExist) {
return time.Time{}, User{}, fmt.Errorf("could not fetch mbox for %s: %w", ym, err)
diff --git a/cmd/generate/src_mastodon.go b/cmd/generate/src_mastodon.go
index da9bea2..a3b9617 100644
--- a/cmd/generate/src_mastodon.go
+++ b/cmd/generate/src_mastodon.go
@@ -4,6 +4,8 @@ import (
"html/template"
"net/url"
"time"
+
+ "git.lukeshu.com/www/lib/httpcache"
)
type MastodonStatus struct {
@@ -18,12 +20,12 @@ func ReadStandups(server, username string) ([]*MastodonStatus, error) {
var account struct {
ID string `json:"id"`
}
- if err := httpGetJSON(server+"/api/v1/accounts/lookup?acct="+username, nil, &account); err != nil {
+ if err := httpcache.GetJSON(server+"/api/v1/accounts/lookup?acct="+username, nil, &account); err != nil {
return nil, err
}
var statuses []*MastodonStatus
- if err := httpGetPaginatedJSON(server+"/api/v1/accounts/"+account.ID+"/statuses", nil, &statuses, func(_ int) url.Values {
+ if err := httpcache.GetPaginatedJSON(server+"/api/v1/accounts/"+account.ID+"/statuses", nil, &statuses, func(_ int) url.Values {
params := make(url.Values)
params.Set("tagged", "DailyStandUp")
params.Set("exclude_reblogs", "true")
diff --git a/cmd/generate/httpcache.go b/lib/httpcache/httpcache.go
index 6d663b7..193a892 100644
--- a/cmd/generate/httpcache.go
+++ b/lib/httpcache/httpcache.go
@@ -1,4 +1,4 @@
-package main
+package httpcache
import (
"encoding/json"
@@ -38,7 +38,7 @@ func (e *httpStatusError) Error() string {
return fmt.Sprintf("unexpected HTTP status: %v", e.Status)
}
-func httpGet(u string, hdr map[string]string) (string, error) {
+func Get(u string, hdr map[string]string) (string, error) {
cacheKey := url.QueryEscape(u)
hdrKeys := make([]string, 0, len(hdr))
for k := range hdr {
@@ -102,15 +102,15 @@ func httpGet(u string, hdr map[string]string) (string, error) {
return httpCache[cacheKey].Body, nil
}
-func httpGetJSON(u string, hdr map[string]string, out any) error {
- str, err := httpGet(u, hdr)
+func GetJSON(u string, hdr map[string]string, out any) error {
+ str, err := Get(u, hdr)
if err != nil {
return err
}
return json.Unmarshal([]byte(str), out)
}
-func httpGetPaginatedJSON[T any](uStr string, hdr map[string]string, out *[]T, pageFn func(i int) url.Values) error {
+func GetPaginatedJSON[T any](uStr string, hdr map[string]string, out *[]T, pageFn func(i int) url.Values) error {
u, err := url.Parse(uStr)
if err != nil {
return err
@@ -125,7 +125,7 @@ func httpGetPaginatedJSON[T any](uStr string, hdr map[string]string, out *[]T, p
u.RawQuery = query.Encode()
var resp []T
- if err := httpGetJSON(u.String(), hdr, &resp); err != nil {
+ if err := GetJSON(u.String(), hdr, &resp); err != nil {
return err
}
fmt.Printf(" -> %d records\n", len(resp))