UNPKG

@itwin/imodels-client-authoring

Version:

iModels API client wrapper for applications that author iModels.

98 lines 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IModelOperations = void 0; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const imodels_client_management_1 = require("@itwin/imodels-client-management"); const types_1 = require("../../base/types"); const BaselineFileOperations_1 = require("../baseline-file/BaselineFileOperations"); class IModelOperations extends imodels_client_management_1.IModelOperations { _baselineFileOperations; constructor(options, iModelsClient) { super(options, iModelsClient); this._baselineFileOperations = new BaselineFileOperations_1.BaselineFileOperations(options); } /** * Creates an iModel from Baseline file with specified properties. Wraps the * {@link https://developer.bentley.com/apis/imodels-v2/operations/create-imodel/ Create iModel} operation from iModels API. * Internally it creates an iModel instance, uploads the Baseline file, confirms Baseline * file upload and then repeatedly queries the Baseline file state until the iModel is initialized. The execution of * this method can take up to several minutes due to waiting for initialization to complete. It also depends on the * Baseline file size - the larger the file, the longer the upload will take. * @param {CreateIModelFromBaselineParams} params parameters for this operation. See {@link CreateIModelFromBaselineParams}. * @returns {Promise<IModel>} newly created iModel. See {@link IModel}. * @throws an error that implements `iModelsError` interface with code {@link IModelsErrorCode.BaselineFileInitializationFailed} if * Baseline file initialization failed or {@link IModelsErrorCode.BaselineFileInitializationTimedOut} if the operation did not complete in time. * See {@link IModelsErrorCode}. */ async createFromBaseline(params) { const baselineFileSize = await this._options.localFileSystem.getFileSize(params.iModelProperties.filePath); const createIModelBody = this.getCreateIModelFromBaselineRequestBody(params.iModelProperties, baselineFileSize); const createdIModel = await this.sendIModelPostRequest(params.authorization, createIModelBody, params.headers); (0, imodels_client_management_1.assertLink)(createdIModel._links.upload); const uploadLink = createdIModel._links.upload; await this._options.cloudStorage.upload({ url: uploadLink.href, storageType: uploadLink.storageType, data: params.iModelProperties.filePath, }); (0, imodels_client_management_1.assertLink)(createdIModel._links.complete); const confirmUploadUrl = createdIModel._links.complete.href; await this.sendPostRequest({ authorization: params.authorization, url: confirmUploadUrl, body: undefined, headers: params.headers, }); await this.waitForBaselineFileInitialization({ authorization: params.authorization, iModelId: createdIModel.id, headers: params.headers, timeOutInMs: params.timeOutInMs, }); return this.getSingle({ authorization: params.authorization, iModelId: createdIModel.id, headers: params.headers, }); } getCreateIModelFromBaselineRequestBody(iModelProperties, baselineFileSize) { return { ...this.getCreateEmptyIModelRequestBody(iModelProperties), baselineFile: { size: baselineFileSize, }, }; } async waitForBaselineFileInitialization(params) { const isBaselineInitialized = async () => { const { state } = await this._baselineFileOperations.getSingle(params); if (state !== types_1.BaselineFileState.Initialized && state !== types_1.BaselineFileState.WaitingForFile && state !== types_1.BaselineFileState.InitializationScheduled) throw new imodels_client_management_1.IModelsErrorImpl({ code: imodels_client_management_1.IModelsErrorCode.BaselineFileInitializationFailed, message: `Baseline File initialization failed with state '${state}.'`, originalError: undefined, statusCode: undefined, details: undefined, }); return state === types_1.BaselineFileState.Initialized; }; return imodels_client_management_1.UtilityFunctions.waitForCondition({ conditionToSatisfy: isBaselineInitialized, timeoutErrorFactory: () => new imodels_client_management_1.IModelsErrorImpl({ code: imodels_client_management_1.IModelsErrorCode.BaselineFileInitializationTimedOut, message: "Timed out waiting for Baseline File initialization.", originalError: undefined, statusCode: undefined, details: undefined, }), timeOutInMs: params.timeOutInMs, }); } } exports.IModelOperations = IModelOperations; //# sourceMappingURL=IModelOperations.js.map