Skip to main content

objectstore_client/
head.rs

1use objectstore_types::metadata::Metadata;
2use reqwest::StatusCode;
3
4use crate::{ObjectKey, Session};
5
6/// The result from a successful [`head()`](Session::head) call.
7///
8/// Returns `Some(metadata)` if the object exists, `None` otherwise.
9pub type HeadResponse = Option<Metadata>;
10
11impl Session {
12    /// Checks whether an object exists and retrieves its metadata.
13    ///
14    /// If the object exists and has a TTI expiration policy, this is considered an access, and
15    /// therefore bumps its expiration.
16    pub fn head(&self, key: &str) -> HeadBuilder {
17        HeadBuilder {
18            session: self.clone(),
19            key: key.to_owned(),
20        }
21    }
22}
23
24/// A [`head`](Session::head) request builder.
25#[derive(Debug)]
26pub struct HeadBuilder {
27    pub(crate) session: Session,
28    pub(crate) key: ObjectKey,
29}
30
31impl HeadBuilder {
32    /// Sends the head request.
33    pub async fn send(self) -> crate::Result<HeadResponse> {
34        let response = self
35            .session
36            .request(reqwest::Method::HEAD, &self.key)?
37            .send()
38            .await?;
39        if response.status() == StatusCode::NOT_FOUND {
40            return Ok(None);
41        }
42        let response = response.error_for_status()?;
43        let metadata = Metadata::from_headers(response.headers(), "")?;
44        Ok(Some(metadata))
45    }
46}