public async static Task <IEnumerable <string> > ParserDtmiResolver(this ModelsRepositoryClient client, IReadOnlyCollection <Dtmi> dtmis) { IEnumerable <string> dtmiStrings = dtmis.Select(s => s.AbsoluteUri); IDictionary <string, string> result = await client.GetModelsAsync(dtmiStrings); return(result.Values.ToList()); }
public async Task GetModelsMultipleDtmisTryFromExpandedPartial() { const string dtmisExpanded = "dtmi:com:example:TemperatureController;1," + // Expanded available. "dtmi:com:example:Thermostat;1," + "dtmi:azure:DeviceManagement:DeviceInformation;1"; const string dtmisNonExpanded = "dtmi:com:example:ColdStorage;1," + // Model uses extends[], No Expanded available. "dtmi:com:example:Room;1," + "dtmi:com:example:Freezer;1"; string[] expandedDtmis = dtmisExpanded.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); string[] nonExpandedDtmis = dtmisNonExpanded.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); string[] totalDtmis = expandedDtmis.Concat(nonExpandedDtmis).ToArray(); ModelsRepositoryClientOptions options = new ModelsRepositoryClientOptions(dependencyResolution: ModelDependencyResolution.TryFromExpanded); ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local, options); // Multi-resolve dtmi:com:example:TemperatureController;1 + dtmi:com:example:ColdStorage;1 IDictionary <string, string> result = await client.GetModelsAsync(new[] { expandedDtmis[0], nonExpandedDtmis[0] }); result.Keys.Count.Should().Be(totalDtmis.Length); foreach (string id in totalDtmis) { result.Should().ContainKey(id); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[id]).Should().Be(id); } }
public static void ClientInitialization() { #region Snippet:ModelsRepositorySamplesCreateServiceClientWithGlobalEndpoint // When no URI is provided for instantiation, the Azure IoT Models Repository global endpoint // https://devicemodels.azure.com/ is used and the model dependency resolution // configuration is set to TryFromExpanded. var client = new ModelsRepositoryClient(new ModelsRepositoryClientOptions()); Console.WriteLine($"Initialized client pointing to global endpoint: {client.RepositoryUri}"); #endregion Snippet:ModelsRepositorySamplesCreateServiceClientWithGlobalEndpoint // This form shows specifing a custom URI for the models repository with default client options. // The default client options will enable model dependency resolution. const string remoteRepoEndpoint = "https://contoso.com/models"; client = new ModelsRepositoryClient(new Uri(remoteRepoEndpoint)); Console.WriteLine($"Initialized client pointing to custom endpoint: {client.RepositoryUri}"); #region Snippet:ModelsRepositorySamplesCreateServiceClientWithLocalRepository // The client will also work with a local filesystem URI. This example shows initalization // with a local URI and disabling model dependency resolution. client = new ModelsRepositoryClient(new Uri(ClientSamplesLocalModelsRepository), new ModelsRepositoryClientOptions(dependencyResolution: ModelDependencyResolution.Disabled)); Console.WriteLine($"Initialized client pointing to local path: {client.RepositoryUri}"); #endregion Snippet:ModelsRepositorySamplesCreateServiceClientWithLocalRepository }
public static void ClientInitialization() { #region Snippet:ModelsRepositorySamplesCreateServiceClientWithGlobalEndpoint // When no URI is provided for instantiation, the Azure IoT Models Repository global endpoint // https://devicemodels.azure.com/ is used. var client = new ModelsRepositoryClient(new ModelsRepositoryClientOptions()); Console.WriteLine($"Initialized client pointing to global endpoint: {client.RepositoryUri.AbsoluteUri}"); #endregion Snippet:ModelsRepositorySamplesCreateServiceClientWithGlobalEndpoint #region Snippet:ModelsRepositorySamplesCreateServiceClientWithCustomEndpoint // This form shows specifing a custom URI for the models repository with default client options. const string remoteRepoEndpoint = "https://contoso.com/models"; client = new ModelsRepositoryClient(new Uri(remoteRepoEndpoint)); Console.WriteLine($"Initialized client pointing to custom endpoint: {client.RepositoryUri.AbsoluteUri}"); #endregion Snippet:ModelsRepositorySamplesCreateServiceClientWithCustomEndpoint #region Snippet:ModelsRepositorySamplesCreateServiceClientWithLocalRepository // The client will also work with a local filesystem URI. client = new ModelsRepositoryClient(new Uri(ClientSamplesLocalModelsRepository)); Console.WriteLine($"Initialized client pointing to local path: {client.RepositoryUri.LocalPath}"); #endregion Snippet:ModelsRepositorySamplesCreateServiceClientWithLocalRepository }
public void GetModelsInvalidDtmiFormatThrowsException(string dtmi) { ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); string expectedExMsg = $"{string.Format(StandardStrings.GenericGetModelsError, dtmi)} {string.Format(StandardStrings.InvalidDtmiFormat, dtmi)}"; Func <Task> act = async() => await client.GetModelsAsync(dtmi); act.Should().Throw <ArgumentException>().WithMessage(expectedExMsg); }
public RepoProvider(string repoLocationUri) { if (IsRelativePath(repoLocationUri)) { repoLocationUri = Path.GetFullPath(repoLocationUri); } _repositoryClient = new ModelsRepositoryClient(new Uri(repoLocationUri)); }
public void GetModelInvalidFileContentFormatThrowsException() { const string dtmi = "dtmi:com:example:invalidformat;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); Func <Task> act = async() => await client.GetModelAsync(dtmi); act.Should().Throw <JsonException>(); }
public async Task GetModelsSingleDtmiWithDepsFromExtendsInline() { const string dtmi = "dtmi:com:example:base;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); IDictionary <string, string> result = await client.GetModelsAsync(dtmi); result.Keys.Count.Should().Be(1); result.Should().ContainKey(dtmi); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmi]).Should().Be(dtmi); }
public void GetModelsForNonExistentDtmiFileThrowsException(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi = "dtmi:com:example:thermojax;999"; ModelsRepositoryClient client = GetClient(clientType); Func <Task> act = async() => await client.GetModelsAsync(dtmi); act.Should().Throw <RequestFailedException>(); }
public void GetModelsInvalidDtmiDepsThrowsException() { const string dtmi = "dtmi:com:example:invalidmodel;1"; const string invalidDep = "dtmi:azure:fakeDeviceManagement:FakeDeviceInformation;2"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); Func <Task> act = async() => await client.GetModelsAsync(dtmi); act.Should().Throw <RequestFailedException>().WithMessage($"Unable to resolve \"{invalidDep}\""); }
public async Task GetModelsEnsuresNoDupes() { const string dtmiDupe1 = "dtmi:azure:DeviceManagement:DeviceInformation;1"; const string dtmiDupe2 = "dtmi:azure:DeviceManagement:DeviceInformation;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); IDictionary <string, string> result = await client.GetModelsAsync(new[] { dtmiDupe1, dtmiDupe2 }); result.Keys.Count.Should().Be(1); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmiDupe1]).Should().Be(dtmiDupe1); }
public async Task GetModelsSingleDtmiWithDepsDisabledDependencyResolution(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi = "dtmi:com:example:TemperatureController;1"; ModelsRepositoryClient client = GetClient(clientType); IDictionary <string, string> result = await client.GetModelsAsync(dtmi, ModelDependencyResolution.Disabled); result.Keys.Count.Should().Be(1); result.Should().ContainKey(dtmi); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmi]).Should().Be(dtmi); }
public async Task GetModelNoDependenciesExtendsInline() { const string dtmi = "dtmi:com:example:base;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); ModelResult result = await client.GetModelAsync(dtmi); result.Content.Count.Should().Be(1); result.Content.ContainsKey(dtmi).Should().BeTrue(); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result.Content[dtmi]).Should().Be(dtmi); }
public async Task GetModelDependenciesDisabledDependencyResolution(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi = "dtmi:com:example:TemperatureController;1"; ModelsRepositoryClient client = GetClient(clientType); ModelResult result = await client.GetModelAsync(dtmi, ModelDependencyResolution.Disabled); result.Content.Count.Should().Be(1); result.Content.ContainsKey(dtmi).Should().BeTrue(); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result.Content[dtmi]).Should().Be(dtmi); }
public RepoProvider(string repoLocationUri, ModelDependencyResolution dependencyResolution = ModelDependencyResolution.Enabled) { if (IsRelativePath(repoLocationUri)) { repoLocationUri = Path.GetFullPath(repoLocationUri); } _repositoryClient = new ModelsRepositoryClient(new Uri(repoLocationUri), new ModelsRepositoryClientOptions(dependencyResolution: dependencyResolution)); }
public static async Task GetModelsAndParseAsync() { var dtmi = "dtmi:com:example:TemperatureController;1"; var client = new ModelsRepositoryClient(); IDictionary <string, string> models = await client.GetModelsAsync(dtmi).ConfigureAwait(false); var parser = new ModelParser(); IReadOnlyDictionary <Dtmi, DTEntityInfo> parseResult = await parser.ParseAsync(models.Values.ToArray()); Console.WriteLine($"{dtmi} resolved in {models.Count} interfaces with {parseResult.Count} entities."); }
public async Task GetModelsSingleDtmiNoDeps(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi = "dtmi:com:example:Thermostat;1"; ModelsRepositoryClient client = GetClient(clientType); IDictionary <string, string> result = await client.GetModelsAsync(dtmi); result.Keys.Count.Should().Be(1); result.Should().ContainKey(dtmi); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmi]).Should().Be(dtmi); }
public void GetModelInvalidDtmiDependencyThrowsException() { const string dtmi = "dtmi:com:example:invalidmodel;1"; const string invalidDep = "dtmi:azure:fakeDeviceManagement:FakeDeviceInformation;2"; string invalidDepPath = DtmiConventions.GetModelUri(invalidDep, new Uri(ModelsRepositoryTestBase.TestLocalModelsRepository)).LocalPath; string expectedExMsg = $"{string.Format(StandardStrings.GenericGetModelsError, invalidDep)} {string.Format(StandardStrings.ErrorFetchingModelContent, invalidDepPath)}"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); Func <Task> act = async() => await client.GetModelAsync(dtmi); act.Should().Throw <RequestFailedException>().WithMessage(expectedExMsg); }
public async Task GetModelsMultipleDtmisNoDeps(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi1 = "dtmi:com:example:Thermostat;1"; const string dtmi2 = "dtmi:azure:DeviceManagement:DeviceInformation;1"; ModelsRepositoryClient client = GetClient(clientType); IDictionary <string, string> result = await client.GetModelsAsync(new string[] { dtmi1, dtmi2 }); result.Keys.Count.Should().Be(2); result.Should().ContainKey(dtmi1); result.Should().ContainKey(dtmi2); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmi1]).Should().Be(dtmi1); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[dtmi2]).Should().Be(dtmi2); }
/// <summary> /// The ParserDtmiResolver extension illustrates the simplicity of /// integrating the ModelsRepositoryClient with the Digital Twins Parser. /// See ParserIntegrationSamples.cs for example usage. /// </summary> public static async Task <IEnumerable <string> > ParserDtmiResolver(this ModelsRepositoryClient client, IReadOnlyCollection <Dtmi> dtmis) { IEnumerable <string> dtmiStrings = dtmis.Select(s => s.AbsoluteUri); List <string> modelDefinitions = new(); foreach (var dtmi in dtmiStrings) { ModelResult result = await client.GetModelAsync(dtmi, ModelDependencyResolution.Disabled); modelDefinitions.Add(result.Content[dtmi]); } return(modelDefinitions); }
public void GetModelsWithWrongCasingThrowsException(ModelsRepositoryTestBase.ClientType clientType) { const string dtmi = "dtmi:com:example:thermostat;1"; ModelsRepositoryClient client = GetClient(clientType); string expectedExMsg = string.Format(StandardStrings.GenericGetModelsError, "dtmi:com:example:thermostat;1") + " " + string.Format(StandardStrings.IncorrectDtmiCasing, "dtmi:com:example:thermostat;1", "dtmi:com:example:Thermostat;1"); Func <Task> act = async() => await client.GetModelsAsync(dtmi); act.Should().Throw <RequestFailedException>().WithMessage(expectedExMsg); }
public static async Task ParseAndGetModelsWithExtensionAsync() { var dtmi = "dtmi:com:example:TemperatureController;1"; var client = new ModelsRepositoryClient(new ModelsRepositoryClientOptions(resolutionOption: DependencyResolutionOption.Disabled)); IDictionary <string, string> models = await client.GetModelsAsync(dtmi).ConfigureAwait(false); var parser = new ModelParser { // Usage of the ModelsRepositoryClientExtensions.ParserDtmiResolver extension. DtmiResolver = client.ParserDtmiResolver }; IReadOnlyDictionary <Dtmi, DTEntityInfo> parseResult = await parser.ParseAsync(models.Values.Take(1).ToArray()); Console.WriteLine($"{dtmi} resolved in {models.Count} interfaces with {parseResult.Count} entities."); }
public static async Task GetModelAndParseAsync() { #region Snippet:ModelsRepositorySamplesParserIntegrationGetModelsAndParseAsync var client = new ModelsRepositoryClient(); var dtmi = "dtmi:com:example:TemperatureController;1"; ModelResult result = await client.GetModelAsync(dtmi).ConfigureAwait(false); var parser = new ModelParser(); IReadOnlyDictionary <Dtmi, DTEntityInfo> parseResult = await parser.ParseAsync(result.Content.Values); Console.WriteLine($"{dtmi} resolved in {result.Content.Count} interfaces with {parseResult.Count} entities."); #endregion Snippet:ModelsRepositorySamplesParserIntegrationGetModelsAndParseAsync }
public static async Task GetModelsFromLocalRepoAsync() { var dtmi = "dtmi:com:example:TemperatureController;1"; // Local sample repository client var client = new ModelsRepositoryClient(new Uri(ClientSamplesLocalModelsRepository)); // The output of GetModelsAsync() will include at least the definition for the target dtmi. // If the dependency model resolution option is not disabled, then models in which the // target dtmi depends on will also be included in the returned IDictionary<string, string>. IDictionary <string, string> models = await client.GetModelsAsync(dtmi).ConfigureAwait(false); // In this case the above dtmi has 2 model dependencies. // dtmi:com:example:Thermostat;1 and dtmi:azure:DeviceManagement:DeviceInformation;1 Console.WriteLine($"{dtmi} resolved in {models.Count} interfaces."); }
public static async Task TryGetModelsFromGlobalRepoButNotFoundAsync() { var dtmi = "dtmi:com:example:NotFound;1"; var client = new ModelsRepositoryClient(); try { IDictionary <string, string> models = await client.GetModelsAsync(dtmi).ConfigureAwait(false); Console.WriteLine($"{dtmi} resolved in {models.Count} interfaces."); } catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.NotFound) { Console.WriteLine($"{dtmi} was not found in the default public models repository: {ex.Message}"); } }
public static async Task TryGetModelsFromLocalRepoButNotFoundAsync() { var client = new ModelsRepositoryClient(new Uri(ClientSamplesLocalModelsRepository)); var dtmi = "dtmi:com:example:NotFound;1"; try { IDictionary <string, string> models = await client.GetModelsAsync(dtmi).ConfigureAwait(false); Console.WriteLine($"{dtmi} resolved in {models.Count} interfaces."); } catch (RequestFailedException ex) when(ex.InnerException is FileNotFoundException) { Console.WriteLine($"{dtmi} was not found in the default public models repository: {ex.Message}"); } }
public async Task GetModelDependenciesExtendsSingleRef() { const string dtmi = "dtmi:com:example:ConferenceRoom;1"; const string expectedDeps = "dtmi:com:example:Room;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); ModelResult result = await client.GetModelAsync(dtmi); var expectedDtmis = $"{dtmi},{expectedDeps}".Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); result.Content.Count.Should().Be(expectedDtmis.Length); foreach (var id in expectedDtmis) { result.Content.ContainsKey(id).Should().BeTrue(); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result.Content[id]).Should().Be(id); } }
public static async Task TryGetModelsWithInvalidDtmiAsync() { var invalidDtmi = "dtmi:com:example:InvalidDtmi"; // Model resolution will fail with invalid dtmis var client = new ModelsRepositoryClient(); try { await client.GetModelsAsync(invalidDtmi); } catch (ArgumentException ex) { // Invalid DTMI format "dtmi:com:example:InvalidDtmi". Console.WriteLine(ex); } }
public async Task GetModelsSingleDtmiWithDeps(ModelsRepositoryTestBase.ClientType clientType, bool hasMetadata) { const string dtmi = "dtmi:com:example:TemperatureController;1"; const string expectedDeps = "dtmi:com:example:Thermostat;1,dtmi:azure:DeviceManagement:DeviceInformation;1"; ModelsRepositoryClient client = GetClient(clientType, hasMetadata); IDictionary <string, string> result = await client.GetModelsAsync(dtmi); var expectedDtmis = $"{dtmi},{expectedDeps}".Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); result.Keys.Count.Should().Be(expectedDtmis.Length); foreach (var id in expectedDtmis) { result.Should().ContainKey(id); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[id]).Should().Be(id); } }
public async Task GetModelsMultipleDtmisWithDepsFromExtends() { const string dtmi1 = "dtmi:com:example:TemperatureController;1"; const string dtmi2 = "dtmi:com:example:ConferenceRoom;1"; const string expectedDeps = "dtmi:com:example:Thermostat;1,dtmi:azure:DeviceManagement:DeviceInformation;1,dtmi:com:example:Room;1"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); IDictionary <string, string> result = await client.GetModelsAsync(new[] { dtmi1, dtmi2 }); var expectedDtmis = $"{dtmi1},{dtmi2},{expectedDeps}".Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); result.Keys.Count.Should().Be(expectedDtmis.Length); foreach (var id in expectedDtmis) { result.Should().ContainKey(id); ModelsRepositoryTestBase.ParseRootDtmiFromJson(result[id]).Should().Be(id); } }