Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
YoDaMa committed May 17, 2021
1 parent f4e6792 commit a393f91
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sdk/iot/modelsrepository/src/fetcherAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DTDL | DTDL[]>;
}
16 changes: 13 additions & 3 deletions sdk/iot/modelsrepository/src/fetcherFilesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit a393f91

Please sign in to comment.