Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples(adt): Update ctor snippets, and update sample options #12409

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task RunSamplesAsync()
await DigitalTwinsClient.CreateDigitalTwinAsync(twinId, twinPayload).ConfigureAwait(false);
Console.WriteLine($"Created digital twin {twinId}.");

#region Snippet:DigitalTwinSampleUpdateComponent
#region Snippet:DigitalTwinsSampleUpdateComponent
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved

// Update Component with replacing property value
string propertyPath = "/ComponentProp1";
Expand All @@ -62,17 +62,17 @@ public async Task RunSamplesAsync()

Response<string> response = await DigitalTwinsClient.UpdateComponentAsync(twinId, SamplesConstants.ComponentPath, componentUpdateUtility.Serialize());

#endregion Snippet:DigitalTwinSampleUpdateComponent
#endregion Snippet:DigitalTwinsSampleUpdateComponent

Console.WriteLine($"Updated component for digital twin {twinId}. Update response status: {response.GetRawResponse().Status}");

// Get Component

#region Snippet:DigitalTwinSampleGetComponent
#region Snippet:DigitalTwinsSampleGetComponent

response = await DigitalTwinsClient.GetComponentAsync(twinId, SamplesConstants.ComponentPath).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleGetComponent
#endregion Snippet:DigitalTwinsSampleGetComponent

