diff --git a/src/infrastructure/client/cups.rs b/src/infrastructure/client/cups.rs new file mode 100644 index 0000000..d09ab78 --- /dev/null +++ b/src/infrastructure/client/cups.rs @@ -0,0 +1,82 @@ +use anyhow::{Context, Result}; +use reqwest::StatusCode; + +use crate::domain::cups::{Cup, CupWithDetails, NewCup, UpdateCup}; +use crate::domain::ids::CupId; + +use super::BrewlogClient; + +pub struct CupsClient<'a> { + inner: &'a BrewlogClient, +} + +impl<'a> CupsClient<'a> { + pub(crate) fn new(inner: &'a BrewlogClient) -> Self { + Self { inner } + } + + pub async fn create(&self, payload: &NewCup) -> Result { + let url = self.inner.endpoint("api/v1/cups")?; + let response = self + .inner + .request(reqwest::Method::POST, url) + .json(payload) + .send() + .await + .context("failed to issue create cup request")?; + + self.inner.handle_response(response).await + } + + pub async fn list(&self) -> Result> { + let url = self.inner.endpoint("api/v1/cups")?; + let response = self + .inner + .request(reqwest::Method::GET, url) + .send() + .await + .context("failed to issue list cups request")?; + + self.inner.handle_response(response).await + } + + pub async fn get(&self, id: CupId) -> Result { + let url = self.inner.endpoint(&format!("api/v1/cups/{id}"))?; + let response = self + .inner + .request(reqwest::Method::GET, url) + .send() + .await + .context("failed to issue get cup request")?; + + self.inner.handle_response(response).await + } + + pub async fn update(&self, id: CupId, payload: &UpdateCup) -> Result { + let url = self.inner.endpoint(&format!("api/v1/cups/{id}"))?; + let response = self + .inner + .request(reqwest::Method::PUT, url) + .json(payload) + .send() + .await + .context("failed to issue update cup request")?; + + self.inner.handle_response(response).await + } + + pub async fn delete(&self, id: CupId) -> Result<()> { + let url = self.inner.endpoint(&format!("api/v1/cups/{id}"))?; + let response = self + .inner + .request(reqwest::Method::DELETE, url) + .send() + .await + .context("failed to issue delete cup request")?; + + match response.status() { + StatusCode::NO_CONTENT => Ok(()), + _ => Err(self.inner.response_error(response).await), + } + } +} diff --git a/src/infrastructure/client/mod.rs b/src/infrastructure/client/mod.rs index 6b0698b..ef3b8be 100644 --- a/src/infrastructure/client/mod.rs +++ b/src/infrastructure/client/mod.rs @@ -1,6 +1,7 @@ pub mod bags; pub mod brews; pub mod cafes; +pub mod cups; pub mod gear; pub mod roasters; pub mod roasts; @@ -71,6 +72,10 @@ impl BrewlogClient { cafes::CafesClient::new(self) } + pub fn cups(&self) -> cups::CupsClient<'_> { + cups::CupsClient::new(self) + } + pub(crate) fn endpoint(&self, path: &str) -> Result { self.base_url .join(path)