opendal/services/azdls/
writer.rs1use std::sync::Arc;
19
20use http::StatusCode;
21
22use super::core::AzdlsCore;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub type AzdlsWriters = TwoWays<oio::OneShotWriter<AzdlsWriter>, oio::AppendWriter<AzdlsWriter>>;
28
29pub struct AzdlsWriter {
30 core: Arc<AzdlsCore>,
31
32 op: OpWrite,
33 path: String,
34}
35
36impl AzdlsWriter {
37 pub fn new(core: Arc<AzdlsCore>, op: OpWrite, path: String) -> Self {
38 AzdlsWriter { core, op, path }
39 }
40}
41
42impl oio::OneShotWrite for AzdlsWriter {
43 async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
44 let mut req =
45 self.core
46 .azdls_create_request(&self.path, "file", &self.op, Buffer::new())?;
47
48 self.core.sign(&mut req).await?;
49
50 let resp = self.core.send(req).await?;
51
52 let status = resp.status();
53 match status {
54 StatusCode::CREATED | StatusCode::OK => {}
55 _ => {
56 return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
57 }
58 }
59
60 let mut req = self
61 .core
62 .azdls_update_request(&self.path, Some(bs.len() as u64), 0, bs)?;
63
64 self.core.sign(&mut req).await?;
65
66 let resp = self.core.send(req).await?;
67
68 let status = resp.status();
69 match status {
70 StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
71 _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
72 }
73 }
74}
75
76impl oio::AppendWrite for AzdlsWriter {
77 async fn offset(&self) -> Result<u64> {
78 let resp = self.core.azdls_get_properties(&self.path).await?;
79
80 let status = resp.status();
81 let headers = resp.headers();
82
83 match status {
84 StatusCode::OK => Ok(parse_content_length(headers)?.unwrap_or_default()),
85 StatusCode::NOT_FOUND => Ok(0),
86 _ => Err(parse_error(resp)),
87 }
88 }
89
90 async fn append(&self, offset: u64, size: u64, body: Buffer) -> Result<Metadata> {
91 if offset == 0 {
92 let mut req =
93 self.core
94 .azdls_create_request(&self.path, "file", &self.op, Buffer::new())?;
95
96 self.core.sign(&mut req).await?;
97
98 let resp = self.core.send(req).await?;
99
100 let status = resp.status();
101 match status {
102 StatusCode::CREATED | StatusCode::OK => {}
103 _ => {
104 return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
105 }
106 }
107 }
108
109 let mut req = self
110 .core
111 .azdls_update_request(&self.path, Some(size), offset, body)?;
112
113 self.core.sign(&mut req).await?;
114
115 let resp = self.core.send(req).await?;
116
117 let status = resp.status();
118 match status {
119 StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
120 _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
121 }
122 }
123}