Skip to main content

tame_gcs/v1/objects/
get.rs

1use crate::{
2    common::{Conditionals, Projection, StandardQueryParameters},
3    error::Error,
4    response::ApiResponse,
5    types::ObjectIdentifier,
6};
7
8#[derive(Default, Serialize)]
9#[serde(rename_all = "camelCase")]
10pub struct GetObjectOptional<'a> {
11    #[serde(flatten)]
12    pub standard_params: StandardQueryParameters<'a>,
13    /// If present, selects 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    /// Set of properties to return. Defaults to `noAcl`, unless the object
20    /// resource specifies the acl property, when it defaults to full.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub projection: Option<Projection>,
23    /// The project to be billed for this request. Required for Requester Pays buckets.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub user_project: Option<&'a str>,
26}
27
28pub struct GetObjectResponse {
29    pub metadata: super::Metadata,
30}
31
32impl ApiResponse<&[u8]> for GetObjectResponse {}
33impl ApiResponse<bytes::Bytes> for GetObjectResponse {}
34
35impl<B> TryFrom<http::Response<B>> for GetObjectResponse
36where
37    B: AsRef<[u8]>,
38{
39    type Error = Error;
40
41    fn try_from(response: http::Response<B>) -> Result<Self, Self::Error> {
42        let (_parts, body) = response.into_parts();
43        let metadata: super::Metadata = serde_json::from_slice(body.as_ref())?;
44        Ok(Self { metadata })
45    }
46}
47
48impl super::Object {
49    /// Gets an object's metadata
50    ///
51    /// Required IAM Permissions: `storage.objects.get`, `storage.objects.getIamPolicy`*
52    ///
53    /// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/get)
54    pub fn get<'a, OID>(
55        &self,
56        id: &OID,
57        optional: Option<GetObjectOptional<'_>>,
58    ) -> Result<http::Request<std::io::Empty>, Error>
59    where
60        OID: ObjectIdentifier<'a> + ?Sized,
61    {
62        let mut uri = crate::__make_obj_url!(
63            "https://{}/storage/v1/b/{}/o/{}?alt=json",
64            self.authority,
65            id
66        );
67
68        let query = optional.unwrap_or_default();
69        let query_params = serde_urlencoded::to_string(query)?;
70        if !query_params.is_empty() {
71            uri.push('&');
72            uri.push_str(&query_params);
73        }
74
75        let req_builder = http::Request::builder();
76
77        Ok(req_builder.method("GET").uri(uri).body(std::io::empty())?)
78    }
79}