Skip to content

Commit

Permalink
Merge pull request #2 from chradek/iot-models-repo-test
Browse files Browse the repository at this point in the history
remove circular dependencies and fix mappings
  • Loading branch information
YoDaMa authored May 19, 2021
2 parents b006c90 + 5d836c3 commit 86a0a1d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 68 deletions.
5 changes: 2 additions & 3 deletions sdk/iot/modelsrepository/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"./dist-esm/src/utils/url.js": "./dist-esm/src/utils/url.browser.js",
"./dist-esm/src/utils/absolutePath.js": "./dist-esm/src/utils/absolutePath.browser.js",
"./dist-esm/src/fetcherFilesystem.js": "./dist-esm/src/utils/fetcherFilesystem.browser.js",
"./dist-esm/src/normalize.js": "./dist-esm/src/utils/normalize.browser.js"
"./dist-esm/src/utils/normalize.js": "./dist-esm/src/utils/normalize.browser.js"
},
"types": "types/iot-models-repository.d.ts",
"scripts": {
Expand Down Expand Up @@ -133,7 +133,6 @@
"azure",
"azure-iot-modelsrepository"
],
"requiredResources": {
}
"requiredResources": {}
}
}
25 changes: 17 additions & 8 deletions sdk/iot/modelsrepository/src/dtmiResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-client";
import { DTDL } from "./DTDL";
import { convertDtmiToPath } from "./dtmiConventions";
import { ModelError } from "./exceptions";
import { Fetcher } from "./fetcherAbstract";
import { convertDtmiToPath, DTDL, logger, ModelError } from "./internal";
import { logger } from "./logger";

