diff options
Diffstat (limited to 'internal/tradlit/tradlit.go')
| -rw-r--r-- | internal/tradlit/tradlit.go | 70 |
1 files changed, 64 insertions, 6 deletions
diff --git a/internal/tradlit/tradlit.go b/internal/tradlit/tradlit.go index edb9ce0..a7b53bc 100644 --- a/internal/tradlit/tradlit.go +++ b/internal/tradlit/tradlit.go @@ -9,6 +9,8 @@ import ( "fmt" "io" "net/http" + "os" + "path/filepath" "regexp" "strings" @@ -86,11 +88,57 @@ func Parse(jsonBody []byte) ([]liturgy.Section, error) { return out, nil } -// Load fetches a day's traditional propers from the missalemeum API -// (https://www.missalemeum.com/{lang}/api/v5/proper/{date}) and parses -// them. On HTTP 404 (no propers published for that date) it returns a -// clear error rather than attempting to parse. -func Load(date, lang string) ([]liturgy.Section, error) { +// cachePath returns where Load caches a (date, lang) day's raw API response: +// <CacheDir>/<date>.trad.<lang>.json -- date-prefixed (like the modern +// lectionary's own cache files) so liturgy.CleanCache can prune it by date. +func cachePath(date, lang string) string { + return filepath.Join(liturgy.CacheDir(), date+".trad."+lang+".json") +} + +// Load returns a day's traditional propers for lang, either read from the +// on-disk cache (offline) or fetched live from missalemeum and cached for +// next time (online). Both paths share Parse, so cached and live results +// are identical. +// +// - offline: reads the cache file written by a prior online Load (see +// cachePath); if it doesn't exist, returns a clear error telling the +// caller to go online or run 'lectio update' first. +// - online: fetches https://www.missalemeum.com/{lang}/api/v5/proper/{date} +// as before. On a successful 200, the raw response body is written to +// the cache path (best-effort -- a cache-write failure never fails the +// request) before being parsed. On HTTP 404 (no propers published for +// that date) it returns a clear error and writes nothing to the cache. +func Load(date, lang string, offline bool) ([]liturgy.Section, error) { + if offline { + return loadCached(date, lang) + } + return loadLive(date, lang) +} + +// loadCached implements Load's offline path. +func loadCached(date, lang string) ([]liturgy.Section, error) { + body, err := os.ReadFile(cachePath(date, lang)) + if err != nil { + return nil, fmt.Errorf("tradlit: no cached traditional propers for %s (%s); view it online or run 'lectio update' first", date, lang) + } + return Parse(body) +} + +// loadLive implements Load's online path: fetch, best-effort cache the raw +// body, then parse. +func loadLive(date, lang string) ([]liturgy.Section, error) { + body, err := fetch(date, lang) + if err != nil { + return nil, err + } + writeCache(date, lang, body) + return Parse(body) +} + +// fetch GETs the day's proper-of-the-day JSON from missalemeum and returns +// the raw response body. On HTTP 404 (no propers published for that date) +// it returns a clear error rather than the raw 404 body. +func fetch(date, lang string) ([]byte, error) { url := fmt.Sprintf(baseURL, lang, date) req, err := http.NewRequest(http.MethodGet, url, nil) @@ -116,6 +164,16 @@ func Load(date, lang string) ([]liturgy.Section, error) { if err != nil { return nil, fmt.Errorf("tradlit: load %s: %w", date, err) } + return body, nil +} - return Parse(body) +// writeCache best-effort writes a day's raw API response body to its cache +// path; a failure to cache (e.g. an unwritable cache dir) must never fail +// the live request that produced body. +func writeCache(date, lang string, body []byte) { + path := cachePath(date, lang) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return + } + _ = os.WriteFile(path, body, 0o644) } |
