tame_gcs/v1/objects/list.rs
1use crate::{
2 common::{Projection, StandardQueryParameters},
3 error::Error,
4 response::ApiResponse,
5 types::BucketName,
6};
7
8#[derive(Default, Serialize)]
9#[serde(rename_all = "camelCase")]
10pub struct ListOptional<'a> {
11 #[serde(flatten)]
12 pub standard_params: StandardQueryParameters<'a>,
13 /// Returns results in a directory-like mode. items will contain
14 /// only objects whose names, aside from the prefix, do not contain
15 /// delimiter. Objects whose names, aside from the prefix, contain
16 /// delimiter will have their name, truncated after the delimiter,
17 /// returned in prefixes. Duplicate prefixes are omitted.
18 #[serde(skip_serializing_if = "Option::is_none")]
19 pub delimiter: Option<&'a str>,
20 /// If true, objects that end in exactly one instance of delimiter
21 /// will have their metadata included in items in addition to prefixes.
22 #[serde(skip_serializing_if = "crate::util::if_false")]
23 pub include_trailing_delimiter: bool,
24 /// Maximum number of items plus prefixes to return in a single page
25 /// of responses. As duplicate prefixes are omitted, fewer total
26 /// results may be returned than requested. The service will use
27 /// this parameter or 1,000 items, whichever is smaller.
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub max_results: Option<u32>,
30 /// A previously-returned page token representing part of the larger
31 /// set of results to view.
32 ///
33 /// The pageToken is an encoded field that marks the name and generation
34 /// of the last object in the returned list. In a subsequent request
35 /// using the pageToken, items that come after the pageToken are shown
36 /// (up to maxResults).
37 ///
38 /// If you start a listing and then create an object in the bucket before
39 /// using a pageToken to continue listing, you will not see the new
40 /// object in subsequent listing results if it is in part of the object
41 /// namespace already listed.
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub page_token: Option<&'a str>,
44 /// Filter results to objects whose names begin with this prefix.
45 #[serde(skip_serializing_if = "Option::is_none")]
46 pub prefix: Option<&'a str>,
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub projection: Option<Projection>,
49 /// The project to be billed for this request.
50 /// Required for Requester Pays buckets.
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub user_project: Option<&'a str>,
53 /// If true, lists all versions of an object as distinct results.
54 /// The default is false. For more information, see Object Versioning.
55 #[serde(skip_serializing_if = "crate::util::if_false")]
56 pub versions: bool,
57}
58
59pub struct ListResponse {
60 /// The list of objects matching the query
61 pub objects: Vec<super::Metadata>,
62 /// The list of prefixes of objects matching-but-not-listed up to
63 /// and including the requested delimiter.
64 pub prefixes: Vec<String>,
65 /// The continuation token, included only if there are more items to return.
66 /// Provide this value as the `page_token` of a subsequent request in order
67 /// to return the next page of results.
68 pub page_token: Option<String>,
69}
70
71impl ApiResponse<&[u8]> for ListResponse {}
72impl ApiResponse<bytes::Bytes> for ListResponse {}
73
74impl<B> TryFrom<http::Response<B>> for ListResponse
75where
76 B: AsRef<[u8]>,
77{
78 type Error = Error;
79
80 fn try_from(response: http::Response<B>) -> Result<Self, Self::Error> {
81 let (_parts, body) = response.into_parts();
82
83 #[derive(Deserialize)]
84 #[serde(rename_all = "camelCase")]
85 struct RawListResponse {
86 next_page_token: Option<String>,
87 #[serde(default)]
88 prefixes: Vec<String>,
89 // This field won't be present if the list doesn't actually
90 // return any items
91 #[serde(default)]
92 items: Vec<super::Metadata>,
93 }
94
95 let res: RawListResponse = serde_json::from_slice(body.as_ref())?;
96
97 Ok(Self {
98 objects: res.items,
99 prefixes: res.prefixes,
100 page_token: res.next_page_token,
101 })
102 }
103}
104
105impl super::Object {
106 /// Retrieves a list of objects matching the criteria.
107 ///
108 /// Required IAM Permissions: `storage.objects.list`, `storage.objects.getIamPolicy`*
109 ///
110 /// [Complete API Documentation](https://cloud.google.com/storage/docs/json_api/v1/objects/list)
111 pub fn list(
112 &self,
113 bucket: &BucketName<'_>,
114 optional: Option<ListOptional<'_>>,
115 ) -> Result<http::Request<std::io::Empty>, Error> {
116 let mut uri = format!(
117 "https://{}/storage/v1/b/{}/o",
118 self.authority.as_str(),
119 bucket
120 );
121
122 let query = optional.unwrap_or_default();
123 let query_params = serde_urlencoded::to_string(query)?;
124 if !query_params.is_empty() {
125 uri.push('?');
126 uri.push_str(&query_params);
127 }
128
129 let req_builder = http::Request::builder();
130
131 Ok(req_builder.method("GET").uri(uri).body(std::io::empty())?)
132 }
133}