Skip to main content

tame_gcs/v1/objects/
delete.rs

1use crate::{
2    common::{Conditionals, StandardQueryParameters},
3    error::Error,
4    response::ApiResponse,
5    types::ObjectIdentifier,
6};
7
8#[derive(Default, Serialize)]
9#[serde(rename_all = "camelCase")]
10pub struct DeleteObjectOptional<'a> {
11    #[serde(flatten)]
12    pub standard_params: StandardQueryParameters<'a>,
13    /// If present, permanently deletes a specific revision of this object
14    /// (as opposed to the latest version, the default).
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub generation: Option<i64>,
17    #[serde(flatten)]
18    pub conditionals: Conditionals,
19    /// The project to be billed for this request. Required for Requester Pays buckets.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub user_project: Option<&'a str>,
22}
23
24pub struct DeleteObjectResponse;
25
26impl ApiResponse<&[u8]> for DeleteObjectResponse {}
27impl ApiResponse<bytes::Bytes> for DeleteObjectResponse {}
28
29impl<B> TryFrom<http::Response<B>> for DeleteObjectResponse
30where
31    B: AsRef<[u8]>,
32{
33    type Error = Error;
34
35    fn try_from(response: http::Response<B>) -> Result<Self, Self::Error> {
36        if response.status() == http::StatusCode::NO_CONTENT {
37            Ok(Self)
38        } else {
39            Err(Self::Error::from(response.status()))
40        }
41    }
42}
43
44impl super::Object {
45    /// Deletes an object and its metadata. Deletions are permanent if versioning
46    /// is not enabled for the bucket, or if the generation parameter is used.
47    ///
48    /// Required IAM Permissions: `storage.objects.delete`
49    ///
50    /// [Complete API documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/delete)
51    pub fn delete<'a, OID>(
52        &self,
53        id: &OID,
54        optional: Option<DeleteObjectOptional<'_>>,
55    ) -> Result<http::Request<std::io::Empty>, Error>
56    where
57        OID: ObjectIdentifier<'a> + ?Sized,
58    {
59        let mut uri = crate::__make_obj_url!("https://{}/storage/v1/b/{}/o/{}", self.authority, id);
60
61        let query = optional.unwrap_or_default();
62        let query_params = serde_urlencoded::to_string(query)?;
63        if !query_params.is_empty() {
64            uri.push('?');
65            uri.push_str(&query_params);
66        }
67
68        let req_builder = http::Request::builder();
69
70        Ok(req_builder
71            .method("DELETE")
72            .uri(uri)
73            .body(std::io::empty())?)
74    }
75}