use http::Request;
use crate::raw::*;
use crate::*;
#[derive(Debug, Clone, Default)]
pub struct RpCreateDir {}
#[derive(Debug, Clone, Default)]
pub struct RpDelete {}
#[derive(Debug, Clone, Default)]
pub struct RpList {}
#[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, Default)]
pub struct RpRead {
size: Option<u64>,
range: Option<BytesContentRange>,
}
impl RpRead {
pub fn new() -> Self {
RpRead::default()
}
pub fn size(&self) -> Option<u64> {
self.size
}
pub fn with_size(mut self, size: Option<u64>) -> Self {
self.size = size;
self
}
pub fn range(&self) -> Option<BytesContentRange> {
self.range
}
pub fn with_range(mut self, range: Option<BytesContentRange>) -> Self {
self.range = range;
self
}
}
#[derive(Debug, Clone)]
pub struct RpStat {
meta: Metadata,
}
impl RpStat {
pub fn new(meta: Metadata) -> Self {
RpStat { meta }
}
pub fn map_metadata(mut self, f: impl FnOnce(Metadata) -> Metadata) -> Self {
self.meta = f(self.meta);
self
}
pub fn into_metadata(self) -> Metadata {
self.meta
}
}
#[derive(Debug, Clone, Default)]
pub struct RpWrite {}
impl RpWrite {
pub fn new() -> Self {
Self {}
}
}
#[derive(Debug, Clone, Default)]
pub struct RpCopy {}
impl RpCopy {
pub fn new() -> Self {
Self {}
}
}
#[derive(Debug, Clone, Default)]
pub struct RpRename {}
impl RpRename {
pub fn new() -> Self {
Self {}
}
}
#[cfg(test)]
mod tests {
use anyhow::Result;
use http::HeaderMap;
use http::Method;
use http::Uri;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use super::*;
#[test]
fn test_presigned_request_convert() -> Result<()> {
let pr = PresignedRequest {
method: Method::PATCH,
uri: Uri::from_static("https://opendal.apache.org/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<Buffer> = pr.into();
assert_eq!(Method::PATCH, req.method());
assert_eq!(
"https://opendal.apache.org/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(())
}
}