From d8a718436e25c02f57ccf6b0d8f417989703fa7c Mon Sep 17 00:00:00 2001 From: Supamiu Date: Mon, 8 Oct 2018 18:34:17 +0200 Subject: [PATCH] feat: added support for staging requests via request options --- src/model/xivapi-options.ts | 5 +++++ src/xivapi.service.ts | 36 ++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/model/xivapi-options.ts b/src/model/xivapi-options.ts index abd8586..699c985 100644 --- a/src/model/xivapi-options.ts +++ b/src/model/xivapi-options.ts @@ -37,4 +37,9 @@ export interface XivapiOptions { * A GitHub issue to add support for this to TypeScript can be found at https://github.com/Microsoft/TypeScript/issues/6579 */ tags?: string[]; + + /** + * Should this request be fired against staging environment? NOT SUITED FOR PRODUCTION !!! + */ + staging?: boolean; } diff --git a/src/xivapi.service.ts b/src/xivapi.service.ts index 3329595..8af07e8 100644 --- a/src/xivapi.service.ts +++ b/src/xivapi.service.ts @@ -21,6 +21,7 @@ export class XivapiService { * Base url of xivapi. */ public static readonly API_BASE_URL: string = 'https://xivapi.com'; + public static readonly STAGING_API_BASE_URL: string = 'https://staging.xivapi.com'; constructor(@Inject(XIVAPI_KEY) protected readonly apiKey: string, private http: HttpClient) { } @@ -33,7 +34,7 @@ export class XivapiService { * @param options The options of the request, optional. */ public get(endpoint: XivapiEndpoint, id: number, options?: XivapiRequestOptions): Observable { - return this.request(`${XivapiService.API_BASE_URL}/${endpoint}/${id}`, options); + return this.request(`/${endpoint}/${id}`, options); } /** @@ -43,7 +44,7 @@ export class XivapiService { * @param options The options of the request, optional. */ public getList(endpoint: XivapiEndpoint, options?: XivapiRequestOptions): Observable> { - return this.request>(`${XivapiService.API_BASE_URL}/${endpoint}`, options); + return this.request>(`/${endpoint}`, options); } /** @@ -62,7 +63,7 @@ export class XivapiService { if (this.apiKey !== undefined) { queryParams = queryParams.set('key', this.apiKey); } - return this.http.get(`${XivapiService.API_BASE_URL}/Search`, {params: queryParams}); + return this.http.get(`/Search`, {params: queryParams}); } /** @@ -74,14 +75,14 @@ export class XivapiService { */ public getCharacter(lodestoneId: number, options?: XivapiCharacterOptions, details?: 'Friends' | 'Achievements' | 'Gearsets' | 'Record' | 'FreeCompany'): Observable { - return this.request(`${XivapiService.API_BASE_URL}/Character/${lodestoneId}${details ? '/' + details : ''}`, options); + return this.request(`/Character/${lodestoneId}${details ? '/' + details : ''}`, options); } /** * Gets the current list of available servers. Useful for character search queries. */ public getServerList(): Observable { - return this.request(`${XivapiService.API_BASE_URL}/servers`); + return this.request(`/servers`); } /** @@ -97,7 +98,7 @@ export class XivapiService { * @param page Search or move to a specific page. */ public searchCharacter(name: string, server?: string, page?: number): Observable { - let url: string = `${XivapiService.API_BASE_URL}/character/search?name=${name}`; + let url: string = `/character/search?name=${name}`; if (server !== undefined) { url += `&server=${server}`; } @@ -114,7 +115,7 @@ export class XivapiService { * @param options Options of the request. */ public verifyCharacter(lodestoneId: number, options?: XivapiCharacterOptions): Observable { - return this.request(`${XivapiService.API_BASE_URL}/Character/${lodestoneId}/Verification`, options); + return this.request(`/Character/${lodestoneId}/Verification`, options); } @@ -127,7 +128,7 @@ export class XivapiService { */ public getFreeCompany(lodestoneId: number, options?: XivapiOptions, details?: 'members' | 'record'): Observable { - return this.request(`${XivapiService.API_BASE_URL}/FreeCompany/${lodestoneId}${details ? '/' + details : ''}`, options); + return this.request(`/FreeCompany/${lodestoneId}${details ? '/' + details : ''}`, options); } /** @@ -139,29 +140,36 @@ export class XivapiService { */ public getLinkshell(lodestoneId: number, options?: XivapiOptions, details?: 'record'): Observable { - return this.request(`${XivapiService.API_BASE_URL}/Linkshell/${lodestoneId}${details ? '/' + details : ''}`, options); + return this.request(`/Linkshell/${lodestoneId}${details ? '/' + details : ''}`, options); } /** * Gets a PvP team based on its lodestone id (string) * * @param id the id of the team to get. + * @param options Options of the request */ - public getPvPTeam(id: string): Observable { - return this.request(`${XivapiService.API_BASE_URL}/PvPTeam/${id}`); + public getPvPTeam(id: string, options?: XivapiOptions): Observable { + return this.request(`/PvPTeam/${id}`, options); } /** * Gets the list of patches using the /PatchList endpoint. * @param options Options of the request. */ - public getPatchList(options: XivapiOptions): Observable { - return this.request(`${XivapiService.API_BASE_URL}/PatchList`, options); + public getPatchList(options?: XivapiOptions): Observable { + return this.request(`/PatchList`, options); } protected request(endpoint: string, params?: XivapiOptions): Observable { const queryParams: HttpParams = this.prepareQueryString(params); - return this.http.get(endpoint, {params: queryParams}); + let baseUrl: string; + if (params !== undefined) { + baseUrl = params.staging ? XivapiService.STAGING_API_BASE_URL : XivapiService.API_BASE_URL; + } else { + baseUrl = XivapiService.API_BASE_URL; + } + return this.http.get(`${baseUrl}${endpoint}`, {params: queryParams}); } private prepareQueryString(options?: XivapiOptions): HttpParams {