/**
* DtmiResolver handles reformatting the DTMIs to paths and passing options down to the configured fetcher.
* @internal
*/
* @internal
*/
export class DtmiResolver {
private _fetcher: Fetcher;
constructor(fetcher: Fetcher) {
this._fetcher = fetcher;
}

async resolve(dtmis: string[], expandedModel: boolean, options?: OperationOptions): Promise<{[dtmi: string]: DTDL}> {
async resolve(
dtmis: string[],
expandedModel: boolean,
options?: OperationOptions
): Promise<{ [dtmi: string]: DTDL }> {
const modelMap: any = {};
const dtdlPromises = dtmis.map(async (dtmi) => {
const dtdlPath = convertDtmiToPath(dtmi, expandedModel);
logger.info(`Model ${dtmi} located in repository at ${dtdlPath}`);
const dtdl = await this._fetcher.fetch(dtdlPath, options)
const dtdl = await this._fetcher.fetch(dtdlPath, options);
if (expandedModel) {
if (Array.isArray(dtdl)) {
const modelIds: string[] = (dtdl as any[]).map((model:any) => model["@id"]);
const modelIds: string[] = (dtdl as any[]).map((model: any) => model["@id"]);
if (!modelIds.includes(dtmi)) {
throw new ModelError(`DTMI mismatch on expanded DTDL - Request: ${dtmi}, Response: ${modelIds}`);
throw new ModelError(
`DTMI mismatch on expanded DTDL - Request: ${dtmi}, Response: ${modelIds}`
);
}
for (const model of dtdl) {
modelMap[model["@id"]] = model;
}
} else {
throw new ModelError('Expanded format should always return an array of models.');
throw new ModelError("Expanded format should always return an array of models.");
}
} else {
const model = dtdl as DTDL;
Expand Down
2 changes: 1 addition & 1 deletion sdk/iot/modelsrepository/src/fetcherAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-client";
import { DTDL } from "./internal";
import { DTDL } from "./DTDL";

/**
* @internal
Expand Down
12 changes: 7 additions & 5 deletions sdk/iot/modelsrepository/src/fetcherFilesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import fs from "fs";
import * as path from "path";
import { Fetcher, DTDL, logger } from "./internal";
import {RestError, RestErrorOptions} from '@azure/core-rest-pipeline';
import { RestError, RestErrorOptions } from "@azure/core-rest-pipeline";
import { Fetcher } from "./fetcherAbstract";
import { logger } from "./logger";
import { DTDL } from "./DTDL";

function readFilePromise(path: string): Promise<string> {
return new Promise((res, rej) => {
Expand Down Expand Up @@ -36,10 +38,10 @@ export class FilesystemFetcher implements Fetcher {
return parsedDtdl;
} catch (e) {
// TODO: Is there a ResourceNotFound Error for Filesystem + Http (Generic API for errors)
const options : RestErrorOptions = {
code: 'ResourceNotFound',
const options: RestErrorOptions = {
code: "ResourceNotFound",
statusCode: e?.status
}
};
throw new RestError("Failed to fetch from Filesystem", options);
}
}
Expand Down
29 changes: 13 additions & 16 deletions sdk/iot/modelsrepository/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
/**
* This is the ModelsRepositoryClient Library for Javascript.
* Why Models and not Model? Because there's more than one Model.
*
*
* @remarks
* This ModelsRepositoryClient is built around getting DTDL Models from a user-specified
* location. The two main variables are the repositoryLocation, which is a path or URI to either a remote
* or local repository where the models are located, and the dtmis, which can be one or more dtmis that
* or local repository where the models are located, and the dtmis, which can be one or more dtmis that
* will be mapped to specific models contained in the repository location that the user wishes to get.
*
*
* @example
* Inline code:
* ```typescript
Expand All @@ -19,30 +19,27 @@
*
* const repositoryEndpoint = "devicemodels.azure.com";
* const dtmi = process.argv[2] || "dtmi:azure:DeviceManagement:DeviceInformation;1";
*
*
* console.log(repositoryEndpoint, dtmi);
*
*
* async function main() {
* const client = new ModelsRepositoryClient({repositoryLocation: repositoryEndpoint});
* const result = await client.getModels(dtmi, {dependencyResolution: 'tryFromExpanded'});
* console.log(result);
* }
*
*
* main().catch((err) => {
* console.error("The sample encountered an error:", err);
* });
*
* ```
*
*
* @packageDocumentation
*/

export {
ModelsRepositoryClient,
ModelsRepositoryClientOptions,
GetModelsOptions,
dependencyResolutionType,
isValidDtmi,
getModelUri,
ModelError
} from "./internal";
export { ModelsRepositoryClient } from "./modelsRepositoryClient";
export { GetModelsOptions } from "./interfaces/getModelsOptions";
export { ModelsRepositoryClientOptions } from "./interfaces/modelsRepositoryClientOptions";
export { dependencyResolutionType } from "./dependencyResolutionType";
export { ModelError } from "./exceptions";
export { getModelUri, isValidDtmi } from "./dtmiConventions";
6 changes: 3 additions & 3 deletions sdk/iot/modelsrepository/src/interfaces/getModelsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-client";
import { dependencyResolutionType } from "../internal";
import { dependencyResolutionType } from "../dependencyResolutionType";

/**
* Options for getting models.
*/
export interface GetModelsOptions extends OperationOptions {
/**
* This is the format of dependency resolution (no dependency resolution, standard dependency
* resolution, resolution using the expanded json format) that will be used when retrieving
* models. This overwrites the default dependencyResolution settings of the client.
* resolution, resolution using the expanded json format) that will be used when retrieving
* models. This overwrites the default dependencyResolution settings of the client.
*/
dependencyResolution?: dependencyResolutionType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Licensed under the MIT license.

import { CommonClientOptions } from "@azure/core-client";
import { dependencyResolutionType } from "../internal";

import { dependencyResolutionType } from "../dependencyResolutionType";

/**
* Options for creating a Pipeline to use with ModelsRepositoryClient.
Expand All @@ -23,7 +22,7 @@ export interface ModelsRepositoryClientOptions extends CommonClientOptions {

/**
* This is the format of dependency resolution (no dependency resolution, standard dependency
* resolution, resolution using the expanded json format) that will be used when retrieving
* resolution, resolution using the expanded json format) that will be used when retrieving
* models.
*/
dependencyResolution?: dependencyResolutionType;
Expand Down
16 changes: 0 additions & 16 deletions sdk/iot/modelsrepository/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
export * from './modelsRepositoryClient';
export * from './constants';
export * from './dependencyResolutionType';
export * from './dtmiConventions';
export * from './fetcherAbstract';
export * from './fetcherFilesystem';
export * from './fetcherHTTP';
export * from './modelsRepositoryServiceClient';
export * from './logger';
export * from './psuedoParser';
export * from './dtmiResolver';

export * from './interfaces/getModelsOptions';
export * from './interfaces/modelsRepositoryClientOptions';
export * from './DTDL';
export * from './exceptions';
22 changes: 10 additions & 12 deletions sdk/iot/modelsrepository/src/modelsRepositoryClient.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// Copyright (c) Microsoft.
// Licensed under the MIT license.

import {
GetModelsOptions,
IoTModelsRepositoryServiceClient,
ModelsRepositoryClientOptions,
dependencyResolutionType,
HttpFetcher,
FilesystemFetcher,
PseudoParser,
DtmiResolver,
logger,
DTDL
} from "./internal";
import * as cnst from "./constants";
import { createClientPipeline, InternalClientPipelineOptions } from "@azure/core-client";
import { Fetcher } from "./fetcherAbstract";
import { URL } from "./utils/url";
import { isLocalPath } from "./utils/absolutePath";
import { normalize } from "./utils/normalize";
import { FilesystemFetcher } from "./fetcherFilesystem";
import { dependencyResolutionType } from "./dependencyResolutionType";
import { DtmiResolver } from "./dtmiResolver";
import { PseudoParser } from "./psuedoParser";
import { ModelsRepositoryClientOptions } from "./interfaces/modelsRepositoryClientOptions";
import { logger } from "./logger";
import { IoTModelsRepositoryServiceClient } from "./modelsRepositoryServiceClient";
import { HttpFetcher } from "./fetcherHTTP";
import { GetModelsOptions } from "./interfaces/getModelsOptions";
import { DTDL } from "./DTDL";

/**
* Initializes a new instance of the IoT Models Repository Client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { ServiceClientOptions, ServiceClient } from "@azure/core-client";
import { DEFAULT_API_VERSION } from "./internal";
import { DEFAULT_API_VERSION } from "./constants";

interface IoTModelsRepositoryServiceClientOptions extends ServiceClientOptions {
version?: string;
Expand Down

0 comments on commit 86a0a1d

Please sign in to comment.