Skip to content

Commit

Permalink
app: add retry-after for poe api
Browse files Browse the repository at this point in the history
  • Loading branch information
shonya3 committed Aug 31, 2023
1 parent 9d27ba2 commit d76f33b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/app/src-tauri/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Error {
DiviError(divi::error::Error),
AuthError(AuthError),
IoError(io::Error),
RetryAfter(String),
}

impl Error {
Expand All @@ -20,6 +21,7 @@ impl Error {
Error::DiviError(_) => "diviError",
Error::AuthError(_) => "authError",
Error::IoError(_) => "ioError",
Error::RetryAfter(_) => "retryAfterError",
}
}
}
Expand All @@ -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")
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/app/src-tauri/lib/src/poe/stash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src-tauri/lib/src/poe/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item>,
pub items: Option<Vec<Item>>,
#[serde(rename = "type")]
pub kind: Option<StashType>,
}
Expand All @@ -51,6 +51,7 @@ impl From<TabWithItems> for SampleData {
fn from(tab: TabWithItems) -> Self {
let cards: Vec<CardNameAmount> = tab
.items
.unwrap_or_default()
.into_iter()
.filter(|item| item.is_card() || item.base_type == "Fire Of Unknown Origin")
.map(|item| CardNameAmount {
Expand Down
37 changes: 26 additions & 11 deletions packages/wc/src/wc/stashes/stashes-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class StashesViewElement extends BaseElement {
<button @click=${this.#onCloseClicked} class="btn-close">Close</button>
</div>
<p class=${classMap({ visible: this.fetchingStash, msg: true })}>${this.msg}</p>
<p class=${classMap({ visible: this.msg.length > 0, msg: true })}>${this.msg}</p>
<p class=${classMap({ visible: this.noStashesMessage.length > 0, msg: true })}>${this.noStashesMessage}</p>
<wc-tab-badge-group
Expand All @@ -144,17 +144,30 @@ export class StashesViewElement extends BaseElement {
while (tabsCopy.length > 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<Events['sample-from-tab']>('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<Events['sample-from-tab']>('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
Expand All @@ -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 = '';
}
Expand Down

0 comments on commit d76f33b

Please sign in to comment.