use std::fmt::Debug;
use std::fmt::Formatter;
use bytes::Bytes;
use http::header;
use http::Request;
use http::Response;
use serde::Deserialize;
use serde_json::json;
use crate::raw::*;
use crate::*;
#[derive(Clone)]
pub struct ChainsafeCore {
pub root: String,
pub api_key: String,
pub bucket_id: String,
pub client: HttpClient,
}
impl Debug for ChainsafeCore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Backend")
.field("root", &self.root)
.field("bucket_id", &self.bucket_id)
.finish_non_exhaustive()
}
}
impl ChainsafeCore {
#[inline]
pub async fn send(&self, req: Request<Buffer>) -> Result<Response<Buffer>> {
self.client.send(req).await
}
}
impl ChainsafeCore {
pub async fn download_object(
&self,
path: &str,
range: BytesRange,
) -> Result<Response<HttpBody>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/download",
self.bucket_id
);
let req_body = &json!({
"path": path,
});
let body = Buffer::from(Bytes::from(req_body.to_string()));
let req = Request::post(url)
.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
)
.header(header::RANGE, range.to_header())
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(new_request_build_error)?;
self.client.fetch(req).await
}
pub async fn object_info(&self, path: &str) -> Result<Response<Buffer>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/file",
self.bucket_id
);
let req_body = &json!({
"path": path,
});
let body = Buffer::from(Bytes::from(req_body.to_string()));
let req = Request::post(url)
.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
)
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(new_request_build_error)?;
self.send(req).await
}
pub async fn delete_object(&self, path: &str) -> Result<Response<Buffer>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/rm",
self.bucket_id
);
let req_body = &json!({
"paths": vec![path],
});
let body = Buffer::from(Bytes::from(req_body.to_string()));
let req = Request::post(url)
.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
)
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(new_request_build_error)?;
self.send(req).await
}
pub async fn upload_object(&self, path: &str, bs: Buffer) -> Result<Response<Buffer>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/upload",
self.bucket_id
);
let file_part = FormDataPart::new("file").content(bs);
let multipart = Multipart::new()
.part(file_part)
.part(FormDataPart::new("path").content(path));
let req = Request::post(url).header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
);
let req = multipart.apply(req)?;
self.send(req).await
}
pub async fn list_objects(&self, path: &str) -> Result<Response<Buffer>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/ls",
self.bucket_id
);
let req_body = &json!({
"path": path,
});
let body = Buffer::from(Bytes::from(req_body.to_string()));
let req = Request::post(url)
.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
)
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(new_request_build_error)?;
self.send(req).await
}
pub async fn create_dir(&self, path: &str) -> Result<Response<Buffer>> {
let path = build_abs_path(&self.root, path);
let url = format!(
"https://api.chainsafe.io/api/v1/bucket/{}/mkdir",
self.bucket_id
);
let req_body = &json!({
"path": path,
});
let body = Buffer::from(Bytes::from(req_body.to_string()));
let req = Request::post(url)
.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.api_key)?,
)
.header(header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(new_request_build_error)?;
self.send(req).await
}
}
#[derive(Debug, Deserialize)]
pub struct Info {
pub name: String,
pub content_type: String,
pub size: u64,
}
#[derive(Deserialize)]
pub struct ObjectInfoResponse {
pub content: Info,
}
pub(super) fn parse_info(info: Info) -> Metadata {
let mode = if info.content_type == "application/chainsafe-files-directory" {
EntryMode::DIR
} else {
EntryMode::FILE
};
let mut md = Metadata::new(mode);
md.set_content_length(info.size)
.set_content_type(&info.content_type);
md
}