Merge pull request #281 from AppFlowy-IO/client-log
feat: add client timestamp to header
This commit is contained in:
commit
d85dcfeb14
|
|
@ -44,21 +44,6 @@ dependencies = [
|
||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "actix-cors"
|
|
||||||
version = "0.6.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370"
|
|
||||||
dependencies = [
|
|
||||||
"actix-utils",
|
|
||||||
"actix-web",
|
|
||||||
"derive_more",
|
|
||||||
"futures-util",
|
|
||||||
"log",
|
|
||||||
"once_cell",
|
|
||||||
"smallvec",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "3.5.1"
|
version = "3.5.1"
|
||||||
|
|
@ -513,7 +498,6 @@ name = "appflowy-cloud"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix",
|
"actix",
|
||||||
"actix-cors",
|
|
||||||
"actix-http",
|
"actix-http",
|
||||||
"actix-identity",
|
"actix-identity",
|
||||||
"actix-router",
|
"actix-router",
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ actix-rt = "2"
|
||||||
actix-web-actors = { version = "4.2.0" }
|
actix-web-actors = { version = "4.2.0" }
|
||||||
actix-service = "2.0.2"
|
actix-service = "2.0.2"
|
||||||
actix-identity = "0.6.0"
|
actix-identity = "0.6.0"
|
||||||
actix-cors = "0.6.5"
|
|
||||||
actix-router = "0.5.2"
|
actix-router = "0.5.2"
|
||||||
actix-session = { version = "0.8", features = ["redis-rs-tls-session"] }
|
actix-session = { version = "0.8", features = ["redis-rs-tls-session"] }
|
||||||
openssl = { version = "0.10.62", features = ["vendored"] }
|
openssl = { version = "0.10.62", features = ["vendored"] }
|
||||||
|
|
@ -134,7 +133,6 @@ shared-entity = { path = "libs/shared-entity" }
|
||||||
app-error = { path = "libs/app_error" }
|
app-error = { path = "libs/app_error" }
|
||||||
serde_json = "1.0.111"
|
serde_json = "1.0.111"
|
||||||
serde = { version = "1.0.195", features = ["derive"] }
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
serde_repr = "0.1.18"
|
|
||||||
bytes = "1.5.0"
|
bytes = "1.5.0"
|
||||||
workspace-template = { path = "libs/workspace-template" }
|
workspace-template = { path = "libs/workspace-template" }
|
||||||
uuid = { version = "1.6.1", features = ["v4"] }
|
uuid = { version = "1.6.1", features = ["v4"] }
|
||||||
|
|
@ -160,9 +158,6 @@ codegen-units = 16
|
||||||
debug = true
|
debug = true
|
||||||
lto = false
|
lto = false
|
||||||
|
|
||||||
[profile.dev.package.sqlx-macros]
|
|
||||||
opt-level = 3
|
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
|
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
|
||||||
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
|
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
|
||||||
|
|
|
||||||
|
|
@ -987,7 +987,9 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ws_url(&self, device_id: &str) -> Result<String, AppResponseError> {
|
pub async fn ws_url(&self, device_id: &str) -> Result<String, AppResponseError> {
|
||||||
self.refresh_if_required().await?;
|
self
|
||||||
|
.refresh_if_expired(chrono::Local::now().timestamp())
|
||||||
|
.await?;
|
||||||
|
|
||||||
let access_token = self.access_token()?;
|
let access_token = self.access_token()?;
|
||||||
Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id))
|
Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id))
|
||||||
|
|
@ -1151,13 +1153,12 @@ impl Client {
|
||||||
.into_data()
|
.into_data()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refresh_if_required(&self) -> Result<(), AppResponseError> {
|
// Refresh token if given timestamp is close to the token expiration time
|
||||||
|
pub async fn refresh_if_expired(&self, ts: i64) -> Result<(), AppResponseError> {
|
||||||
let expires_at = self.token_expires_at()?;
|
let expires_at = self.token_expires_at()?;
|
||||||
|
|
||||||
// Refresh token if it's about to expire
|
if ts + 30 > expires_at {
|
||||||
let time_now_sec = chrono::Local::now().timestamp();
|
// Add 30 seconds buffer
|
||||||
if time_now_sec + 10 > expires_at {
|
|
||||||
// Add 10 seconds buffer
|
|
||||||
self.refresh_token().await?;
|
self.refresh_token().await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -1169,7 +1170,8 @@ impl Client {
|
||||||
method: Method,
|
method: Method,
|
||||||
url: &str,
|
url: &str,
|
||||||
) -> Result<RequestBuilder, AppResponseError> {
|
) -> Result<RequestBuilder, AppResponseError> {
|
||||||
self.refresh_if_required().await?;
|
let ts_now = chrono::Local::now().timestamp();
|
||||||
|
self.refresh_if_expired(ts_now).await?;
|
||||||
|
|
||||||
let access_token = self.access_token()?;
|
let access_token = self.access_token()?;
|
||||||
trace!("start request: {}, method: {}", url, method);
|
trace!("start request: {}, method: {}", url, method);
|
||||||
|
|
@ -1177,6 +1179,7 @@ impl Client {
|
||||||
.cloud_client
|
.cloud_client
|
||||||
.request(method, url)
|
.request(method, url)
|
||||||
.header("client-version", CLIENT_API_VERSION)
|
.header("client-version", CLIENT_API_VERSION)
|
||||||
|
.header("client-timestamp", ts_now.to_string())
|
||||||
.bearer_auth(access_token);
|
.bearer_auth(access_token);
|
||||||
Ok(request_builder)
|
Ok(request_builder)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue