/// <summary> /// The main demo method. /// </summary> /// <returns>Does not return anything.</returns> private static async Task Run() { try { // Create an authentication context based on the values in the config. var authCtx = new AuthenticationContext(new ClientCredential(Config.ClientId, Config.ClientKey, "TC.SDK.Example"), new TokenCache()) { AuthorityUri = new Uri(Config.AuthorityUrl) }; Console.WriteLine("Acquiring TID token..."); var token = await authCtx.AcquireTokenAsync(); // First create a credentials based on the previously created authentication context. // A single credentials provider can be used to create multiple service clients. ICredentialsProvider credentialsProvider = new AuthCodeCredentialsProvider(authCtx); // Create the usage examples demo instance, which internally initializes the Organizer and Property Set clients. UsageExamplesDemo examplesDemo = new UsageExamplesDemo(credentialsProvider); // Run the usage examples demo... await examplesDemo.Run().ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e); throw; } Console.WriteLine("Done."); }
/// <summary> /// The main demo method. /// </summary> private static async void Run() { try { Console.WriteLine("Starting CDM services demo..."); Console.WriteLine(); // Create an authentication context based on the values in the config. var authCtx = new AuthenticationContext(new ClientCredential(Config.ClientId, Config.ClientKey, "TC.SDK.Example"), new TokenCache()) { AuthorityUri = new Uri(Config.AuthorityUrl) }; Console.WriteLine("Acquiring TID token..."); var token = await authCtx.AcquireTokenAsync(); Console.WriteLine("Initializing CDM services clients..."); Console.WriteLine(); // First create a credentials based on the previously created authentication context. // A single credentials provider can be used to create multiple service clients. ICredentialsProvider credentialsProvider = new AuthCodeCredentialsProvider(authCtx); // Create the Organizer demo instance, which internally initializes the Organizer client. OrgDemo orgDemo = new OrgDemo(credentialsProvider); // Create the Property Set demo instance, which internally initializes the Property Set client. PSetDemo psetDemo = new PSetDemo(credentialsProvider); // Run the Organizer service .NET SDK demo... await orgDemo.Run().ConfigureAwait(false); // Run the Property Set service .NET SDK demo... await psetDemo.Run().ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e); throw; } Console.WriteLine("Finished CDM services demo."); }
/// <summary> /// Implements the main demo functionality. /// </summary> private static async void Run() { var authCtx = new AuthenticationContext(ClientCredentials, new TokenCache()) { AuthorityUri = new Uri(AuthorityUri) }; ICredentialsProvider credentialsProvider = new AuthCodeCredentialsProvider(authCtx); try { Console.WriteLine("Acquiring TID token..."); var token = await authCtx.AcquireTokenAsync(); using (var client = new TrimbleConnectClient(new TrimbleConnectClientConfig { ServiceURI = new Uri(ServiceUri) }, credentialsProvider)) { Console.WriteLine("Logging in to TCPS as {0}...", token.UserInfo.DisplayableId); Console.WriteLine("Projects:"); var projects = (await client.GetProjectsAsync()).ToArray(); foreach (var p in projects) { Console.WriteLine($"\t{p.Name}"); } var project = projects.FirstOrDefault(); if (project == null) { Console.WriteLine("You have no projects."); return; } Console.WriteLine("Selected project: " + project.Name); // get project specific wrapper var projectClient = await client.GetProjectClientAsync(project); // ---------- // working with todos // ---------- Console.WriteLine("Issues:"); var todos = await projectClient.Todos.GetAllAsync(); foreach (var todo in todos) { Console.WriteLine("\t" + todo.Label + " : " + todo.Description); } // ---------- // working with files // ---------- Console.WriteLine("Root folder content:"); var files = (await projectClient.Files.GetFolderItemsAsync(project.RootFolderIdentifier)).ToArray(); foreach (var f in files) { Console.WriteLine("\t" + f.Name + " : " + f.Size + " bytes"); } var file = files.FirstOrDefault(f => f.Type == EntityType.File); if (file == null) { Console.WriteLine("No files in the root folder."); } else { // ---------- // downloading file content // ---------- Console.Write("Downloading '{0}'.", file.Name); using (var stream = await projectClient.Files.DownloadAsync( file.Identifier, progress: args => { Console.Write("."); })) { var destPath = Path.GetTempFileName(); using (var destination = File.Create(destPath)) { await stream.CopyToAsync(destination); } } Console.WriteLine("finished."); } // ---------- // working with the PSet service // ---------- // we have a well known library in the Europe region, so we work with the European service region specifically. using (var psetClient = new PSetClient(new PSetClientConfig { Region = "europe" }, credentialsProvider)) { try { // Get a well known PSet definition var getDefinitionRequest = new GetDefinitionRequest { LibraryId = WellKnonwLibraryID, DefinitionId = WellKnonwDefinitionID, }; Definition definition = await psetClient.GetDefinitionAsync(getDefinitionRequest).ConfigureAwait(false); Console.WriteLine($"Got PSet definition: {definition.Id}"); // List the PSet instances that exist for the definition and are accessible by the user var listAllPSetsRequest = new ListPSetsRequest { LibraryId = definition.LibraryId, DefinitionId = definition.Id, }; var allPSets = new List <PSet>(); await psetClient.ListAllPSetsAsync( listAllPSetsRequest, (PSetsPage psetsPage) => { if (psetsPage != null && psetsPage.Items != null) { allPSets.AddRange(psetsPage.Items); } }).ConfigureAwait(false); if (allPSets.Count > 0) { Console.WriteLine("Got PSets:"); foreach (var pset in allPSets) { Console.WriteLine($"LibID={pset.LibraryId} DefID={pset.DefinitionId} Link={pset.Link}"); } } else { Console.WriteLine("No PSets found."); } } catch (InvalidServiceOperationException e) { Console.WriteLine($"PSet service error: {e.ErrorMessage}"); } } } } catch (Exception e) { Console.WriteLine(e); throw; } Console.WriteLine("Done."); }