pub struct Cart<'a> { /* private fields */ }Implementations§
Source§impl<'a> Cart<'a>
impl<'a> Cart<'a>
pub fn new(api: &'a dyn KrApi) -> Self
Sourcepub async fn active(&self, store_id: &str) -> Result<Basket, ApiError>
pub async fn active(&self, store_id: &str) -> Result<Basket, ApiError>
Fetch (or implicitly create) the active basket for a store.
On an anonymous session this happily returns a fresh, valid basket rather
than a 401 – so a successful call is not evidence of being logged in.
Check userInfo for that.
Sourcepub async fn apply(
&self,
basket_id: &str,
events: &[BasketEvent],
) -> Result<Basket, ApiError>
pub async fn apply( &self, basket_id: &str, events: &[BasketEvent], ) -> Result<Basket, ApiError>
Apply a batch of events. The endpoint always takes an array, even for one change, and returns the whole updated basket.
Sourcepub async fn mutate(
&self,
store_id: &str,
events: &[BasketEvent],
) -> Result<Basket, ApiError>
pub async fn mutate( &self, store_id: &str, events: &[BasketEvent], ) -> Result<Basket, ApiError>
Read the cart, apply events to it, and return the result. Saves callers
from threading the basket id around; basket/active is cheap.
Sourcepub async fn add(
&self,
store_id: &str,
ean: &str,
amount: f64,
unit: &str,
local_store_id: Option<String>,
allow_substitutes: bool,
) -> Result<Basket, ApiError>
pub async fn add( &self, store_id: &str, ean: &str, amount: f64, unit: &str, local_store_id: Option<String>, allow_substitutes: bool, ) -> Result<Basket, ApiError>
amount is the resulting quantity, not a delta.
Observed live: ADD-ITEM for an EAN already in the basket replaces that
item’s amount rather than accumulating – add 1 twice and the cart holds 1.
K-Ruoka’s own frontend never sends ADD-ITEM for an item already present
(it switches to SET-ITEM-AMOUNT), so this path is outside what the site
itself exercises; the behaviour was measured rather than assumed.
Sourcepub async fn set_amount(
&self,
store_id: &str,
item_id: &str,
amount: f64,
unit: Option<&str>,
) -> Result<Basket, ApiError>
pub async fn set_amount( &self, store_id: &str, item_id: &str, amount: f64, unit: Option<&str>, ) -> Result<Basket, ApiError>
amount of 0 removes the item – verified live, the server handles it
rather than needing a REMOVE-ITEM translation the way the frontend does.
unit defaults to the unit the item already carries. Defaulting it to
"kpl" instead would silently convert a kg item to pieces, which is
corruption rather than a no-op.
pub async fn remove( &self, store_id: &str, item_id: &str, ) -> Result<Basket, ApiError>
Sourcepub async fn clear(&self, store_id: &str) -> Result<Basket, ApiError>
pub async fn clear(&self, store_id: &str) -> Result<Basket, ApiError>
Empty the basket via CLEAR-ITEMS.
DELETE /kr-api/basket/by-id/{id} also exists and destroys the basket
itself. CLEAR-ITEMS is preferred: it leaves the basket and its settings
in place, and returns the emptied basket so the caller can see the result.