aboutsummaryrefslogtreecommitdiff
path: root/miniflux/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'miniflux/client.go')
-rw-r--r--miniflux/client.go100
1 files changed, 86 insertions, 14 deletions
diff --git a/miniflux/client.go b/miniflux/client.go
index d430852..29441c4 100644
--- a/miniflux/client.go
+++ b/miniflux/client.go
@@ -2,12 +2,17 @@
//
// Typed over exactly what fluxrec needs: GET /v1/entries with the filter
// subset used by export (starred/status/changed_after) and serve
-// (after_entry_id). Auth is the X-Auth-Token header; credentials come from
-// the environment (MINIFLUX_* with MFLUX_* fallback, so existing mflux
-// exports work). Read-only by construction: there are no mutation methods.
+// (after_entry_id), GET /v1/feeds (audits, the politeness sweep), and
+// exactly one write: MakeFeedPolite, whose body type carries exactly two
+// fields so the request can only ever say {"crawler": false,
+// "ignore_http_cache": false} and nothing else. Auth is the X-Auth-Token
+// header; credentials come from the environment (MINIFLUX_* with MFLUX_*
+// fallback, so existing mflux exports work). Everything else is read-only
+// by construction: no entry, star, or other feed mutation exists.
package miniflux
import (
+ "bytes"
"encoding/json"
"fmt"
"io"
@@ -54,26 +59,36 @@ func firstEnv(names [2]string) string {
return ""
}
-// Client is a read-only Miniflux API client.
+// Client talks to Miniflux: reads plus the single politeness write.
+// Transport-level blips (DNS read timeouts, resets) are retried with the
+// waits in retryWait.
type Client struct {
baseURL string
token string
httpClient *http.Client
+ retryWait []time.Duration
}
+// transportBackoff is the default retry spacing after a transport-level
+// request failure (dial/read timeout, reset — live-observed as DNS read
+// timeouts on a flaky resolver). Short enough to clear a flap without ever
+// looking like extra load to the server.
+var transportBackoff = []time.Duration{time.Second, 4 * time.Second}
+
// NewClient builds a Client with a sane default HTTP timeout.
func NewClient(cfg Config) *Client {
return &Client{
baseURL: cfg.BaseURL,
token: cfg.Token,
httpClient: &http.Client{Timeout: 60 * time.Second},
+ retryWait: transportBackoff,
}
}
// NewClientWithHTTP is NewClient with a caller-provided http.Client
// (tests point it at httptest servers).
func NewClientWithHTTP(cfg Config, hc *http.Client) *Client {
- return &Client{baseURL: cfg.BaseURL, token: cfg.Token, httpClient: hc}
+ return &Client{baseURL: cfg.BaseURL, token: cfg.Token, httpClient: hc, retryWait: transportBackoff}
}
// Feed is the subset of a Miniflux feed fluxrec needs.
@@ -164,36 +179,93 @@ func (c *Client) Feeds() ([]FeedInfo, error) {
return feeds, nil
}
+// MakeFeedPolite sets the two per-feed fetch settings that keep polling
+// light — crawler (fetch-original-content) and ignore_http_cache — both
+// false, so feeds stop being scraped and conditional requests (ETag /
+// If-Modified-Since) are honored again. This is the sole write fluxrec
+// performs on Miniflux. The request body is a marshalled feedPatch, which
+// carries exactly these two fields, so nothing else on the feed can move
+// (Miniflux's update is pointer-based: omitted fields stay untouched).
+// Idempotent: same input, same state.
+func (c *Client) MakeFeedPolite(id int64) error {
+ payload, err := json.Marshal(feedPatch{})
+ if err != nil {
+ return fmt.Errorf("encoding update request: %w", err)
+ }
+ _, err = c.sendJSON(http.MethodPut, c.baseURL+"/v1/feeds/"+strconv.FormatInt(id, 10), payload)
+ return err
+}
+
+// feedPatch is the PUT /v1/feeds/{id} request body. Exactly these two
+// fields, both false: the wire format is
+// {"crawler":false,"ignore_http_cache":false}.
+type feedPatch struct {
+ Crawler bool `json:"crawler"` // "Fetch original content"
+ IgnoreHTTPCache bool `json:"ignore_http_cache"` // "Ignore HTTP cache"
+}
+
// fetchEntries issues the GET and returns the body on HTTP 200.
func (c *Client) fetchEntries(f Filters) ([]byte, error) {
return c.getJSON(entriesURL(c.baseURL, f))
}
-// getJSON GETs u with the auth header and returns the body on HTTP 200;
-// any other status becomes an error naming the status and a response sniff.
+// getJSON GETs u with the auth header and returns the body via sendJSON.
func (c *Client) getJSON(u string) ([]byte, error) {
- req, err := http.NewRequest(http.MethodGet, u, nil)
+ return c.sendJSON(http.MethodGet, u, nil)
+}
+
+// sendJSON issues an authenticated request to u — payload nil means no
+// request body (GET), non-nil means a JSON body. Returns the response body
+// on any 2xx status: Miniflux answers reads with 200 and mutations with
+// 200, 201 or 204 (201 even for updates — live-observed; the 204 body is
+// empty). Any other status becomes an error naming the method, URL, status
+// and a response sniff.
+//
+// Transport-level failures are retried (up to len(retryWait) extra
+// attempts) but status errors are not: a blip is transient, a server
+// answer is an answer. Retrying is safe for every call this client makes
+// — GETs are idempotent, and the one write (MakeFeedPolite) sets a fixed
+// state. Each attempt re-arms the body from GetBody so a drained reader
+// can never be re-sent empty.
+func (c *Client) sendJSON(method, u string, payload []byte) ([]byte, error) {
+ var body io.Reader
+ if payload != nil {
+ body = bytes.NewReader(payload)
+ }
+ req, err := http.NewRequest(method, u, body)
if err != nil {
return nil, fmt.Errorf("building request: %w", err)
}
req.Header.Set("X-Auth-Token", c.token)
+ if payload != nil {
+ req.Header.Set("Content-Type", "application/json")
+ }
resp, err := c.httpClient.Do(req)
- if err != nil {
- return nil, fmt.Errorf("GET %s: %w", u, err)
+ for attempt := 0; err != nil; attempt++ {
+ if attempt >= len(c.retryWait) {
+ return nil, fmt.Errorf("%s %s: %w", method, u, err)
+ }
+ time.Sleep(c.retryWait[attempt])
+ if req.GetBody != nil {
+ if fresh, berr := req.GetBody(); berr == nil {
+ req.Body = fresh
+ }
+ }
+ resp, err = c.httpClient.Do(req)
}
defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode != http.StatusOK {
+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
- return nil, fmt.Errorf("GET %s: status %d: %s", u, resp.StatusCode, body)
+ return nil, fmt.Errorf("%s %s: status %d: %s", method, u, resp.StatusCode, body)
}
- body, err := io.ReadAll(resp.Body)
+ out, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response from %s: %w", u, err)
}
- return body, nil
+ return out, nil
}
// entriesURL builds /v1/entries?... honoring only the non-zero filters.