Skip to content

Commit

Permalink
0.6.20
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyusung4698 committed Apr 10, 2020
1 parent eef6e84 commit fe5ef75
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
15 changes: 14 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app, BrowserWindow, dialog, Display, ipcMain, Menu, MenuItem, MenuItemConstructorOptions, screen, systemPreferences, Tray } from 'electron';
import { app, BrowserWindow, dialog, Display, ipcMain, Menu, MenuItem, MenuItemConstructorOptions, screen, session, systemPreferences, Tray } from 'electron';
import * as path from 'path';
import * as url from 'url';
import UserAgent from 'user-agents';
import * as launch from './electron/auto-launch';
import * as update from './electron/auto-updater';
import * as game from './electron/game';
Expand Down Expand Up @@ -49,6 +50,17 @@ const childs: {
[key: string]: BrowserWindow
} = {};

/* session */

function setUserAgent() {
const userAgent = new UserAgent();
const generatedUserAgent = userAgent.random().toString();
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
details.requestHeaders['User-Agent'] = generatedUserAgent;
callback({ cancel: false, requestHeaders: details.requestHeaders });
});
}

/* helper */

function getDisplay(): Display {
Expand Down Expand Up @@ -314,6 +326,7 @@ try {
createWindow();
createTray();
}, 300);
setUserAgent();
});

app.on('window-all-closed', () => {
Expand Down
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"robotjs": "git+https://github.com/Kyusung4698/robotjs.git#a219829c83ff7f3a802c98a9c52a672ca798d80e",
"rxjs": "~6.5.4",
"tslib": "^1.11.1",
"user-agents": "^1.0.559",
"zone.js": "^0.10.3"
},
"resolutions": {
Expand Down
29 changes: 20 additions & 9 deletions src/app/data/poe/service/trade-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { HttpClient, HttpErrorResponse, HttpParams, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BrowserService, SessionService } from '@app/service';
import { BrowserService, LoggerService, SessionService } from '@app/service';
import { environment } from '@env/environment';
import { Language } from '@shared/module/poe/type';
import { Observable, of, throwError } from 'rxjs';
import { delay, flatMap, map, retryWhen } from 'rxjs/operators';
import { TradeFetchResult, TradeItemsResult, TradeLeaguesResult, TradeResponse, TradeSearchRequest, TradeSearchResponse, TradeStaticResult, TradeStatsResult } from '../schema/trade';

const RETRY_COUNT = 3;
const RETRY_DELAY = 100;
const RETRY_LIMIT_DELAY = 300;
const RETRY_LIMIT_DELAY_FACTOR = [1, 2, 4];
const RETRY_DELAY = 300;
const RETRY_LIMIT_COUNT = 1;
const RETRY_LIMIT_DELAY = 2000;

@Injectable({
providedIn: 'root'
Expand All @@ -19,7 +19,8 @@ export class TradeHttpService {
constructor(
private readonly http: HttpClient,
private readonly browser: BrowserService,
private readonly session: SessionService) { }
private readonly session: SessionService,
private readonly logger: LoggerService) { }

public getItems(language: Language): Observable<TradeResponse<TradeItemsResult>> {
const url = this.getApiUrl('data/items', language);
Expand All @@ -43,7 +44,9 @@ export class TradeHttpService {

public search(request: TradeSearchRequest, language: Language, leagueId: string): Observable<TradeSearchResponse> {
const url = this.getApiUrl(`search/${encodeURIComponent(leagueId)}`, language);
return this.http.post<TradeSearchResponse>(url, request).pipe(
return this.http.post<TradeSearchResponse>(url, request, {
withCredentials: true
}).pipe(
retryWhen(errors => errors.pipe(
flatMap((response, count) => this.handleError(url, response, count))
)),
Expand All @@ -61,7 +64,8 @@ export class TradeHttpService {
fromObject: {
query: queryId
}
})
}),
withCredentials: true
}).pipe(
retryWhen(errors => errors.pipe(
flatMap((response, count) => this.handleError(url, response, count))
Expand All @@ -72,7 +76,8 @@ export class TradeHttpService {
private getAndTransform<TResponse>(url: string): Observable<TResponse> {
return this.http.get(url, {
observe: 'response',
responseType: 'text'
responseType: 'text',
withCredentials: true
}).pipe(
retryWhen(errors => errors.pipe(
flatMap((response, count) => this.handleError(url, response, count))
Expand Down Expand Up @@ -143,9 +148,15 @@ export class TradeHttpService {
return throwError(response.error);
}
case 403:
if (count >= RETRY_LIMIT_COUNT) {
return throwError(response);
}
return this.browser.retrieve(url).pipe(delay(RETRY_DELAY));
case 429:
return of(null).pipe(delay(RETRY_LIMIT_DELAY * RETRY_LIMIT_DELAY_FACTOR[count]));
if (count >= RETRY_LIMIT_COUNT) {
return throwError(response);
}
return of(null).pipe(delay(RETRY_LIMIT_DELAY));
default:
return this.session.clear().pipe(delay(RETRY_DELAY));
}
Expand Down

0 comments on commit fe5ef75

Please sign in to comment.