diff --git a/rust-libs/ease-remote-storage/src/impls/onedrive.rs b/rust-libs/ease-remote-storage/src/impls/onedrive.rs index c1ae373..d79e0a0 100644 --- a/rust-libs/ease-remote-storage/src/impls/onedrive.rs +++ b/rust-libs/ease-remote-storage/src/impls/onedrive.rs @@ -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) -> StorageBackendResult<()> { let client_id = EASEM_ONEDRIVE_ID; let refresh_token = self.refresh_token.clone(); let body = @@ -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 { let subdir = if dir == "/" { "/root/children".to_string() @@ -228,6 +241,7 @@ impl OneDriveBackend { } async fn list_with_retry_impl(&self, dir: String) -> StorageBackendResult> { + 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; @@ -253,6 +267,7 @@ impl OneDriveBackend { } async fn get_with_retry_impl(&self, p: String) -> StorageBackendResult { + 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;