1use crate::Error;
4use crate::api::v3::ApiV3Client;
5use crate::api::v4::ApiV4Client as ApiV4ClientInner;
6use crate::api::{ApiVersion, VersionInfo};
7use log::debug;
8
9pub enum UnifiedClient {
11 V3(ApiV3Client),
12 V4(ApiV4ClientInner),
13}
14
15impl UnifiedClient {
16 pub async fn new(base_url: &str) -> Result<Self, Error> {
18 Self::with_version(base_url, None).await
19 }
20
21 pub async fn with_version(base_url: &str, version: Option<ApiVersion>) -> Result<Self, Error> {
23 let base_url = base_url.trim_end_matches('/');
24
25 match version {
26 Some(ApiVersion::V3) => {
27 debug!("Creating V3 client for {}", base_url);
28 Ok(UnifiedClient::V3(ApiV3Client::new(base_url)))
29 }
30 Some(ApiVersion::V4) => {
31 debug!("Creating V4 client for {}", base_url);
32 Ok(UnifiedClient::V4(ApiV4ClientInner::new(base_url)))
33 }
34 None => {
35 debug!("Auto-detecting API version for {}", base_url);
37 Self::detect_version(base_url).await
38 }
39 }
40 }
41
42 async fn detect_version(base_url: &str) -> Result<Self, Error> {
44 let base_url = base_url.trim_end_matches('/');
45
46 debug!("Trying V4 endpoint...");
48 let v4_client = ApiV4ClientInner::new(base_url);
49 match v4_client.ping().await {
50 Ok(_) => {
51 debug!("V4 endpoint available, using V4 client");
52 return Ok(UnifiedClient::V4(v4_client));
53 }
54 Err(e) => {
55 debug!("V4 endpoint failed: {}", e);
56 }
57 }
58
59 debug!("Trying V3 endpoint...");
61 let v3_client = ApiV3Client::new(base_url);
62 match v3_client.ping().await {
63 Ok(_) => {
64 debug!("V3 endpoint available, using V3 client");
65 Ok(UnifiedClient::V3(v3_client))
66 }
67 Err(e) => {
68 debug!("V3 endpoint failed: {}", e);
69 Err(Error::InvalidResponse(
70 "Could not detect API version. Neither V3 nor V4 endpoints responded."
71 .to_string(),
72 ))
73 }
74 }
75 }
76
77 pub async fn get_version(&self) -> Result<VersionInfo, Error> {
79 match self {
80 UnifiedClient::V3(client) => client.get_version().await,
81 UnifiedClient::V4(client) => client.get_version().await,
82 }
83 }
84
85 pub fn api_version(&self) -> ApiVersion {
87 match self {
88 UnifiedClient::V3(_) => ApiVersion::V3,
89 UnifiedClient::V4(_) => ApiVersion::V4,
90 }
91 }
92
93 pub fn base_url(&self) -> &str {
95 match self {
96 UnifiedClient::V3(client) => &client.base_url,
97 UnifiedClient::V4(client) => &client.base_url,
98 }
99 }
100
101 pub fn is_v3(&self) -> bool {
103 matches!(self, UnifiedClient::V3(_))
104 }
105
106 pub fn is_v4(&self) -> bool {
108 matches!(self, UnifiedClient::V4(_))
109 }
110
111 pub fn as_v3(&self) -> Option<&ApiV3Client> {
113 match self {
114 UnifiedClient::V3(client) => Some(client),
115 _ => None,
116 }
117 }
118
119 pub fn as_v4(&self) -> Option<&ApiV4ClientInner> {
121 match self {
122 UnifiedClient::V4(client) => Some(client),
123 _ => None,
124 }
125 }
126
127 pub fn as_v3_mut(&mut self) -> Option<&mut ApiV3Client> {
129 match self {
130 UnifiedClient::V3(client) => Some(client),
131 _ => None,
132 }
133 }
134
135 pub fn as_v4_mut(&mut self) -> Option<&mut ApiV4ClientInner> {
137 match self {
138 UnifiedClient::V4(client) => Some(client),
139 _ => None,
140 }
141 }
142
143 pub async fn upload_file(
145 &self,
146 path: &str,
147 content: Vec<u8>,
148 policy_id: Option<&str>,
149 overwrite: bool,
150 ) -> Result<(), Error> {
151 match self {
152 UnifiedClient::V3(client) => {
153 let parent_dir = if let Some(pos) = path.rfind('/') {
155 if pos == 0 { "/" } else { &path[..pos] }
156 } else {
157 "/"
158 };
159 let file_name = path.rsplit('/').next().unwrap_or("file");
160
161 let dir_list = client.list_directory(parent_dir).await?;
162 let policy_id = policy_id.unwrap_or(&dir_list.policy.id);
163
164 let request = crate::api::v3::models::UploadFileRequest {
165 path: parent_dir,
166 name: file_name,
167 policy_id,
168 size: content.len() as i64,
169 last_modified: 0,
170 mime_type: "",
171 };
172 let session = client.upload_file(&request).await?;
173 client.upload_chunk(&session.session_id, 0, content).await?;
174
175 let _ = client.complete_upload(&session.session_id).await;
177 Ok(())
178 }
179 UnifiedClient::V4(client) => {
180 let parent_dir = if let Some(pos) = path.rfind('/') {
182 if pos == 0 { "/" } else { &path[..pos] }
183 } else {
184 "/"
185 };
186
187 let policy_id_str = match client
188 .list_files(&crate::api::v4::models::ListFilesRequest {
189 path: parent_dir,
190 page: Some(0),
191 page_size: Some(1),
192 ..Default::default()
193 })
194 .await
195 {
196 Ok(response) => response
197 .storage_policy
198 .map(|p| p.id)
199 .unwrap_or_else(|| "default".to_string()),
200 Err(_) => "default".to_string(),
201 };
202
203 let entity_type = if overwrite { Some("version") } else { None };
205 let request = crate::api::v4::models::CreateUploadSessionRequest {
206 uri: &crate::api::v4::uri::path_to_uri(path),
207 size: content.len() as u64,
208 policy_id: &policy_id_str,
209 last_modified: None,
210 mime_type: None,
211 metadata: None,
212 entity_type,
213 };
214 let session = client.create_upload_session(&request).await?;
215 client
216 .upload_file_chunk(&session.session_id, 0, &content)
217 .await?;
218 Ok(())
219 }
220 }
221 }
222}
223
224impl Clone for UnifiedClient {
226 fn clone(&self) -> Self {
227 match self {
228 UnifiedClient::V3(client) => UnifiedClient::V3(client.clone()),
229 UnifiedClient::V4(client) => UnifiedClient::V4(client.clone()),
230 }
231 }
232}
233
234impl std::fmt::Debug for UnifiedClient {
235 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
236 match self {
237 UnifiedClient::V3(client) => f.debug_tuple("UnifiedClient::V3").field(client).finish(),
238 UnifiedClient::V4(client) => f.debug_tuple("UnifiedClient::V4").field(client).finish(),
239 }
240 }
241}