reduct_rs/record/
remove_record.rs

1// Copyright 2024 ReductStore
2// This Source Code Form is subject to the terms of the Mozilla Public
3//    License, v. 2.0. If a copy of the MPL was not distributed with this
4//    file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
6use crate::http_client::HttpClient;
7use crate::record::from_system_time;
8use http::Method;
9use reduct_base::error::ReductError;
10use std::sync::Arc;
11
12/// Builder for a remove record request.
13pub struct RemoveRecordBuilder {
14    bucket: String,
15    entry: String,
16    timestamp: Option<u64>,
17    client: Arc<HttpClient>,
18}
19
20impl RemoveRecordBuilder {
21    pub(crate) fn new(bucket: String, entry: String, client: Arc<HttpClient>) -> Self {
22        Self {
23            bucket,
24            entry,
25            timestamp: None,
26            client,
27        }
28    }
29
30    /// Set the timestamp of the record to remove as a unix timestamp in microseconds.
31    pub fn timestamp_us(mut self, timestamp: u64) -> Self {
32        self.timestamp = Some(timestamp);
33        self
34    }
35
36    /// Set the timestamp of the record to remove.
37    pub fn timestamp(mut self, timestamp: std::time::SystemTime) -> Self {
38        self.timestamp = Some(from_system_time(timestamp));
39        self
40    }
41
42    /// Send the remove record request.
43    ///
44    /// # Returns
45    ///
46    /// Returns an error if the record could not be removed.
47    ///
48    /// # Panics
49    ///
50    /// Panics if the timestamp is not set.
51    pub async fn send(self) -> Result<(), ReductError> {
52        let request = self.client.request(
53            Method::DELETE,
54            &format!(
55                "/b/{}/{}?ts={}",
56                self.bucket,
57                self.entry,
58                self.timestamp.expect("timestamp is required")
59            ),
60        );
61        self.client.send_request(request).await?;
62        Ok(())
63    }
64}