fetch item prices - throttle end points with promise queue

This commit is contained in:
2018-08-28 21:45:46 -05:00
parent 97acc0d4fa
commit 7ca8606284
17 changed files with 241 additions and 64 deletions

View File

@@ -1,9 +1,10 @@
import * as _ from 'lodash';
import { FrameType } from '../model/frame-type';
import { IItem } from '../model/item';
const lineBreak = '--------\n';
const parseItem = (item: any): string => {
const parseItem = (item: IItem): string => {
let name = '';
name += `Rarity: ${FrameType[item.frameType]}\n`;

View File

@@ -1,8 +1,12 @@
import PQueue from 'p-queue';
import * as _ from 'lodash';
import { POE_HOME, POE_LEAGUE_LIST_URL, POE_STASH_ITEMS_URL } from '../constants';
import { http } from '../http';
import { IItem } from '../model/item';
import { PromiseQueue } from '../util';
import { ItemTextService } from './item-text.service';
import { StorageService } from './storage.service';
const queue = new PQueue({ concurrency: 1 });
const queue = new PromiseQueue();
const getStash = async (username: string, league: string, tabIndex: number | string): Promise<any> => {
const res = await http.get(
@@ -11,32 +15,51 @@ const getStash = async (username: string, league: string, tabIndex: number | str
return res.data;
};
const priceCheck = async (item: any): Promise<any> => {
/**
* Clears the request queue. Used when switching leagues/stashes
*/
const clearRequestQueue = () => {
queue.clearQueue();
};
const priceCheck = async (item: IItem, league: string): Promise<any> => {
const itemCache = StorageService.getItemCache(item.id);
// return cached item if it exists - otherwise fetch from Poe Prices
if (itemCache) {
return Promise.resolve(itemCache);
}
return new Promise(resolve => {
queue.add(() => {
return http
.get(`https://poeprices.info/api?l=Incursion&i=${encodeURI(btoa(item))}`, {
.get(`https://poeprices.info/api?l=${league}&i=${encodeURI(btoa(ItemTextService.parseItem(item)))}`, {
headers: {
'Cache-Control': 'max-age=600',
},
})
.then(resolve);
});
.then(data => {
if (_.get(data, 'data.error') === 0) {
StorageService.storeItemCache(item.id, data.data);
}
return resolve(data.data);
});
}, 1000);
});
};
const getUsername = async (): Promise<any> => {
const res = await http.get(POE_HOME);
const getUsername = async (headers: any): Promise<any> => {
const res = await http.get(POE_HOME, { headers });
const username = res.data.match(/\/account\/view-profile\/(.*?)\"/);
return username[1];
};
const getLeagues = async (): Promise<any> => {
const res = await http.get(POE_LEAGUE_LIST_URL);
const getLeagues = async (headers: any): Promise<any> => {
const res = await http.get(POE_LEAGUE_LIST_URL, { headers });
return res.data;
};
export const PoeService = {
clearRequestQueue,
getLeagues,
getStash,
getUsername,

View File

@@ -1,4 +1,7 @@
import Store from 'electron-store';
import { DateTime } from 'luxon';
import { IItemPrice } from '../model';
import { IItem } from '../model/item';
const store = new Store();
const storeUsername = (username: string) => {
@@ -17,9 +20,30 @@ const getUsername = (): string | undefined => {
return store.get('user') || undefined;
};
const getItemCache = (id: string): IItem | undefined => {
const storedVal = store.get(id);
if (!storedVal) {
return undefined;
}
const val = JSON.parse(storedVal);
if (DateTime.fromISO(val.timestamp) < DateTime.local().plus({ minutes: 10 })) {
return val.item;
}
store.delete(id);
return undefined;
};
const storeItemCache = (id: string, item: IItemPrice) => {
const timestamp = DateTime.local();
const val = JSON.stringify({ timestamp, item });
store.set(id, val);
};
export const StorageService = {
getItemCache,
getUsername,
getSessionID,
storeItemCache,
storeUsername,
storeSessionID,
};