From 9f2d9c0a91d470a693502e6e74d7154fc84581f5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 7 Oct 2019 10:02:18 -0400 Subject: [PATCH] allow custom user agent string --- src/services/api.service.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/services/api.service.ts b/src/services/api.service.ts index eb431926eae..d62f2a2abc4 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -117,7 +117,7 @@ export class ApiService implements ApiServiceAbstraction { private usingBaseUrl = false; constructor(private tokenService: TokenService, private platformUtilsService: PlatformUtilsService, - private logoutCallback: (expired: boolean) => Promise) { + private logoutCallback: (expired: boolean) => Promise, private customUserAgent: string = null) { this.device = platformUtilsService.getDevice(); this.deviceType = this.device.toString(); this.isWebClient = this.device === DeviceType.IEBrowser || this.device === DeviceType.ChromeBrowser || @@ -158,15 +158,19 @@ export class ApiService implements ApiServiceAbstraction { // Auth APIs async postIdentityToken(request: TokenRequest): Promise { + const headers = new Headers({ + 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', + 'Accept': 'application/json', + 'Device-Type': this.deviceType, + }); + if (this.customUserAgent != null) { + headers.set('User-Agent', this.customUserAgent); + } const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', { body: this.qsStringify(request.toIdentityToken(this.platformUtilsService.identityClientId)), credentials: this.getCredentials(), cache: 'no-cache', - headers: new Headers({ - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', - 'Accept': 'application/json', - 'Device-Type': this.deviceType, - }), + headers: headers, method: 'POST', })); @@ -860,6 +864,9 @@ export class ApiService implements ApiServiceAbstraction { 'Authorization': 'Bearer ' + authHeader, 'Content-Type': 'application/json; charset=utf-8', }); + if (this.customUserAgent != null) { + headers.set('User-Agent', this.customUserAgent); + } const response = await this.fetch(new Request(this.eventsBaseUrl + '/collect', { cache: 'no-cache', credentials: this.getCredentials(), @@ -926,6 +933,9 @@ export class ApiService implements ApiServiceAbstraction { const headers = new Headers({ 'Device-Type': this.deviceType, }); + if (this.customUserAgent != null) { + headers.set('User-Agent', this.customUserAgent); + } const requestInit: RequestInit = { cache: 'no-cache', @@ -985,6 +995,14 @@ export class ApiService implements ApiServiceAbstraction { if (refreshToken == null || refreshToken === '') { throw new Error(); } + const headers = new Headers({ + 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', + 'Accept': 'application/json', + 'Device-Type': this.deviceType, + }); + if (this.customUserAgent != null) { + headers.set('User-Agent', this.customUserAgent); + } const decodedToken = this.tokenService.decodeToken(); const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', { @@ -995,11 +1013,7 @@ export class ApiService implements ApiServiceAbstraction { }), cache: 'no-cache', credentials: this.getCredentials(), - headers: new Headers({ - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', - 'Accept': 'application/json', - 'Device-Type': this.deviceType, - }), + headers: headers, method: 'POST', }));