Skip to content

Commit

Permalink
refactor: try ensure refresh token in onedrive
Browse files Browse the repository at this point in the history
  • Loading branch information
hpp2334 committed Nov 25, 2024
1 parent c0e623d commit 3df14cd
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion rust-libs/ease-remote-storage/src/impls/onedrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ impl OneDriveBackend {
return header_map;
}

async fn refresh_token_by_refresh_token(&self) -> StorageBackendResult<()> {
async fn try_ensure_refresh_token_by_refresh_token(&self) -> StorageBackendResult<()> {
let mut w= self.auth.write().await;
if w.is_none() {
self.refresh_token_by_refresh_token_impl(&mut w).await?;
}
Ok(())
}

async fn refresh_token_by_refresh_token_impl(&self, w: &mut Option<Auth>) -> StorageBackendResult<()> {
let client_id = EASEM_ONEDRIVE_ID;
let refresh_token = self.refresh_token.clone();
let body =
Expand All @@ -159,6 +166,12 @@ impl OneDriveBackend {
Ok(())
}

async fn refresh_token_by_refresh_token(&self) -> StorageBackendResult<()> {
let mut w= self.auth.write().await;
self.refresh_token_by_refresh_token_impl(&mut w).await?;
Ok(())
}

async fn list_core(&self, dir: &str) -> StorageBackendResult<reqwest::Response> {
let subdir = if dir == "/" {
"/root/children".to_string()
Expand Down Expand Up @@ -228,6 +241,7 @@ impl OneDriveBackend {
}

async fn list_with_retry_impl(&self, dir: String) -> StorageBackendResult<Vec<Entry>> {
self.try_ensure_refresh_token_by_refresh_token().await?;
let r = self.list_impl(dir.as_str()).await;
if !is_auth_error(&r) {
return r;
Expand All @@ -253,6 +267,7 @@ impl OneDriveBackend {
}

async fn get_with_retry_impl(&self, p: String) -> StorageBackendResult<StreamFile> {
self.try_ensure_refresh_token_by_refresh_token().await?;
let r = self.get_impl(p.as_str()).await;
if !is_auth_error(&r) {
return r;
Expand Down

0 comments on commit 3df14cd

Please sign in to comment.