import * as request from "request-promise" ; import { Constants } from "../common/constants" ; import { ModelType } from "../deviceModel/deviceModelManager" ; import { GetResult , MetaModelType , SearchOptions , SearchResult } from "./modelRepositoryInterface" ; import { RepositoryInfo } from "./modelRepositoryManager" ; /**
 * Http method type
 */ enum HttpMethod { Get = "GET" , Post = "POST" , Put = "PUT" , Delete = "DELETE" } /**
 * DigitalTwin model repository client
 */ export class ModelRepositoryClient { param repoInfo param modelId param expand static async getModel ( repoInfo , modelId , expand = false ) { const options = ModelRepositoryClient . createOptions ( HttpMethod . Get , repoInfo , modelId ) ; if ( expand ) { options . qs . expand = "true" ; } return new Promise < GetResult > ( ( resolve , reject ) => { request ( options ) . then ( response => { const result = { etag : response . headers [ ModelRepositoryClient . ETAG_HEADER ] , modelId : response . headers [ "x-ms-model-id" ] , content : response . body } ; return resolve ( result ) ; } ) . catch ( err => { reject ( err ) ; } ) ; } ) ; } param repoInfo param type param keyword param pageSize param continuationToken static async searchModel ( repoInfo , type , keyword , pageSize , continuationToken ) { const options = ModelRepositoryClient . createOptions ( HttpMethod . Post , repoInfo ) ; const modelFilterType = ModelRepositoryClient . convertToMetaModelType ( type ) ; const payload = { searchKeyword : keyword , modelFilterType , continuationToken , pageSize } ; options . body = payload ; return new Promise < SearchResult > ( ( resolve , reject ) => { request ( options ) . then ( response => { const result = response . body as SearchResult ; return resolve ( result ) ; } ) . catch ( err => { reject ( err ) ; } ) ; } ) ; } param repoInfo param modelId param content static async updateModel ( repoInfo , modelId , content ) { const options = ModelRepositoryClient . createOptions ( HttpMethod . Put , repoInfo , modelId ) ; options . body = content ; return new Promise < string > ( ( resolve , reject ) => { request ( options ) . then ( response => { const result = response . headers [ ModelRepositoryClient . ETAG_HEADER ] ; return resolve ( result ) ; } ) . catch ( err => { reject ( err ) ; } ) ; } ) ; } param repoInfo param modelId static async deleteModel ( repoInfo , modelId ) { const options = ModelRepositoryClient . createOptions ( HttpMethod . Delete , repoInfo , modelId ) ; return new Promise < void > ( ( resolve , reject ) => { request ( options ) . then ( ( ) => { resolve ( ) ; } ) . catch ( err => { reject ( err ) ; } ) ; } ) ; } private static readonly ETAG_HEADER = "etag" ; param type private static convertToMetaModelType ( type ) { switch ( type ) { case ModelType . Interface : return MetaModelType . Interface ; case ModelType . CapabilityModel : return MetaModelType . CapabilityModel ; default : return MetaModelType . None ; } } param method param repoInfo param modelId private static createOptions ( method , repoInfo , modelId ? ) { const uri = modelId ? `${ repoInfo . hostname }/models/${ encodeURIComponent ( modelId ) }` : `${ repoInfo . hostname }/models/search` ; const qs = { "api-version" : repoInfo . apiVersion } ; if ( repoInfo . repositoryId ) { qs . repositoryId = repoInfo . repositoryId ; } const accessToken = repoInfo . accessToken || Constants . EMPTY_STRING ; const options = { method , uri , qs , encoding : Constants . UTF8 , json : true , headers : { Authorization : accessToken , "Content-Type" : "application/json" } , resolveWithFullResponse : true } ; return options ; } private constructor ( ) { } }