Ability to skip resource validation with RP calls (#73)

* Support ability to skip resource validation

* Use request options
This commit is contained in:
Vignesh Rangaishenvi 2020-07-07 14:31:19 -07:00 committed by GitHub
parent 84ea3796ec
commit a08890aadf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 35 deletions

View File

@ -83,7 +83,9 @@ export class NotebookWorkspaceManager implements ViewModels.NotebookWorkspaceMan
public async startNotebookWorkspaceAsync(cosmosdbResourceId: string, notebookWorkspaceId: string): Promise<void> {
const uri = `${cosmosdbResourceId}/notebookWorkspaces/${notebookWorkspaceId}/start`;
try {
return await this.rpClient(uri).postAsync(uri, ArmApiVersions.documentDB, undefined);
return await this.rpClient(uri).postAsync(uri, ArmApiVersions.documentDB, undefined, {
skipResourceValidation: true
});
} catch (error) {
Logger.logError(error, "NotebookWorkspaceManager/startNotebookWorkspaceAsync");
throw error;

View File

@ -1,9 +1,28 @@
export interface IResourceProviderClient<TResource> {
deleteAsync(url: string, apiVersion: string): Promise<void>;
getAsync(url: string, apiVersion: string, queryString?: string): Promise<TResource | TResource[]>;
postAsync(url: string, apiVersion: string, body: any): Promise<any>;
putAsync(url: string, apiVersion: string, body: any): Promise<TResource>;
patchAsync(url: string, apiVersion: string, body: any): Promise<TResource>;
deleteAsync(url: string, apiVersion: string, requestOptions?: IResourceProviderRequestOptions): Promise<void>;
getAsync(
url: string,
apiVersion: string,
queryString?: string,
requestOptions?: IResourceProviderRequestOptions
): Promise<TResource | TResource[]>;
postAsync(url: string, apiVersion: string, body: any, requestOptions?: IResourceProviderRequestOptions): Promise<any>;
putAsync(
url: string,
apiVersion: string,
body: any,
requestOptions?: IResourceProviderRequestOptions
): Promise<TResource>;
patchAsync(
url: string,
apiVersion: string,
body: any,
requestOptions?: IResourceProviderRequestOptions
): Promise<TResource>;
}
export interface IResourceProviderRequestOptions {
skipResourceValidation: boolean;
}
export interface IResourceProviderClientFactory<TResult> {

View File

@ -1,6 +1,6 @@
import * as ViewModels from "../Contracts/ViewModels";
import { HttpStatusCodes } from "../Common/Constants";
import { IResourceProviderClient } from "./IResourceProviderClient";
import { IResourceProviderClient, IResourceProviderRequestOptions } from "./IResourceProviderClient";
import { OperationStatus } from "../Contracts/DataModels";
import { TokenProviderFactory } from "../TokenProviders/TokenProviderFactory";
import UrlUtility from "../Common/UrlUtility";
@ -12,32 +12,74 @@ export class ResourceProviderClient<T> implements IResourceProviderClient<T> {
this.httpClient = new HttpClient();
}
public async getAsync(url: string, apiVersion: string, queryString?: string): Promise<T | T[]> {
public async getAsync(
url: string,
apiVersion: string,
queryString?: string,
requestOptions?: IResourceProviderRequestOptions
): Promise<T | T[]> {
let uri = `${this.armEndpoint}${url}?api-version=${apiVersion}`;
if (queryString) {
uri += `&${queryString}`;
}
return await this.httpClient.getAsync<T | T[]>(uri);
return await this.httpClient.getAsync<T | T[]>(
uri,
Object.assign({}, { skipResourceValidation: false }, requestOptions)
);
}
public async postAsync(url: string, apiVersion: string, body: any): Promise<any> {
public async postAsync(
url: string,
apiVersion: string,
body: any,
requestOptions?: IResourceProviderRequestOptions
): Promise<any> {
const fullUrl = UrlUtility.createUri(this.armEndpoint, url);
return await this.httpClient.postAsync(`${fullUrl}?api-version=${apiVersion}`, body);
return await this.httpClient.postAsync(
`${fullUrl}?api-version=${apiVersion}`,
body,
Object.assign({}, { skipResourceValidation: false }, requestOptions)
);
}
public async putAsync(url: string, apiVersion: string, body: any): Promise<T> {
public async putAsync(
url: string,
apiVersion: string,
body: any,
requestOptions?: IResourceProviderRequestOptions
): Promise<T> {
const fullUrl = UrlUtility.createUri(this.armEndpoint, url);
return await this.httpClient.putAsync<T>(`${fullUrl}?api-version=${apiVersion}`, body);
return await this.httpClient.putAsync<T>(
`${fullUrl}?api-version=${apiVersion}`,
body,
Object.assign({}, { skipResourceValidation: false }, requestOptions)
);
}
public async patchAsync(url: string, apiVersion: string, body: any): Promise<T> {
public async patchAsync(
url: string,
apiVersion: string,
body: any,
requestOptions?: IResourceProviderRequestOptions
): Promise<T> {
const fullUrl = UrlUtility.createUri(this.armEndpoint, url);
return await this.httpClient.patchAsync<T>(`${fullUrl}?api-version=${apiVersion}`, body);
return await this.httpClient.patchAsync<T>(
`${fullUrl}?api-version=${apiVersion}`,
body,
Object.assign({}, { skipResourceValidation: false }, requestOptions)
);
}
public async deleteAsync(url: string, apiVersion: string): Promise<void> {
public async deleteAsync(
url: string,
apiVersion: string,
requestOptions?: IResourceProviderRequestOptions
): Promise<void> {
const fullUrl = UrlUtility.createUri(this.armEndpoint, url);
return await this.httpClient.deleteAsync(`${fullUrl}?api-version=${apiVersion}`);
return await this.httpClient.deleteAsync(
`${fullUrl}?api-version=${apiVersion}`,
Object.assign({}, { skipResourceValidation: true }, requestOptions)
);
}
}
@ -55,40 +97,44 @@ class HttpClient {
this.tokenProvider = TokenProviderFactory.create();
}
public async getAsync<T>(url: string): Promise<T> {
public async getAsync<T>(url: string, requestOptions: IResourceProviderRequestOptions): Promise<T> {
const args: RequestInit = { method: "GET" };
const response = await this.httpRequest(new Request(url, args));
const response = await this.httpRequest(new Request(url, args), requestOptions);
return (await response.json()) as T;
}
public async postAsync(url: string, body: any): Promise<any> {
public async postAsync(url: string, body: any, requestOptions: IResourceProviderRequestOptions): Promise<any> {
body = typeof body !== "string" && body !== undefined ? JSON.stringify(body) : body;
const args: RequestInit = { method: "POST", headers: { "Content-Type": "application/json" }, body };
const response = await this.httpRequest(new Request(url, args));
const response = await this.httpRequest(new Request(url, args), requestOptions);
return await response.json();
}
public async putAsync<T>(url: string, body: any): Promise<T> {
public async putAsync<T>(url: string, body: any, requestOptions: IResourceProviderRequestOptions): Promise<T> {
body = typeof body !== "string" && body !== undefined ? JSON.stringify(body) : body;
const args: RequestInit = { method: "PUT", headers: { "Content-Type": "application/json" }, body };
const response = await this.httpRequest(new Request(url, args));
const response = await this.httpRequest(new Request(url, args), requestOptions);
return (await response.json()) as T;
}
public async patchAsync<T>(url: string, body: any): Promise<T> {
public async patchAsync<T>(url: string, body: any, requestOptions: IResourceProviderRequestOptions): Promise<T> {
body = typeof body !== "string" && body !== undefined ? JSON.stringify(body) : body;
const args: RequestInit = { method: "PATCH", headers: { "Content-Type": "application/json" }, body };
const response = await this.httpRequest(new Request(url, args));
const response = await this.httpRequest(new Request(url, args), requestOptions);
return (await response.json()) as T;
}
public async deleteAsync(url: string): Promise<void> {
public async deleteAsync(url: string, requestOptions: IResourceProviderRequestOptions): Promise<void> {
const args: RequestInit = { method: "DELETE" };
await this.httpRequest(new Request(url, args));
await this.httpRequest(new Request(url, args), requestOptions);
return null;
}
public async httpRequest<T>(request: RequestInfo, numRetries: number = 12): Promise<Response> {
public async httpRequest<T>(
request: RequestInfo,
requestOptions: IResourceProviderRequestOptions,
numRetries: number = 12
): Promise<Response> {
const authHeader = await this.tokenProvider.getAuthHeader();
authHeader &&
authHeader.forEach((value: string, header: string) => {
@ -99,7 +145,7 @@ class HttpClient {
if (response.status === HttpStatusCodes.Accepted) {
const operationStatusUrl: string =
response.headers && response.headers.get(HttpClient.AZURE_ASYNC_OPERATION_HEADER);
const resource = await this.pollOperationAndGetResultAsync<T>(request, operationStatusUrl);
const resource = await this.pollOperationAndGetResultAsync<T>(request, operationStatusUrl, requestOptions);
return new Response(resource && JSON.stringify(resource));
}
@ -112,7 +158,7 @@ class HttpClient {
return new Promise<Response>((resolve: (value: Response) => void, reject: (error: any) => void) => {
setTimeout(async () => {
try {
const response = await this.httpRequest<T>(request, numRetries - 1);
const response = await this.httpRequest<T>(request, requestOptions, numRetries - 1);
resolve(response);
} catch (error) {
reject(error);
@ -128,7 +174,7 @@ class HttpClient {
response.headers && response.headers.get(HttpClient.AZURE_ASYNC_OPERATION_HEADER);
if (operationStatusUrl) {
const resource = await this.pollOperationAndGetResultAsync<T>(request, operationStatusUrl);
const resource = await this.pollOperationAndGetResultAsync<T>(request, operationStatusUrl, requestOptions);
return new Response(resource && JSON.stringify(resource));
}
@ -140,16 +186,17 @@ class HttpClient {
private async pollOperationAndGetResultAsync<T>(
originalRequest: RequestInfo,
operationStatusUrl: string
operationStatusUrl: string,
requestOptions: IResourceProviderRequestOptions
): Promise<T> {
const getOperationResult = async (resolve: (value: T) => void, reject: (error: any) => void) => {
const operationStatus: OperationStatus = await this.getAsync<OperationStatus>(operationStatusUrl);
const operationStatus: OperationStatus = await this.getAsync<OperationStatus>(operationStatusUrl, requestOptions);
if (!operationStatus) {
return reject("Could not retrieve operation status");
} else if (operationStatus.status === HttpClient.SUCCEEDED_STATUS) {
let result;
if ((originalRequest as Request).method !== "DELETE") {
result = await this.getAsync<T>((originalRequest as Request).url);
if (requestOptions?.skipResourceValidation === false) {
result = await this.getAsync<T>((originalRequest as Request).url, requestOptions);
}
return resolve(result);
} else if (