From d76f33b68aa09fe52c721de91a15810b1815e0f6 Mon Sep 17 00:00:00 2001 From: shonya3 Date: Thu, 31 Aug 2023 19:57:02 +0300 Subject: [PATCH] app: add retry-after for poe api --- packages/app/src-tauri/lib/src/error.rs | 5 +++ packages/app/src-tauri/lib/src/poe/stash.rs | 9 +++++ packages/app/src-tauri/lib/src/poe/types.rs | 5 +-- packages/wc/src/wc/stashes/stashes-view.ts | 37 +++++++++++++++------ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/packages/app/src-tauri/lib/src/error.rs b/packages/app/src-tauri/lib/src/error.rs index 50beea94..fa002ef6 100644 --- a/packages/app/src-tauri/lib/src/error.rs +++ b/packages/app/src-tauri/lib/src/error.rs @@ -10,6 +10,7 @@ pub enum Error { DiviError(divi::error::Error), AuthError(AuthError), IoError(io::Error), + RetryAfter(String), } impl Error { @@ -20,6 +21,7 @@ impl Error { Error::DiviError(_) => "diviError", Error::AuthError(_) => "authError", Error::IoError(_) => "ioError", + Error::RetryAfter(_) => "retryAfterError", } } } @@ -32,6 +34,9 @@ impl Display for Error { Error::SerdeError(err) => err.fmt(f), Error::DiviError(err) => err.fmt(f), Error::IoError(err) => err.fmt(f), + Error::RetryAfter(secs) => { + write!(f, "You have reached the limit, retry after {secs} seconds") + } } } } diff --git a/packages/app/src-tauri/lib/src/poe/stash.rs b/packages/app/src-tauri/lib/src/poe/stash.rs index e5f1965d..46d2c494 100644 --- a/packages/app/src-tauri/lib/src/poe/stash.rs +++ b/packages/app/src-tauri/lib/src/poe/stash.rs @@ -85,6 +85,15 @@ impl StashAPI { let headers = &response.headers(); + for header in headers.iter() { + println!("{header:?}"); + } + + if let Some(s) = headers.get("retry-after") { + let s = s.to_str().unwrap().to_owned(); + return Err(Error::RetryAfter(s)); + } + if let Some(limit_account_header) = headers.get("x-rate-limit-account") { if let Some(limit_account_state_header) = headers.get("x-rate-limit-account-state") { println!( diff --git a/packages/app/src-tauri/lib/src/poe/types.rs b/packages/app/src-tauri/lib/src/poe/types.rs index 709b93e7..5a15ab58 100644 --- a/packages/app/src-tauri/lib/src/poe/types.rs +++ b/packages/app/src-tauri/lib/src/poe/types.rs @@ -30,9 +30,9 @@ pub struct Item { #[derive(Serialize, Deserialize, Clone, Debug)] pub struct TabNoItems {} -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, Default)] pub struct TabWithItems { - pub items: Vec, + pub items: Option>, #[serde(rename = "type")] pub kind: Option, } @@ -51,6 +51,7 @@ impl From for SampleData { fn from(tab: TabWithItems) -> Self { let cards: Vec = tab .items + .unwrap_or_default() .into_iter() .filter(|item| item.is_card() || item.base_type == "Fire Of Unknown Origin") .map(|item| CardNameAmount { diff --git a/packages/wc/src/wc/stashes/stashes-view.ts b/packages/wc/src/wc/stashes/stashes-view.ts index 6a25b0b2..6c4607bc 100644 --- a/packages/wc/src/wc/stashes/stashes-view.ts +++ b/packages/wc/src/wc/stashes/stashes-view.ts @@ -122,7 +122,7 @@ export class StashesViewElement extends BaseElement { -

${this.msg}

+

0, msg: true })}>${this.msg}

0, msg: true })}>${this.noStashesMessage}

0) { const chunkTabs = tabsCopy.splice(0, LOAD_AT_ONE_ITERATION); this.msg = `${new Date().toLocaleTimeString('ru')}: Loading ${chunkTabs.length} tabs data`; - await Promise.all( - chunkTabs.map(async ({ id, name }) => { - if (!this.stashLoader) { - throw new Error('No stash loader'); + try { + await Promise.all( + chunkTabs.map(async ({ id, name }) => { + if (!this.stashLoader) { + throw new Error('No stash loader'); + } + const sample = await this.stashLoader.sampleFromTab(id, league); + this.emit('sample-from-tab', { sample, league, name }); + this.selectedTabs.delete(id); + this.selectedTabs = new Map(this.selectedTabs); + }) + ); + } catch (err) { + if (typeof err === 'object' && err !== null && 'message' in err) { + if (typeof err.message === 'string') { + this.msg = err.message; } - const sample = await this.stashLoader.sampleFromTab(id, league); - this.emit('sample-from-tab', { sample, league, name }); - this.selectedTabs.delete(id); - this.selectedTabs = new Map(this.selectedTabs); - }) - ); + } + this.fetchingStash = false; + + this.countdown = 0; + this.selectedTabs = new Map(); + throw err; + } if (tabsCopy.length === 0) break; // Countdown @@ -179,6 +192,8 @@ export class StashesViewElement extends BaseElement { await new Promise(r => setTimeout(r, SLEEP_SECS * 1000)); } + console.log('here'); + this.fetchingStash = false; this.msg = ''; }