opendal/services/onedrive/
backend.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::sync::Arc;
21
22use http::Response;
23use http::StatusCode;
24
25use super::core::OneDriveCore;
26use super::delete::OneDriveDeleter;
27use super::error::parse_error;
28use super::lister::OneDriveLister;
29use super::writer::OneDriveWriter;
30use crate::raw::*;
31use crate::*;
32
33#[derive(Clone)]
34pub struct OnedriveBackend {
35 pub core: Arc<OneDriveCore>,
36}
37
38impl Debug for OnedriveBackend {
39 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40 f.debug_struct("OnedriveBackend")
41 .field("core", &self.core)
42 .finish()
43 }
44}
45
46impl Access for OnedriveBackend {
47 type Reader = HttpBody;
48 type Writer = oio::OneShotWriter<OneDriveWriter>;
49 type Lister = oio::PageLister<OneDriveLister>;
50 type Deleter = oio::OneShotDeleter<OneDriveDeleter>;
51 type BlockingReader = ();
52 type BlockingWriter = ();
53 type BlockingLister = ();
54 type BlockingDeleter = ();
55
56 fn info(&self) -> Arc<AccessorInfo> {
57 self.core.info.clone()
58 }
59
60 async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
61 if path == "/" {
62 return Ok(RpCreateDir::default());
64 }
65
66 let response = self.core.onedrive_create_dir(path).await?;
67 match response.status() {
68 StatusCode::CREATED | StatusCode::OK => Ok(RpCreateDir::default()),
69 _ => Err(parse_error(response)),
70 }
71 }
72
73 async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
74 let meta = self.core.onedrive_stat(path, args).await?;
75
76 Ok(RpStat::new(meta))
77 }
78
79 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
80 let response = self.core.onedrive_get_content(path, &args).await?;
81 match response.status() {
82 StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
83 Ok((RpRead::default(), response.into_body()))
84 }
85 _ => {
86 let (part, mut body) = response.into_parts();
87 let buf = body.to_buffer().await?;
88 Err(parse_error(Response::from_parts(part, buf)))
89 }
90 }
91 }
92
93 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
94 Ok((
95 RpWrite::default(),
96 oio::OneShotWriter::new(OneDriveWriter::new(
97 self.core.clone(),
98 args,
99 path.to_string(),
100 )),
101 ))
102 }
103
104 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
105 Ok((
106 RpDelete::default(),
107 oio::OneShotDeleter::new(OneDriveDeleter::new(self.core.clone())),
108 ))
109 }
110
111 async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
112 let monitor_url = self.core.initialize_copy(from, to).await?;
113 self.core.wait_until_complete(monitor_url).await?;
114 Ok(RpCopy::default())
115 }
116
117 async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
118 if from == to {
119 return Ok(RpRename::default());
120 }
121
122 self.core.onedrive_move(from, to).await?;
123
124 Ok(RpRename::default())
125 }
126
127 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
128 let l = OneDriveLister::new(path.to_string(), self.core.clone(), &args);
129 Ok((RpList::default(), oio::PageLister::new(l)))
130 }
131}