packageDocumentation module import { AccessToken , IModelHubStatus , Logger } from "@itwin/core-bentley" ; import { Project as ITwin } from "@itwin/projects-client" ; import { request , RequestOptions , Response } from "../itwin-client/Request" ; import { WsgInstance } from "../wsg/ECJsonTypeMap" ; import { WsgError , WSStatus } from "../wsg/WsgClient" ; import { ITwinManagerClient } from "../IModelCloudEnvironment" ; import { IModelHubClientError } from "../imodelhub/Errors" ; import { IModelHubClientLoggerCategory } from "../IModelHubClientLoggerCategories" ; const loggerCategory = IModelHubClientLoggerCategory . IModelBank ; internal export interface IModelFileSystemITwinProps { name : string ; id : string ; description : string ; } internal export class IModelBankFileSystemITwinClient implements ITwinManagerClient { constructor ( public baseUri ) { } private async queryITwinProps ( accessToken , iTwinName ) { const url = `${ this . baseUri }/sv1.0/Repositories/Global--main/GlobalScope/Context` ; Logger . logInfo ( loggerCategory , `Sending GET request to ${ url }` ) ; const queryOptions = { $select : "*" , $filter : `name+eq+'${ iTwinName }'` } ; const options = { method : "GET" , headers : { authorization : accessToken } , qs : queryOptions , accept : "application/json" } ; const res = await request ( url , options ) ; if ( ! res . body || ! res . body . instances ) { throw new Error ( `Query to URL ${ url } executed successfully, but did NOT return anything.` ) ; } const props = res . body . instances as WsgInstance [ ] ; if ( props . length === 0 ) throw new WsgError ( WSStatus . InstanceNotFound ) ; if ( props . length !== 1 ) throw new IModelHubClientError ( IModelHubStatus . InvalidArgumentError ) ; Logger . logTrace ( loggerCategory , `Successful GET request to ${ url }` ) ; return props . map ( ( value ) => value . properties as IModelFileSystemITwinProps ) ; } public async getITwinByName ( accessToken , name ) { const props = await this . queryITwinProps ( accessToken , name ) ; return props [ 0 ] as ITwin ; } public async createITwin ( accessToken , name ) { const url = `${ this . baseUri }/sv1.0/Repositories/Global--main/GlobalScope/Context` ; Logger . logInfo ( loggerCategory , `Sending POST request to ${ url }` ) ; const body = { instance : { className : "ITwin" , schemaName : "GlobalScope" , properties : { name , id : "" , description : "" } } } ; const options = { method : "POST" , headers : { authorization : accessToken } , body } ; await request ( url , options ) ; } public async deleteITwin ( accessToken , iTwinId ) { const url = `${ this . baseUri }/sv1.0/Repositories/Global--main/GlobalScope/Context/${ iTwinId }` ; Logger . logInfo ( loggerCategory , `Sending DELETE request to ${ url }` ) ; const options = { method : "DELETE" , headers : { authorization : accessToken } } ; await request ( url , options ) ; } }