Console.WriteLine($"Get component for digital twin: \n{response.Value}. Get response status: {response.GetRawResponse().Status}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ public async Task GetAllModelsAsync()
{
// Get all the twin types

#region Snippet:DigitalTwinSampleGetModels
#region Snippet:DigitalTwinsSampleGetModels

AsyncPageable<ModelData> allModels = DigitalTwinsClient.GetModelsAsync();
await foreach (ModelData model in allModels)
{
Console.WriteLine($"Model Id: {model.Id}, display name: {model.DisplayName["en"]}, upload time: {model.UploadTime}, is decommissioned: {model.Decommissioned}");
}

#endregion Snippet:DigitalTwinSampleGetModels
#endregion Snippet:DigitalTwinsSampleGetModels
}
catch (Exception ex)
{
Expand All @@ -202,11 +202,11 @@ public async Task DeleteTwinsAsync()
{
// Delete all relationships

#region Snippet:DigitalTwinSampleGetRelationships
#region Snippet:DigitalTwinsSampleGetRelationships

AsyncPageable<string> relationships = DigitalTwinsClient.GetRelationshipsAsync(twin.Key);

#endregion Snippet:DigitalTwinSampleGetRelationships
#endregion Snippet:DigitalTwinsSampleGetRelationships

await foreach (var relationshipJson in relationships)
{
Expand All @@ -217,11 +217,11 @@ public async Task DeleteTwinsAsync()

// Delete any incoming relationships

#region Snippet:DigitalTwinSampleGetIncomingRelationships
#region Snippet:DigitalTwinsSampleGetIncomingRelationships

AsyncPageable<IncomingRelationship> incomingRelationships = DigitalTwinsClient.GetIncomingRelationshipsAsync(twin.Key);

#endregion Snippet:DigitalTwinSampleGetIncomingRelationships
#endregion Snippet:DigitalTwinsSampleGetIncomingRelationships

await foreach (IncomingRelationship incomingRelationship in incomingRelationships)
{
Expand All @@ -231,11 +231,11 @@ public async Task DeleteTwinsAsync()

// Now the digital twin should be safe to delete

#region Snippet:DigitalTwinSampleDeleteTwin
#region Snippet:DigitalTwinsSampleDeleteTwin

await DigitalTwinsClient.DeleteDigitalTwinAsync(twin.Key).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleDeleteTwin
#endregion Snippet:DigitalTwinsSampleDeleteTwin

Console.WriteLine($"Deleted digital twin {twin.Key}");
}
Expand Down Expand Up @@ -263,11 +263,11 @@ public async Task CreateAllTwinsAsync()
{
try
{
#region Snippet:DigitalTwinSampleCreateTwin
#region Snippet:DigitalTwinsSampleCreateTwin

Response<string> response = await DigitalTwinsClient.CreateDigitalTwinAsync(twin.Key, twin.Value).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleCreateTwin
#endregion Snippet:DigitalTwinsSampleCreateTwin

Console.WriteLine($"Created digital twin {twin.Key}. Create response status: {response.GetRawResponse().Status}");
Console.WriteLine($"Body: {response?.Value}");
Expand All @@ -287,7 +287,7 @@ public async Task QueryTwinsAsync()
{
Console.WriteLine("Making a twin query and iterating over the results.");

#region Snippet:DigitalTwinSampleQueryTwins
#region Snippet:DigitalTwinsSampleQueryTwins

// This code snippet demonstrates the simplest way to iterate over the digital twin results, where paging
// happens under the covers.
Expand All @@ -302,11 +302,11 @@ public async Task QueryTwinsAsync()
Console.WriteLine($"Found digital twin: {twin.Id}");
}

#endregion Snippet:DigitalTwinSampleQueryTwins
#endregion Snippet:DigitalTwinsSampleQueryTwins

Console.WriteLine("Making a twin query, with query-charge header extraction.");

#region Snippet:DigitalTwinSampleQueryTwinsWithQueryCharge
#region Snippet:DigitalTwinsSampleQueryTwinsWithQueryCharge

// This code snippet demonstrates how you could extract the query charges incurred when calling
// the query API. It iterates over the response pages first to access to the query-charge header,
Expand Down Expand Up @@ -335,7 +335,7 @@ public async Task QueryTwinsAsync()
}
}

#endregion Snippet:DigitalTwinSampleQueryTwinsWithQueryCharge
#endregion Snippet:DigitalTwinsSampleQueryTwinsWithQueryCharge
}
catch (Exception ex)
{
Expand Down Expand Up @@ -363,7 +363,7 @@ public async Task ConnectTwinsTogetherAsync()
{
try
{
#region Snippet:DigitalTwinSampleCreateRelationship
#region Snippet:DigitalTwinsSampleCreateRelationship

string serializedRelationship = JsonSerializer.Serialize(relationship);

Expand All @@ -374,7 +374,7 @@ await DigitalTwinsClient
serializedRelationship)
.ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleCreateRelationship
#endregion Snippet:DigitalTwinsSampleCreateRelationship

Console.WriteLine($"Linked {serializedRelationship}");
}
Expand All @@ -394,15 +394,15 @@ public async Task GetEventRoutes()
PrintHeader("LISTING EVENT ROUTES");
try
{
#region Snippet:DigitalTwinSampleGetEventRoutes
#region Snippet:DigitalTwinsSampleGetEventRoutes

AsyncPageable<EventRoute> response = DigitalTwinsClient.GetEventRoutesAsync();
await foreach (EventRoute er in response)
{
Console.WriteLine($"Event route: {er.Id}, endpoint name: {er.EndpointName}");
}

#endregion Snippet:DigitalTwinSampleGetEventRoutes
#endregion Snippet:DigitalTwinsSampleGetEventRoutes
}
catch (Exception ex)
{
Expand All @@ -418,7 +418,7 @@ public async Task CreateEventRoute()
PrintHeader("CREATE EVENT ROUTE");
try
{
#region Snippet:DigitalTwinSampleCreateEventRoute
#region Snippet:DigitalTwinsSampleCreateEventRoute

string eventFilter = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
var eventRoute = new EventRoute(_eventhubEndpointName)
Expand All @@ -428,7 +428,7 @@ public async Task CreateEventRoute()

Response createEventRouteResponse = await DigitalTwinsClient.CreateEventRouteAsync(_eventRouteId, eventRoute).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleCreateEventRoute
#endregion Snippet:DigitalTwinsSampleCreateEventRoute

Console.WriteLine($"Created event route: {_eventRouteId} Response status: {createEventRouteResponse.Status}");
}
Expand All @@ -446,11 +446,11 @@ public async Task DeleteEventRoute()
PrintHeader("DELETING EVENT ROUTE");
try
{
#region Snippet:DigitalTwinSampleDeleteEventRoute
#region Snippet:DigitalTwinsSampleDeleteEventRoute

Response response = await DigitalTwinsClient.DeleteEventRouteAsync(_eventRouteId).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleDeleteEventRoute
#endregion Snippet:DigitalTwinsSampleDeleteEventRoute

Console.WriteLine($"Successfully deleted event route: {_eventRouteId}, status: {response.Status}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public async Task RunSamplesAsync()

try
{
#region Snippet:DigitalTwinSampleCreateModels
#region Snippet:DigitalTwinsSampleCreateModels

Response<IReadOnlyList<ModelData>> response = await DigitalTwinsClient.CreateModelsAsync(new[] { newComponentModelPayload, newModelPayload }).ConfigureAwait(false);
Console.WriteLine($"Successfully created a model with Id: {newComponentModelId}, {sampleModelId}, status: {response.GetRawResponse().Status}");

#endregion Snippet:DigitalTwinSampleCreateModels
#endregion Snippet:DigitalTwinsSampleCreateModels
}
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.Conflict)
{
Expand All @@ -66,11 +66,11 @@ public async Task RunSamplesAsync()
// Get Model
try
{
#region Snippet:DigitalTwinSampleGetModel
#region Snippet:DigitalTwinsSampleGetModel

Response<ModelData> sampleModel = await DigitalTwinsClient.GetModelAsync(sampleModelId).ConfigureAwait(false);

#endregion Snippet:DigitalTwinSampleGetModel
#endregion Snippet:DigitalTwinsSampleGetModel

Console.WriteLine($"{sampleModel.Value.Id} has decommission status of {sampleModel.Value.Decommissioned}");
}
Expand All @@ -81,7 +81,7 @@ public async Task RunSamplesAsync()

// Now we decommission the model

#region Snippet:DigitalTwinSampleDecommisionModel
#region Snippet:DigitalTwinsSampleDecommisionModel

try
{
Expand All @@ -94,11 +94,11 @@ public async Task RunSamplesAsync()
FatalError($"Failed to decommision model {sampleModelId} due to:\n{ex}");
}

#endregion Snippet:DigitalTwinSampleDecommisionModel
#endregion Snippet:DigitalTwinsSampleDecommisionModel

// Now delete created model

#region Snippet:DigitalTwinSampleDeleteModel
#region Snippet:DigitalTwinsSampleDeleteModel

try
{
Expand All @@ -111,7 +111,7 @@ public async Task RunSamplesAsync()
FatalError($"Failed to delete model {sampleModelId} due to:\n{ex}");
}

#endregion Snippet:DigitalTwinSampleDeleteModel
#endregion Snippet:DigitalTwinsSampleDeleteModel
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,44 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using CommandLine;

namespace Azure.DigitalTwins.Core.Samples
{
internal enum LoginMethod
{
AppId,
User,
};

public class Options
{
public Options(string adtEndpoint, string clientId, string tenantId, string clientSecret, string eventHubName)
{
AdtEndpoint = adtEndpoint;
ClientId = clientId;
TenantId = tenantId;
ClientSecret = clientSecret;
EventHubName = eventHubName;
}
[Option('a', "adtEndpoint", Required = true, HelpText = "Digital twins service endpoint")]
public string AdtEndpoint { get; set; }

[Option("adtEndpoint", Required = true, HelpText = "Digital twins service endpoint")]
public string AdtEndpoint { get; }
[Option('i', "clientId", Required = true, HelpText = "Client Id of the application Id to login, or the application Id used to log the user in.")]
public string ClientId { get; set; }

[Option("clientId", Required = true, HelpText = "Application client Id")]
public string ClientId { get; }
[Option('m', "loginMethod", Required = false, Default = "AppId", HelpText = "Choose between: AppId, User.")]
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
public string LoginMethod { get; set; }

[Option("tenantId", Required = true, HelpText = "Application tenant Id")]
public string TenantId { get; }
[Option('t', "tenantId", Required = true, HelpText = "Application tenant Id")]
public string TenantId { get; set; }

[Option("clientSecret", Required = true, HelpText = "Application client secret")]
public string ClientSecret { get; }
[Option('s', "clientSecret", Required = false, HelpText = "Application client secret. Only applicable when using LoginMethod of AppId.")]
public string ClientSecret { get; set; }

[Option("eventHubName", Required = true, HelpText = "Event Hub Name linked to digital twins instance")]
public string EventHubName { get; }
[Option('e', "eventHubName", Required = true, HelpText = "Event Hub Name linked to digital twins instance")]
public string EventHubName { get; set; }

public static void HandleParseError(IEnumerable<Error> errors)
internal LoginMethod GetLoginMethod()
{
if (errors.IsVersion())
{
Console.WriteLine("1.0.0");
return;
}

if (errors.IsHelp())
if (Enum.TryParse<LoginMethod>(LoginMethod, out LoginMethod loginMethod))
{
Console.WriteLine("Usage: .\\DigitalTwinServiceClientSample.exe " +
"--adtEndpoint <yourAdtEndpointName> " +
"--clientId <yourApplicationClientId> " +
"--tenantId <yourApplicationTenantId> " +
"--clientSecret <yourApplicationClientSecret>" +
"--eventHubName <yourEventHubName>");
return;
return loginMethod;
}

Console.WriteLine("Parser Fail");
return Samples.LoginMethod.AppId;
}
}
}
Loading