feat: add bags API client
This commit is contained in:
parent
dbfc6c4775
commit
ef6f30d6c3
2 changed files with 116 additions and 0 deletions
111
src/infrastructure/client/bags.rs
Normal file
111
src/infrastructure/client/bags.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
use anyhow::{Context, Result};
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::domain::bags::{Bag, UpdateBag};
|
||||
use crate::domain::ids::{BagId, RoastId};
|
||||
|
||||
use super::BrewlogClient;
|
||||
|
||||
pub struct BagsClient<'a> {
|
||||
inner: &'a BrewlogClient,
|
||||
}
|
||||
|
||||
impl<'a> BagsClient<'a> {
|
||||
pub(crate) fn new(inner: &'a BrewlogClient) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
&self,
|
||||
roast_id: RoastId,
|
||||
roast_date: Option<NaiveDate>,
|
||||
amount: f64,
|
||||
) -> Result<Bag> {
|
||||
let url = self.inner.endpoint("api/v1/bags")?;
|
||||
let payload = serde_json::json!({
|
||||
"roast_id": roast_id,
|
||||
"roast_date": roast_date.map(|d| d.to_string()),
|
||||
"amount": amount,
|
||||
});
|
||||
|
||||
let response = self
|
||||
.inner
|
||||
.request(reqwest::Method::POST, url)
|
||||
.json(&payload)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to issue create bag request")?;
|
||||
|
||||
self.inner.handle_response(response).await
|
||||
}
|
||||
|
||||
pub async fn list(&self, roast_id: Option<RoastId>) -> Result<Vec<Bag>> {
|
||||
let mut url = self.inner.endpoint("api/v1/bags")?;
|
||||
if let Some(roast_id) = roast_id {
|
||||
url.query_pairs_mut()
|
||||
.append_pair("roast_id", &roast_id.to_string());
|
||||
}
|
||||
|
||||
let response = self
|
||||
.inner
|
||||
.request(reqwest::Method::GET, url)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to issue list bags request")?;
|
||||
|
||||
self.inner.handle_response(response).await
|
||||
}
|
||||
|
||||
pub async fn get(&self, id: BagId) -> Result<Bag> {
|
||||
let url = self.inner.endpoint(&format!("api/v1/bags/{id}"))?;
|
||||
let response = self
|
||||
.inner
|
||||
.request(reqwest::Method::GET, url)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to issue get bag request")?;
|
||||
|
||||
self.inner.handle_response(response).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
&self,
|
||||
id: BagId,
|
||||
remaining: Option<f64>,
|
||||
closed: Option<bool>,
|
||||
finished_at: Option<NaiveDate>,
|
||||
) -> Result<Bag> {
|
||||
let url = self.inner.endpoint(&format!("api/v1/bags/{id}"))?;
|
||||
let payload = UpdateBag {
|
||||
remaining,
|
||||
closed,
|
||||
finished_at,
|
||||
};
|
||||
|
||||
let response = self
|
||||
.inner
|
||||
.request(reqwest::Method::PUT, url)
|
||||
.json(&payload)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to issue update bag request")?;
|
||||
|
||||
self.inner.handle_response(response).await
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: BagId) -> Result<()> {
|
||||
let url = self.inner.endpoint(&format!("api/v1/bags/{id}"))?;
|
||||
let response = self
|
||||
.inner
|
||||
.request(reqwest::Method::DELETE, url)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to issue delete bag request")?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.inner.response_error(response).await)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod bags;
|
||||
pub mod roasters;
|
||||
pub mod roasts;
|
||||
pub mod tokens;
|
||||
|
|
@ -51,6 +52,10 @@ impl BrewlogClient {
|
|||
tokens::TokensClient::new(self)
|
||||
}
|
||||
|
||||
pub fn bags(&self) -> bags::BagsClient<'_> {
|
||||
bags::BagsClient::new(self)
|
||||
}
|
||||
|
||||
pub(crate) fn endpoint(&self, path: &str) -> Result<Url> {
|
||||
self.base_url
|
||||
.join(path)
|
||||
|
|
|
|||
Loading…
Reference in a new issue