/// <summary> /// Illustrates how to construct a <see cref="DigitalTwinsClient"/> including client options, /// using the <see cref="ClientSecretCredential"/> implementation of <see cref="Azure.Core.TokenCredential"/>. /// </summary> /// <param name="tenantId">The Id of the tenant of the application Id.</param> /// <param name="clientId">The application Id.</param> /// <param name="clientSecret">A client secret for the application Id.</param> /// <param name="adtEndpoint">The endpoint of the digital twins instance.</param> /// <param name="httpClient">An HttpClient instance for the client to use</param> private static DigitalTwinsClient GetDigitalTwinsClient(string tenantId, string clientId, string clientSecret, string adtEndpoint, HttpClient httpClient) { #region Snippet:DigitalTwinSampleCreateServiceClientWithHttpClient // This illustrates how to specify client options, in this case, by providing an // instance of HttpClient for the digital twins client to use var clientOptions = new DigitalTwinsClientOptions { Transport = new HttpClientTransport(httpClient), }; var clientSecretCredential = new ClientSecretCredential( tenantId, clientId, clientSecret, new TokenCredentialOptions { AuthorityHost = KnownAuthorityHosts.AzureCloud }); var dtClient = new DigitalTwinsClient( new Uri(adtEndpoint), clientSecretCredential, clientOptions); #endregion Snippet:DigitalTwinSampleCreateServiceClientWithHttpClient return(dtClient); }
/// <summary> /// Illustrates how to construct a <see cref="DigitalTwinsClient"/> including client options, /// using the <see cref="InteractiveBrowserCredential"/> implementation of <see cref="Azure.Core.TokenCredential"/>. /// </summary> /// <param name="tenantId">The Id of the tenant of the application Id.</param> /// <param name="clientId">The application Id.</param> /// <param name="adtEndpoint">The endpoint of the digital twins instance.</param> /// <param name="httpClient">An HttpClient instance for the client to use</param> private static DigitalTwinsClient GetDigitalTwinsClient(string tenantId, string clientId, string adtEndpoint, HttpClient httpClient) { #region Snippet:DigitalTwinsSampleCreateServiceClientInteractiveLogin // This illustrates how to specify client options, in this case, by providing an // instance of HttpClient for the digital twins client to use. var clientOptions = new DigitalTwinsClientOptions { Transport = new HttpClientTransport(httpClient), }; // By using the InteractiveBrowserCredential, the current user can login using a web browser // interactively with the AAD var tokenCredential = new InteractiveBrowserCredential( tenantId, clientId, new TokenCredentialOptions { AuthorityHost = KnownAuthorityHosts.AzureCloud }); var dtClient = new DigitalTwinsClient( new Uri(adtEndpoint), tokenCredential, clientOptions); #endregion Snippet:DigitalTwinsSampleCreateServiceClientInteractiveLogin return(dtClient); }
protected DigitalTwinsClient GetClient(DigitalTwinsClientOptions options = null) { if (options == null) { options = new DigitalTwinsClientOptions(); } return(InstrumentClient( new DigitalTwinsClient( new Uri(TestEnvironment.DigitalTwinHostname), TestEnvironment.Credential, InstrumentClientOptions(options)))); }
protected DigitalTwinsClient GetClient(DigitalTwinsClientOptions options = null) { if (options == null) { options = new DigitalTwinsClientOptions() { Retry = { Delay = TimeSpan.Zero, Mode = RetryMode.Fixed } }; } return(InstrumentClient( new DigitalTwinsClient( new Uri(TestEnvironment.DigitalTwinHostname), TestEnvironment.Credential, InstrumentClientOptions(options)))); }
public async Task DigitalTwinOperationsWithCustomObjectSerializer_Succeeds() { // arrange var serializer = new TestObjectSerializer(); DigitalTwinsClientOptions options = new DigitalTwinsClientOptions { Serializer = serializer }; DigitalTwinsClient client = GetClient(options); serializer.WasDeserializeCalled.Should().BeFalse(); serializer.WasSerializeCalled.Should().BeFalse(); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); // create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await CreateAndListModelsAsync(client, new List <string> { roomModel }).ConfigureAwait(false); // act // create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateOrReplaceDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); roomTwin = await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false); // assert roomTwin.Should().NotBeNull(); serializer.WasDeserializeCalled.Should().BeTrue(); serializer.WasSerializeCalled.Should().BeTrue(); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); DigitalTwinsClient client; AsyncPageable <ModelData> modelList; try { ManagedIdentityCredential cred = new ManagedIdentityCredential(adtAppId); DigitalTwinsClientOptions opts = new DigitalTwinsClientOptions { Transport = new HttpClientTransport(httpClient) }; client = new DigitalTwinsClient(new Uri(adtInstanceUrl), cred, opts); modelList = client.GetModelsAsync(null, true); await foreach (ModelData md in modelList) { log.LogInformation($"Id: {md.Id}"); } log.LogInformation("Done"); } catch (Exception e) { log.LogCritical($"Authentication or client creation error: {e.Message}"); return(new BadRequestObjectResult(e.Message)); } return(new OkObjectResult(modelList)); }