fix(#1233): subscription --watch uses PUT, GET handles 404 as not-watching

This commit is contained in:
damocles 2026-06-03 23:19:08 +02:00 committed by mara
commit d3239fef35
2 changed files with 36 additions and 7 deletions

View file

@ -93,6 +93,17 @@ impl Client {
decode_json(resp, &format!("GET {url}"))
}
/// GET `<api>/<path>` and decode JSON. Returns `None` if the
/// server responds with 404 (resource absent rather than an error).
pub fn get_json_optional(&self, path: &str) -> Result<Option<Value>> {
let url = format!("{}{}", self.api(), path);
let resp = self.http.get(&url).send().context("GET")?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(None);
}
decode_json(resp, &format!("GET {url}")).map(Some)
}
/// GET a paginated list endpoint and concatenate all pages.
/// `path` should NOT include `page=` (we own it); other query
/// params (`?limit=N&state=open&...`) are preserved. Pages drain
@ -160,6 +171,19 @@ impl Client {
decode_json(resp, &format!("PATCH {url}"))
}
/// PUT a JSON body to `<api>/<path>` and decode the response.
pub fn put_json<B: Serialize>(&self, path: &str, body: &B) -> Result<Value> {
let url = format!("{}{}", self.api(), path);
let resp = self
.http
.put(&url)
.header(CONTENT_TYPE, "application/json")
.json(body)
.send()
.context("PUT")?;
decode_json(resp, &format!("PUT {url}"))
}
/// DELETE `<api>/<path>`. Optional JSON body for endpoints that
/// need it (Forgejo's subscription unwatch uses bodyless DELETE).
pub fn delete(&self, path: &str, body: Option<&Value>) -> Result<()> {