diff --git a/src/endpoints.rs b/src/endpoints.rs index 052daf5..55dc0d5 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,3 +1,5 @@ +use anyhow::Ok; + use crate::types; pub struct Client { @@ -42,7 +44,9 @@ impl Client { .map_err(Into::into) } - pub async fn get_upload_token(&self) -> anyhow::Result { + pub async fn get_upload_token( + &self, + ) -> anyhow::Result { let client = reqwest::Client::new(); let request = client .get(&format!( @@ -127,6 +131,28 @@ impl Client { } } +pub async fn refresh_token( + req: types::request::RefreshTokenRequest, +) -> anyhow::Result { + let client = reqwest::Client::new(); + let request = client + .post("https://www.rakuten-drive.com/api/account/refreshtoken") + .json(&req); + + let response = request.send().await?; + let text = response.text().await?; + + // println!("{}", text); + + let json: types::response::RefreshTokenResponse = serde_json::from_str(&text)?; + Ok(json) + + // response + // .json::() + // .await + // .map_err(Into::into) +} + // https://www.rakuten-drive.com/api/account/refreshtoken POST RefreshTokenRequest RefreshTokenResponse // https://forest.sendy.jp/cloud/service/file/v1/file POST FileDetailRequest FileDetailResponse // https://forest.sendy.jp/cloud/service/file/v1/files POST ListFilesRequest ListFilesResponse diff --git a/src/main.rs b/src/main.rs index 6152b90..23d375f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ mod endpoints; mod types; const BEARER_TOKEN: &str = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxNTQwYWM3MWJiOTJhYTA2OTNjODI3MTkwYWNhYmU1YjA1NWNiZWMiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoi5bm457-8IOW_l-adkSIsInBsYW4iOiJza2YiLCJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vc2VuZHktc2VydmljZSIsImF1ZCI6InNlbmR5LXNlcnZpY2UiLCJhdXRoX3RpbWUiOjE3MjEyMjYwMTUsInVzZXJfaWQiOiJHY2xUN0RybkxGaG83dm5JaXJVemp0TUxoUmsyIiwic3ViIjoiR2NsVDdEcm5MRmhvN3ZuSWlyVXpqdE1MaFJrMiIsImlhdCI6MTcyMTI2MzA4NCwiZXhwIjoxNzIxMjY2Njg0LCJlbWFpbCI6ImtvdXN1a2UxMTIzNjEyNEBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsia291c3VrZTExMjM2MTI0QGdtYWlsLmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6ImN1c3RvbSJ9fQ.F7gbJ41DOBk6lmOEYzJTYOKPOn0xVleQm2ZQGKGc5rVHudIjhahfkc5av5LsooLA8SI_BZf70Tic6MUz5yOtmSVKDk67pYgJLPpDvWnVgfhcQz-MV4qOmZkvQRLsmsRlG5kcP0BhZSRfIt3DdMQ1FCcqrw6G0Kirvj7C5OJvPnwtqNjDgI9J1HFH71t_5Q7Mx2OHRYSjUM1jZR6bAngG-aNpJC9BpcF-1dgITIrvNGXkcWO1W0tQwwovQSMVq9on_bOq2arnvq8hj0BK7cu4yntBBNY2Mx_qhng7kNWoTFK4pd9p3GQc_kUacJ0PZIxE_63JiQuwiGJVuiSYjbu8iw"; +const REFRESH_TOKEN: &str = "AMf-vBwuDNdrMsPlvESgMqZWGCdVlBxMvrQVNvHOWb-FdDRV0Ozeq26POxH2tzy463DGlZTZpPYYhWCSi-KI0-8pSYEXtuCG_8DlVRyqwm4POeobYWkrw3dMgiEDNjFCfXIN4-k65CsizmCFbWQz6ASsi-XGAwMn_mXyFj9JirB7vyzTTr2ugbA"; const HOST_ID: &str = "GclT7DrnLFho7vnIirUzjtMLhRk2"; const CHUNK_SIZE: usize = 1024 * 1024 * 10; // 10MB const APP_VERSION: &str = "v21.11.10"; @@ -70,9 +71,16 @@ struct TargetFile { async fn main() { let args = Args::parse(); + let refresh_token_req = types::request::RefreshTokenRequest { + refresh_token: REFRESH_TOKEN.to_string(), + }; + let token = endpoints::refresh_token(refresh_token_req).await.unwrap(); + // println!("{:?}", token); + let token = token.id_token; + match &args.command { Commands::List { prefix } => { - let res = list_files(Some(&prefix.clone().unwrap_or("".to_string()))) + let res = list_files(Some(&prefix.clone().unwrap_or("".to_string())), &token) .await .unwrap(); res.file.iter().for_each(|f| { @@ -148,7 +156,7 @@ async fn main() { }); } - let client = endpoints::Client::new(BEARER_TOKEN.to_string(), HOST_ID.to_string()); + let client = endpoints::Client::new(token.to_string(), HOST_ID.to_string()); let req = types::request::CheckUploadRequest { host_id: client.host_id.clone(), @@ -244,13 +252,13 @@ async fn main() { println!("Uploaded"); } Commands::Download { path, prefix } => { - let client = endpoints::Client::new(BEARER_TOKEN.to_string(), HOST_ID.to_string()); + let client = endpoints::Client::new(token.to_string(), HOST_ID.to_string()); let file_name = path.split('/').last().unwrap(); let file_path = path.split('/').collect::>()[0..path.split('/').count() - 1].join("/"); - let list = list_files(Some(&file_path)).await.unwrap(); + let list = list_files(Some(&file_path), &token).await.unwrap(); let file = list .file @@ -294,8 +302,8 @@ async fn main() { println!("Move"); } Commands::Delete { path, recursive } => { - let client = endpoints::Client::new(BEARER_TOKEN.to_string(), HOST_ID.to_string()); - let file = file_detail(path).await.unwrap(); + let client = endpoints::Client::new(token.to_string(), HOST_ID.to_string()); + let file = file_detail(path, &token).await.unwrap(); if file.is_folder && !*recursive { println!("Use --recursive option for folder delete"); return; @@ -479,8 +487,11 @@ async fn multipart_upload( Ok(()) } -async fn file_detail(path: &str) -> anyhow::Result { - let client = endpoints::Client::new(BEARER_TOKEN.to_string(), HOST_ID.to_string()); +async fn file_detail( + path: &str, + token: &str, +) -> anyhow::Result { + let client = endpoints::Client::new(token.to_string(), HOST_ID.to_string()); let req = types::request::FileDetailRequest { host_id: client.host_id.clone(), path: path.to_string(), @@ -490,8 +501,11 @@ async fn file_detail(path: &str) -> anyhow::Result) -> anyhow::Result { - let client = endpoints::Client::new(BEARER_TOKEN.to_string(), HOST_ID.to_string()); +async fn list_files( + prefix: Option<&str>, + token: &str, +) -> anyhow::Result { + let client = endpoints::Client::new(token.to_string(), HOST_ID.to_string()); let pagination_size = 40; let mut files = Vec::::new(); let req = types::request::ListFilesRequest { diff --git a/src/types/request.rs b/src/types/request.rs index 82224cc..1898595 100644 --- a/src/types/request.rs +++ b/src/types/request.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] +#[serde(rename_all = "snake_case")] pub struct RefreshTokenRequest { pub refresh_token: String, } diff --git a/src/types/response.rs b/src/types/response.rs index f4e06fe..72486a2 100644 --- a/src/types/response.rs +++ b/src/types/response.rs @@ -17,14 +17,13 @@ pub struct RefreshTokenResponse { } #[derive(Debug, Serialize, Deserialize)] - +#[serde(rename_all = "camelCase")] pub struct RefreshTokenResponseMetadata { pub last_sign_in_time: String, // Wed, 17 Jul 2024 14:20:15 GMT pub creation_time: String, // Wed, 17 Jul 2024 14:20:15 GMT } #[derive(Debug, Serialize, Deserialize)] - pub struct RefreshTokenResponseCustomClaims { pub plan: String, // skf = 50GB free }