conditionally add consistency level
Signed-off-by: Guilherme Oenning <goenning@eshopworld.com>
This commit is contained in:
@@ -106,7 +106,14 @@ export class MicrosoftGraphClient {
|
||||
path: string,
|
||||
query?: ODataQuery,
|
||||
): AsyncIterable<T> {
|
||||
let response = await this.requestApi(path, query);
|
||||
|
||||
const headers: Record<string, string> = query?.search ? {
|
||||
// Eventual consistency is required to use $search.
|
||||
// If a new user/group is not found, it'll eventually be imported on a subsequent read
|
||||
ConsistencyLevel: 'eventual',
|
||||
} : {}
|
||||
|
||||
let response = await this.requestApi(path, query, headers);
|
||||
|
||||
for (;;) {
|
||||
if (response.status !== 200) {
|
||||
@@ -125,7 +132,7 @@ export class MicrosoftGraphClient {
|
||||
return;
|
||||
}
|
||||
|
||||
response = await this.requestRaw(result['@odata.nextLink']);
|
||||
response = await this.requestRaw(result['@odata.nextLink'], headers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,8 +142,9 @@ export class MicrosoftGraphClient {
|
||||
* @public
|
||||
* @param path - Resource in Microsoft Graph
|
||||
* @param query - OData Query {@link ODataQuery}
|
||||
* @param headers - optional HTTP headers
|
||||
*/
|
||||
async requestApi(path: string, query?: ODataQuery): Promise<Response> {
|
||||
async requestApi(path: string, query?: ODataQuery, headers?: Record<string, string>): Promise<Response> {
|
||||
const queryString = qs.stringify(
|
||||
{
|
||||
$search: query?.search,
|
||||
@@ -151,15 +159,16 @@ export class MicrosoftGraphClient {
|
||||
},
|
||||
);
|
||||
|
||||
return await this.requestRaw(`${this.baseUrl}/${path}${queryString}`);
|
||||
return await this.requestRaw(`${this.baseUrl}/${path}${queryString}`, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a HTTP call to Graph API with token
|
||||
*
|
||||
* @param url - HTTP Endpoint of Graph API
|
||||
* @param headers - optional HTTP headers
|
||||
*/
|
||||
async requestRaw(url: string): Promise<Response> {
|
||||
async requestRaw(url: string, headers?: Record<string, string>): Promise<Response> {
|
||||
// Make sure that we always have a valid access token (might be cached)
|
||||
const token = await this.pca.acquireTokenByClientCredential({
|
||||
scopes: ['https://graph.microsoft.com/.default'],
|
||||
@@ -171,12 +180,8 @@ export class MicrosoftGraphClient {
|
||||
|
||||
return await fetch(url, {
|
||||
headers: {
|
||||
...headers,
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
|
||||
// Eventual consistency is required to use $search.
|
||||
// Groups/Users are not changed that frequently to require strong consistency
|
||||
// If a new user/group is not found, it'll eventually be imported on a subsequent call
|
||||
ConsistencyLevel: 'eventual',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user