box_open_sdk/managers/
files.rs1use crate::internal::path_escape;
4use crate::runtime::{self, Error};
5
6#[derive(Clone, Debug, Default)]
8pub struct FilesGetOptions {
9 pub fields: Option<Vec<String>>,
10 pub if_none_match: Option<String>,
11 pub boxapi: Option<String>,
12 pub x_rep_hints: Option<String>,
13}
14
15#[derive(Clone, Debug, Default)]
17pub struct FilesUpdateOptions {
18 pub fields: Option<Vec<String>>,
19 pub if_match: Option<String>,
20}
21
22#[derive(Clone, Debug, Default)]
24pub struct FilesDeleteOptions {
25 pub if_match: Option<String>,
26}
27
28#[derive(Clone, Debug, Default)]
30pub struct FilesCopyOptions {
31 pub fields: Option<Vec<String>>,
32}
33
34#[derive(Clone, Debug, Default)]
36pub struct FilesGetThumbnailOptions {
37 pub min_height: Option<i64>,
38 pub min_width: Option<i64>,
39 pub max_height: Option<i64>,
40 pub max_width: Option<i64>,
41}
42
43pub struct FilesManager {
45 session: std::sync::Arc<runtime::Client>,
46}
47
48impl FilesManager {
49 pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
50 Self { session }
51 }
52
53 pub async fn get(
54 &self,
55 file_id: String,
56 opts: Option<FilesGetOptions>,
57 ) -> Result<crate::models::schemas::FileFull, Error> {
58 let mut url = self.session.base_url("api");
59 url.push_str("/files");
60 url.push('/');
61 let seg = path_escape(&file_id);
62 url.push_str(&seg);
63 let mut req = self.session.new_request("GET", &url);
64 let opts = opts.unwrap_or_default();
65 if let Some(value) = opts.fields {
66 req = runtime::with_query(req, "fields", &value.join(","));
67 }
68 if let Some(value) = opts.if_none_match {
69 req = runtime::with_header(req, "if-none-match", &value);
70 }
71 if let Some(value) = opts.boxapi {
72 req = runtime::with_header(req, "boxapi", &value);
73 }
74 if let Some(value) = opts.x_rep_hints {
75 req = runtime::with_header(req, "x-rep-hints", &value);
76 }
77 let resp = self.session.fetch(req).await?;
78 let data = runtime::response_bytes(&resp)?;
79 Ok(serde_json::from_slice(&data)?)
80 }
81
82 pub async fn update(
83 &self,
84 file_id: String,
85 body: crate::models::schemas::UpdateFileRequest,
86 opts: Option<FilesUpdateOptions>,
87 ) -> Result<crate::models::schemas::FileFull, Error> {
88 let mut url = self.session.base_url("api");
89 url.push_str("/files");
90 url.push('/');
91 let seg = path_escape(&file_id);
92 url.push_str(&seg);
93 let mut req = self.session.new_request("PUT", &url);
94 let opts = opts.unwrap_or_default();
95 if let Some(value) = opts.fields {
96 req = runtime::with_query(req, "fields", &value.join(","));
97 }
98 if let Some(value) = opts.if_match {
99 req = runtime::with_header(req, "if-match", &value);
100 }
101 let payload = serde_json::to_vec(&body)?;
102 req = runtime::with_json_body(req, &payload);
103 let resp = self.session.fetch(req).await?;
104 let data = runtime::response_bytes(&resp)?;
105 Ok(serde_json::from_slice(&data)?)
106 }
107
108 pub async fn delete(
109 &self,
110 file_id: String,
111 opts: Option<FilesDeleteOptions>,
112 ) -> Result<(), Error> {
113 let mut url = self.session.base_url("api");
114 url.push_str("/files");
115 url.push('/');
116 let seg = path_escape(&file_id);
117 url.push_str(&seg);
118 let mut req = self.session.new_request("DELETE", &url);
119 let opts = opts.unwrap_or_default();
120 if let Some(value) = opts.if_match {
121 req = runtime::with_header(req, "if-match", &value);
122 }
123 let _ = self.session.fetch(req).await?;
124 Ok(())
125 }
126
127 pub async fn copy(
128 &self,
129 file_id: String,
130 body: crate::models::schemas::CopyFileRequest,
131 opts: Option<FilesCopyOptions>,
132 ) -> Result<crate::models::schemas::FileFull, Error> {
133 let mut url = self.session.base_url("api");
134 url.push_str("/files");
135 url.push('/');
136 let seg = path_escape(&file_id);
137 url.push_str(&seg);
138 url.push_str("/copy");
139 let mut req = self.session.new_request("POST", &url);
140 let opts = opts.unwrap_or_default();
141 if let Some(value) = opts.fields {
142 req = runtime::with_query(req, "fields", &value.join(","));
143 }
144 let payload = serde_json::to_vec(&body)?;
145 req = runtime::with_json_body(req, &payload);
146 let resp = self.session.fetch(req).await?;
147 let data = runtime::response_bytes(&resp)?;
148 Ok(serde_json::from_slice(&data)?)
149 }
150
151 pub async fn get_thumbnail(
152 &self,
153 file_id: String,
154 extension: crate::models::schemas::GetIdThumbnailIdExtension,
155 opts: Option<FilesGetThumbnailOptions>,
156 ) -> Result<runtime::Stream, Error> {
157 let mut url = self.session.base_url("api");
158 url.push_str("/files");
159 url.push('/');
160 let seg = path_escape(&file_id);
161 url.push_str(&seg);
162 url.push('/');
163 url.push_str("thumbnail.");
164 let seg = path_escape(&extension.0);
165 url.push_str(&seg);
166 let mut req = self.session.new_request("GET", &url);
167 let opts = opts.unwrap_or_default();
168 if let Some(value) = opts.min_height {
169 req = runtime::with_query(req, "min_height", &value.to_string());
170 }
171 if let Some(value) = opts.min_width {
172 req = runtime::with_query(req, "min_width", &value.to_string());
173 }
174 if let Some(value) = opts.max_height {
175 req = runtime::with_query(req, "max_height", &value.to_string());
176 }
177 if let Some(value) = opts.max_width {
178 req = runtime::with_query(req, "max_width", &value.to_string());
179 }
180 let resp = self.session.fetch(req).await?;
181 Ok(runtime::response_stream(&resp))
182 }
183}