-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoCommand.cs
More file actions
138 lines (127 loc) · 7.02 KB
/
Copy pathDemoCommand.cs
File metadata and controls
138 lines (127 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using CommandLine;
using ShapeDiver.SDK.PlatformBackend;
using ShapeDiver.SDK.PlatformBackend.DTO;
using System;
using System.Threading.Tasks;
namespace DotNetSdkSampleConsoleApp.Commands
{
/// <summary>
/// Demo command showing basic usage of the SDK.
/// Makes use of
/// <see cref="https://help.shapediver.com/doc/platform-api-access-keys">Platform API access keys</see>
/// for authentication, or browser based authentication.
/// </summary>
[Verb("demo", isDefault: false, HelpText = "Demo which prints some information about your account.")]
class DemoCommand : BaseCommand, ICommand
{
public async Task Execute()
{
await WrapExceptions(async () =>
{
// get authenticated SDK
var sdk = await GetAuthenticatedSDK();
Console.WriteLine($"{Environment.NewLine}IsAuthenticated: {sdk.AuthenticationClient.IsAuthenticated}");
// get user information
var user = (await sdk.PlatformClient.UserApi.Get<UserDto>(sdk.AuthenticationClient.GetUserId(), UserGetEmbeddableFields.Used_Credits)).Data;
Console.WriteLine();
Console.WriteLine($"User Id: {user.Id}");
Console.WriteLine($"Username: {user.Username}");
Console.WriteLine($"FirstName: {user.FirstName}");
Console.WriteLine($"LastName: {user.LastName}");
Console.WriteLine($"Email: {user.Email}");
Console.WriteLine($"Credits used this month: {user.UsedCredits.UsedCreditsCurrentMonthV2}");
// get detailed information about usage in the past days
Console.WriteLine();
Console.WriteLine($"Usage of credits per day and backend system this month:");
long unixTimeStartOfCurrentMonth = ((DateTimeOffset)new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds();
var analyticsQuery = sdk.PlatformClient.UserCreditMetricsApi.CreateQueryBody(1000, true);
analyticsQuery.AddFilter(ex => ex.Property(d => d.TimestampType).EqualTo(AnalyticsTimestampTypeEnum.Day));
analyticsQuery.AddFilter(ex => ex.Property(d => d.TimestampDate).GreaterOrEqualTo(unixTimeStartOfCurrentMonth));
analyticsQuery.AddFilter(ex => ex.Property(d => d.UserId).EqualTo(user.Id));
analyticsQuery.AddSorter(SorterType.Created_At, SortOrder.Asc);
var analyticsResult = await sdk.PlatformClient.UserCreditMetricsApi.Query(analyticsQuery);
foreach (var dailyStats in analyticsResult.Data.Result)
{
var data = dailyStats.Data;
var credits =
data.Ar.Credits +
data.Default.Combined.Credits +
data.Default.Computations.Credits +
data.Default.Exports.Credits +
data.Default.Outputs.Credits +
data.Limited.Combined.Credits +
data.Limited.Computations.Credits +
data.Limited.Exports.Credits +
data.Limited.Sessions.Credits
;
Console.WriteLine($"{dailyStats.Timestamp}: credits used on system {dailyStats.BackendSystem.Alias}: {credits}");
}
if (analyticsResult.Data.Result.Count == 0)
{
Console.WriteLine("No aggregated analytics found.");
}
// get latest 10 published models
var query = sdk.PlatformClient.ModelApi.CreateQueryBody(10);
query.AddSorter(SorterType.Created_At, SortOrder.Desc);
query.AddFilter(ex => ex.Property(m => m.Status).EqualTo(ModelStatusEnum.Done));
query.AddFilter(ex => ex.Property(m => m.UserId).EqualTo(user.Id));
var result = await sdk.PlatformClient.ModelApi.Query(query);
var models = result.Data.Result;
Console.WriteLine();
if (models.Count == 0)
{
Console.WriteLine("No published models found.");
}
else
{
Console.WriteLine("Latest published models:");
foreach (var model in models)
{
Console.WriteLine($"\tTitle: {model.Title}, Slug: {model.Slug}");
}
}
// get latest model which allows backend access
query = sdk.PlatformClient.ModelApi.CreateQueryBody(1);
query.AddSorter(SorterType.Created_At, SortOrder.Desc);
query.AddFilter(ex => ex.Property(m => m.Status).EqualTo(ModelStatusEnum.Done));
query.AddFilter(ex => ex.Property(m => m.UserId).EqualTo(user.Id));
query.AddFilter(ex => ex.Property(m => m.BackendAccess).EqualTo(true));
query.AddFilter(ex => ex.Property(m => m.DeletedAt).IsNull());
result = await sdk.PlatformClient.ModelApi.Query(query);
models = result.Data.Result;
Console.WriteLine();
if (models.Count == 0)
{
Console.WriteLine("No published models found which allow backend access.");
}
else
{
Console.WriteLine("Latest published model which allows backend access:");
foreach (var model in models)
{
Console.WriteLine($"\tTitle: {model.Title}, Slug: {model.Slug}");
}
// get parameters of latest model
var context = await sdk.GeometryBackendClient.GetSessionContext(models[0].Id, sdk.PlatformClient);
Console.WriteLine();
Console.WriteLine("Parameters and outputs of latest published model which allows backend access:");
Console.WriteLine("Parameters:");
foreach (var param in context.ModelData.Parameters)
{
Console.WriteLine($"\tId: {param.Key}, Name: {param.Value.Name}, Type: {param.Value.Type}");
}
Console.WriteLine("Outputs:");
foreach (var output in context.ModelData.Outputs)
{
Console.WriteLine($"\tId: {output.Key}, Name: {output.Value.Name}, Uid: {output.Value.Uid}");
}
Console.WriteLine("Binary glTF files available:");
foreach (var asset in sdk.GeometryBackendClient.GetAllOutputAssetsForFormat(context, "glb"))
{
Console.WriteLine($"\tOutput Name: {context.ModelData.Outputs[asset.OutputId].Name}, Format: {asset.Format}, Size: {asset.Size}");
}
}
});
}
}
}