brewlog/src/infrastructure/client/backup.rs
Jon Seager 50a372015c
feat(backup): expose backup/restore via authenticated API endpoints
- Add GET /api/v1/backup and POST /api/v1/backup/restore endpoints
  behind AuthenticatedUser
- Add BackupService to AppState and BackupClient for HTTP access
- Update CLI backup/restore to use API instead of direct DB access
- Remove --database-url flag from backup and restore commands
- Increase body limit to 50MB for restore endpoint
- Add API and CLI tests for auth, export, restore, and round-trip
- Update README to document auth requirement and API endpoints
2026-02-04 19:38:49 +00:00

44 lines
1.2 KiB
Rust

use anyhow::{Context, Result};
use reqwest::StatusCode;
use crate::infrastructure::backup::BackupData;
use super::BrewlogClient;
pub struct BackupClient<'a> {
inner: &'a BrewlogClient,
}
impl<'a> BackupClient<'a> {
pub(crate) fn new(inner: &'a BrewlogClient) -> Self {
Self { inner }
}
pub async fn export(&self) -> Result<BackupData> {
let url = self.inner.endpoint("api/v1/backup")?;
let response = self
.inner
.request(reqwest::Method::GET, url)
.send()
.await
.context("failed to issue backup export request")?;
self.inner.handle_response(response).await
}
pub async fn restore(&self, data: &BackupData) -> Result<()> {
let url = self.inner.endpoint("api/v1/backup/restore")?;
let response = self
.inner
.request(reqwest::Method::POST, url)
.json(data)
.send()
.await
.context("failed to issue backup restore request")?;
match response.status() {
StatusCode::NO_CONTENT => Ok(()),
_ => Err(self.inner.response_error(response).await),
}
}
}