-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* graph client authentication and service calls * Microsoft Graph Client Service Added * rolling back deleted space * scope constants added in api call * telemetry error log changed to hard coded string --------- Co-authored-by: tyaginidhi <[email protected]>
- Loading branch information
1 parent
1d40111
commit 9a0c7e7
Showing
5 changed files
with
238 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import path from "path"; | ||
import WebExtensionContext from "../WebExtensionContext"; | ||
import { getCommonHeaders, graphClientAuthentication } from "../common/authenticationProvider"; | ||
import * as Constants from "../common/constants"; | ||
import { telemetryEventNames } from "../telemetry/constants"; | ||
|
||
export class GraphClientService { | ||
private _graphToken: string; | ||
|
||
constructor() { | ||
this._graphToken = ""; | ||
} | ||
|
||
get graphToken() { | ||
return this._graphToken; | ||
} | ||
|
||
public async graphClientAuthentication(firstTimeAuth = false) { | ||
const accessToken = await graphClientAuthentication(firstTimeAuth); | ||
this._graphToken = accessToken; | ||
} | ||
|
||
private async requestGraphClient( | ||
service: string, | ||
userId: string | ||
) { | ||
let requestSentAtTime = new Date().getTime(); | ||
|
||
const basePath = Constants.MICROSOFT_GRAPH_USERS_BASE_URL; | ||
let requestUrl; | ||
|
||
switch (service) { | ||
case Constants.GraphService.GRAPH_MAIL: | ||
requestUrl = new URL(userId, basePath); | ||
break; | ||
case Constants.GraphService.GRAPH_PROFILE_PICTURE: | ||
requestUrl = new URL( | ||
path.join( | ||
userId, | ||
Constants.MICROSOFT_GRAPH_PROFILE_PICTURE_SERVICE_CALL | ||
), | ||
basePath | ||
); | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
try { | ||
WebExtensionContext.telemetry.sendAPITelemetry( | ||
requestUrl.href, | ||
service, | ||
Constants.httpMethod.GET, | ||
this.requestGraphClient.name | ||
); | ||
|
||
requestSentAtTime = new Date().getTime(); | ||
|
||
const response = | ||
await WebExtensionContext.concurrencyHandler.handleRequest( | ||
requestUrl.href, | ||
{ | ||
headers: { | ||
...getCommonHeaders(this._graphToken), | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(JSON.stringify(response)); | ||
} | ||
|
||
WebExtensionContext.telemetry.sendAPISuccessTelemetry( | ||
requestUrl.href, | ||
service, | ||
Constants.httpMethod.POST, | ||
new Date().getTime() - requestSentAtTime, | ||
this.requestGraphClient.name | ||
); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
const errorMsg = (error as Error)?.message; | ||
if ((error as Response)?.status > 0) { | ||
WebExtensionContext.telemetry.sendAPIFailureTelemetry( | ||
requestUrl.href, | ||
service, | ||
Constants.httpMethod.GET, | ||
new Date().getTime() - requestSentAtTime, | ||
this.requestGraphClient.name, | ||
errorMsg, | ||
"", | ||
(error as Response)?.status.toString() | ||
); | ||
} else { | ||
WebExtensionContext.telemetry.sendErrorTelemetry( | ||
telemetryEventNames.WEB_EXTENSION_GET_FROM_GRAPH_CLIENT_FAILED, | ||
this.requestGraphClient.name, | ||
Constants.WEB_EXTENSION_GET_FROM_GRAPH_CLIENT_FAILED, | ||
error as Error | ||
); | ||
} | ||
} | ||
} | ||
|
||
public async getUserEmail(userId: string) { | ||
try { | ||
if (!this._graphToken) { | ||
await this.graphClientAuthentication(true); | ||
} | ||
|
||
const response = await this.requestGraphClient( | ||
Constants.GraphService.GRAPH_MAIL, | ||
userId | ||
); | ||
return await response.mail; | ||
} catch (error) { | ||
WebExtensionContext.telemetry.sendErrorTelemetry( | ||
telemetryEventNames.WEB_EXTENSION_GET_EMAIL_FROM_GRAPH_CLIENT_FAILED, | ||
this.getUserEmail.name, | ||
(error as Error)?.message, | ||
error as Error | ||
); | ||
} | ||
} | ||
|
||
public async getUserProfilePicture(userId: string) { | ||
try { | ||
if (!this._graphToken) { | ||
await this.graphClientAuthentication(true); | ||
} | ||
|
||
const response = await this.requestGraphClient( | ||
Constants.GraphService.GRAPH_PROFILE_PICTURE, | ||
userId | ||
); | ||
return await response; | ||
} catch (error) { | ||
WebExtensionContext.telemetry.sendErrorTelemetry( | ||
telemetryEventNames.WEB_EXTENSION_GET_PROFILE_PICTURE_FROM_GRAPH_CLIENT_FAILED, | ||
this.getUserProfilePicture.name, | ||
(error as Error)?.message, | ||
error as Error | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters