diff --git a/sdk/iot/modelsrepository/src/fetcherAbstract.ts b/sdk/iot/modelsrepository/src/fetcherAbstract.ts index f42e12698eee..ef0ba19cb473 100644 --- a/sdk/iot/modelsrepository/src/fetcherAbstract.ts +++ b/sdk/iot/modelsrepository/src/fetcherAbstract.ts @@ -2,7 +2,8 @@ // Licensed under the MIT license. import { OperationOptions } from "@azure/core-client"; +import { DTDL } from "./internal"; export abstract class Fetcher { - abstract fetch(path: string, options: OperationOptions): any; + abstract fetch(path: string, options: OperationOptions): Promise; } diff --git a/sdk/iot/modelsrepository/src/fetcherFilesystem.ts b/sdk/iot/modelsrepository/src/fetcherFilesystem.ts index 819e926fc15d..4b156561e24a 100644 --- a/sdk/iot/modelsrepository/src/fetcherFilesystem.ts +++ b/sdk/iot/modelsrepository/src/fetcherFilesystem.ts @@ -6,6 +6,15 @@ import * as path from "path"; import { Fetcher, DTDL, logger } from "./internal"; import {RestError, RestErrorOptions} from '@azure/core-rest-pipeline'; +function readFilePromise(path: string): Promise { + return new Promise((res, rej) => { + fs.readFile(path, "utf8", (err, data) => { + err ? rej(err) : res(data); + return 0; + }); + }); +} + export class FilesystemFetcher extends Fetcher { private _baseFilePath: string; @@ -14,14 +23,15 @@ export class FilesystemFetcher extends Fetcher { this._baseFilePath = baseFilePath; } - fetch(filePath: string) { + async fetch(filePath: string) { logger.info(`Fetching ${filePath} from local filesystem`); const absolutePath = path.join(this._baseFilePath, filePath); try { logger.info(`File open on ${absolutePath}`); - const dtdlFile = fs.readFileSync(absolutePath, "utf8"); - const parsedDtdl: DTDL | DTDL[] = JSON.parse(dtdlFile); + const parsedDtdl: DTDL | DTDL[] = await readFilePromise(absolutePath).then((dtdlFile) => { + return JSON.parse(dtdlFile); + }); return parsedDtdl; } catch (e) { // TODO: Is there a ResourceNotFound Error for Filesystem + Http (Generic API for errors) diff --git a/sdk/iot/modelsrepository/src/modelsRepositoryServiceClient.ts b/sdk/iot/modelsrepository/src/modelsRepositoryServiceClient.ts index d8d6759a1f99..469f0716f436 100644 --- a/sdk/iot/modelsrepository/src/modelsRepositoryServiceClient.ts +++ b/sdk/iot/modelsrepository/src/modelsRepositoryServiceClient.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. import { ServiceClientOptions, ServiceClient } from "@azure/core-client"; +import { DEFAULT_API_VERSION } from "./internal"; interface IoTModelsRepositoryServiceClientOptions extends ServiceClientOptions { version?: string; @@ -31,6 +32,6 @@ export class IoTModelsRepositoryServiceClient extends ServiceClient { super(optionsWithDefaults); this.url = url; - this.version = options.version || "2019-02-02"; + this.version = options.version || DEFAULT_API_VERSION; } }