|
| 1 | +use mime_guess::from_path; |
| 2 | +use reqwest; |
| 3 | +use serde::de::DeserializeOwned; |
| 4 | +use snafu::{ResultExt, Snafu}; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use tokio::fs::File; |
| 7 | +use tokio_util::codec::{BytesCodec, FramedRead}; |
| 8 | +use url::{ParseError, Url}; |
| 9 | +use walkdir::WalkDir; |
| 10 | + |
| 11 | +use std::io; |
| 12 | +use std::sync::LazyLock; |
| 13 | + |
| 14 | +pub struct ZenodoClient { |
| 15 | + http_client: reqwest::Client, |
| 16 | + base_url: Url, |
| 17 | + token: String, |
| 18 | +} |
| 19 | + |
| 20 | +static BASE_URL: LazyLock<Url> = |
| 21 | + LazyLock::new(|| Url::parse("https://zenodo.org").expect("Invalid Base URL config")); |
| 22 | + |
| 23 | +#[derive(Debug, Snafu)] |
| 24 | +pub enum Error { |
| 25 | + #[snafu(display("A zendoo client error occured: {}", source))] |
| 26 | + Reqwest { source: reqwest::Error }, |
| 27 | + #[snafu(display("A directory listing error occured: {}", source))] |
| 28 | + DirWalk { source: walkdir::Error }, |
| 29 | + #[snafu(display("An error occured reading the response: {}", source))] |
| 30 | + DeserializeResp { source: reqwest::Error }, |
| 31 | + #[snafu(display("An error occured reading the response: {}", source))] |
| 32 | + FileReading { source: io::Error }, |
| 33 | + #[snafu(display("An error occured parsing the file path: {}", fp.to_string_lossy()))] |
| 34 | + FileParsing { fp: PathBuf }, |
| 35 | + #[snafu(display("An error occured parsing the url {}", source))] |
| 36 | + UrlParse { source: ParseError }, |
| 37 | +} |
| 38 | + |
| 39 | +impl ZenodoClient { |
| 40 | + pub fn new(token: String) -> ZenodoClient { |
| 41 | + let http_client = reqwest::Client::new(); |
| 42 | + ZenodoClient { |
| 43 | + http_client, |
| 44 | + base_url: BASE_URL.clone(), |
| 45 | + token, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + fn make_url(&self, path: &str) -> Result<Url, Error> { |
| 50 | + self.base_url.join(path).context(UrlParseSnafu) |
| 51 | + } |
| 52 | + |
| 53 | + pub async fn get_deposition<R: DeserializeOwned>( |
| 54 | + &self, |
| 55 | + deposition_id: &str, |
| 56 | + ) -> Result<R, Error> { |
| 57 | + let endpoint = self.make_url(&format!("/api/deposit/depositions/{deposition_id}"))?; |
| 58 | + let res = self |
| 59 | + .http_client |
| 60 | + .get(endpoint) |
| 61 | + .bearer_auth(&self.token) |
| 62 | + .send() |
| 63 | + .await |
| 64 | + .context(ReqwestSnafu)?; |
| 65 | + // res.error_for_status().context(ReqwestSnafu)?; |
| 66 | + res.json::<R>().await.context(DeserializeRespSnafu) |
| 67 | + } |
| 68 | + |
| 69 | + pub async fn upload_files(&self, deposition_id: &str, source_path: &Path) -> Result<(), Error> { |
| 70 | + for f in WalkDir::new(source_path) { |
| 71 | + self.upload_file::<()>(deposition_id, f.context(DirWalkSnafu)?.path()) |
| 72 | + .await?; |
| 73 | + } |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + |
| 77 | + pub async fn list_files<R: DeserializeOwned>(&self, deposition_id: &str) -> Result<R, Error> { |
| 78 | + let endpoint = self.make_url(&format!("/api/deposit/depositions/{deposition_id}/files"))?; |
| 79 | + let res = self |
| 80 | + .http_client |
| 81 | + .get(endpoint) |
| 82 | + .bearer_auth(&self.token) |
| 83 | + .send() |
| 84 | + .await |
| 85 | + .context(ReqwestSnafu)?; |
| 86 | + res.json::<R>().await.context(DeserializeRespSnafu) |
| 87 | + } |
| 88 | + |
| 89 | + async fn upload_file<R: DeserializeOwned>( |
| 90 | + &self, |
| 91 | + deposition_id: &str, |
| 92 | + file_path: &Path, |
| 93 | + ) -> Result<R, Error> { |
| 94 | + let file = File::open(file_path).await.context(FileReadingSnafu)?; |
| 95 | + let endpoint = self.make_url(&format!("/api/deposit/depositions/{deposition_id}/files"))?; |
| 96 | + let stream = FramedRead::new(file, BytesCodec::new()); |
| 97 | + let file_name = file_path |
| 98 | + .file_name() |
| 99 | + .ok_or(Error::FileParsing { |
| 100 | + fp: file_path.to_path_buf(), |
| 101 | + })? |
| 102 | + .to_str() |
| 103 | + .ok_or(Error::FileParsing { |
| 104 | + fp: file_path.to_path_buf(), |
| 105 | + })?; |
| 106 | + let mime_type = from_path(file_path).first_or_octet_stream(); |
| 107 | + let form = reqwest::multipart::Form::new() |
| 108 | + .text("name", file_name.to_owned()) |
| 109 | + .part( |
| 110 | + "file", |
| 111 | + reqwest::multipart::Part::stream(reqwest::Body::wrap_stream(stream)) |
| 112 | + .file_name(file_name.to_owned()) |
| 113 | + .mime_str(mime_type.as_ref()) |
| 114 | + .context(ReqwestSnafu)?, |
| 115 | + ); |
| 116 | + let res = self |
| 117 | + .http_client |
| 118 | + .post(endpoint) |
| 119 | + .bearer_auth(&self.token) |
| 120 | + .multipart(form) |
| 121 | + .send() |
| 122 | + .await |
| 123 | + .context(ReqwestSnafu)?; |
| 124 | + res.json::<R>().await.context(DeserializeRespSnafu) |
| 125 | + } |
| 126 | +} |
0 commit comments