1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use http::Request;
use crate::*;
#[derive(Debug, Clone, Default)]
pub struct RpCreate {}
#[derive(Debug, Clone, Default)]
pub struct RpDelete {}
#[derive(Debug, Clone, Default)]
pub struct RpList {}
#[derive(Debug, Clone, Default)]
pub struct RpCreateMultipart {
upload_id: String,
}
impl RpCreateMultipart {
pub fn new(upload_id: &str) -> Self {
Self {
upload_id: upload_id.to_string(),
}
}
pub fn upload_id(&self) -> &str {
&self.upload_id
}
}
#[derive(Debug, Clone)]
pub struct RpWriteMultipart {
part_number: usize,
etag: String,
}
impl RpWriteMultipart {
pub fn new(part_number: usize, etag: &str) -> Self {
Self {
part_number,
etag: etag.to_string(),
}
}
pub fn part_number(&self) -> usize {
self.part_number
}
pub fn etag(&self) -> &str {
&self.etag
}
pub fn into_object_part(self) -> ObjectPart {
ObjectPart::new(self.part_number, &self.etag)
}
}
#[derive(Debug, Clone, Default)]
pub struct RpCompleteMultipart {}
#[derive(Debug, Clone, Default)]
pub struct RpAbortMultipart {}
#[derive(Debug, Clone)]
pub struct RpPresign {
req: PresignedRequest,
}
impl RpPresign {
pub fn new(req: PresignedRequest) -> Self {
RpPresign { req }
}
pub fn into_presigned_request(self) -> PresignedRequest {
self.req
}
}
#[derive(Debug, Clone)]
pub struct PresignedRequest {
method: http::Method,
uri: http::Uri,
headers: http::HeaderMap,
}
impl PresignedRequest {
pub fn new(method: http::Method, uri: http::Uri, headers: http::HeaderMap) -> Self {
Self {
method,
uri,
headers,
}
}
pub fn method(&self) -> &http::Method {
&self.method
}
pub fn uri(&self) -> &http::Uri {
&self.uri
}
pub fn header(&self) -> &http::HeaderMap {
&self.headers
}
}
impl<T: Default> From<PresignedRequest> for Request<T> {
fn from(v: PresignedRequest) -> Self {
let mut builder = Request::builder().method(v.method).uri(v.uri);
let headers = builder.headers_mut().expect("header map must be valid");
headers.extend(v.headers);
builder
.body(T::default())
.expect("request must build succeed")
}
}
#[derive(Debug, Clone)]
pub struct RpRead {
meta: ObjectMetadata,
}
impl RpRead {
pub fn new(content_length: u64) -> Self {
RpRead {
meta: ObjectMetadata::new(ObjectMode::FILE).with_content_length(content_length),
}
}
pub fn with_metadata(meta: ObjectMetadata) -> Self {
RpRead { meta }
}
pub fn into_metadata(self) -> ObjectMetadata {
self.meta
}
}
#[derive(Debug, Clone)]
pub struct RpStat {
meta: ObjectMetadata,
}
impl RpStat {
pub fn new(meta: ObjectMetadata) -> Self {
RpStat { meta }
}
pub fn into_metadata(self) -> ObjectMetadata {
self.meta
}
}
#[derive(Debug, Clone, Default)]
pub struct RpWrite {
written: u64,
}
impl RpWrite {
pub fn new(written: u64) -> Self {
Self { written }
}
pub fn written(&self) -> u64 {
self.written
}
}
#[cfg(test)]
mod tests {
use anyhow::Result;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::HeaderMap;
use http::Method;
use http::Uri;
use super::*;
use crate::raw::*;
#[test]
fn test_presigned_request_convert() -> Result<()> {
let pr = PresignedRequest {
method: Method::PATCH,
uri: Uri::from_static("https://opendal.databend.rs/path/to/file"),
headers: {
let mut headers = HeaderMap::new();
headers.insert(CONTENT_LENGTH, "123".parse()?);
headers.insert(CONTENT_TYPE, "application/json".parse()?);
headers
},
};
let req: Request<AsyncBody> = pr.clone().into();
assert_eq!(Method::PATCH, req.method());
assert_eq!(
"https://opendal.databend.rs/path/to/file",
req.uri().to_string()
);
assert_eq!("123", req.headers().get(CONTENT_LENGTH).unwrap());
assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap());
let req: Request<Body> = pr.into();
assert_eq!(Method::PATCH, req.method());
assert_eq!(
"https://opendal.databend.rs/path/to/file",
req.uri().to_string()
);
assert_eq!("123", req.headers().get(CONTENT_LENGTH).unwrap());
assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap());
Ok(())
}
}