|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using PowerDms.Api.Rest.Client; |
| 6 | +using PowerDms.Api.Rest.Dto; |
| 7 | + |
| 8 | +namespace DemoConsoleApp |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + // because there is no async Main until c# 7 |
| 15 | + try |
| 16 | + { |
| 17 | + DoDemo().Wait(); |
| 18 | + } |
| 19 | + catch (Exception e) |
| 20 | + { |
| 21 | + Console.WriteLine(e); |
| 22 | + throw; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static async Task DoDemo() |
| 27 | + { |
| 28 | + // do not check in credentials, copy the sample appsettings.json file here |
| 29 | + var appsettingsPath = "c:\\temp"; |
| 30 | + var configuration = new ConfigurationBuilder() |
| 31 | + .SetBasePath(appsettingsPath) |
| 32 | + .AddJsonFile("appsettings.json") |
| 33 | + .Build(); |
| 34 | + |
| 35 | + var credentials = |
| 36 | + new Credentials |
| 37 | + { |
| 38 | + SiteKey = configuration["Credentials:SiteKey"], |
| 39 | + Username = configuration["Credentials:Username"], |
| 40 | + Password = configuration["Credentials:Password"], |
| 41 | + ClientSecret = configuration["Credentials:ClientSecret"] |
| 42 | + }; |
| 43 | + |
| 44 | + var requestManager = |
| 45 | + new HttpRequestManagerBuilder() |
| 46 | + .SetApiHost(configuration["Host"]) // only needed if not using public PowerDMS API |
| 47 | + .SetOAuthHost(configuration["Host"]) // only needed if not using public PowerDMS API |
| 48 | + .Build(credentials); |
| 49 | + |
| 50 | + var newGroup = new GroupDto |
| 51 | + { |
| 52 | + Name = "ApiCreated" + Guid.NewGuid() |
| 53 | + }; |
| 54 | + |
| 55 | + var createdGroup = await NewGroup(requestManager, newGroup); |
| 56 | + |
| 57 | + Console.WriteLine($" ID: {createdGroup.Id}"); |
| 58 | + Console.WriteLine($" Name: {createdGroup.Name}"); |
| 59 | + |
| 60 | + var getItAgain = await GetGroup(requestManager, createdGroup.Id); |
| 61 | + |
| 62 | + var getBogusOne = await GetGroup(requestManager, "0"); |
| 63 | + |
| 64 | + Console.WriteLine("-- press any key to exit --"); |
| 65 | + Console.ReadKey(); |
| 66 | + } |
| 67 | + |
| 68 | + private static async Task<GroupDto> GetGroup(HttpRequestManager requestManager, string groupId) |
| 69 | + { |
| 70 | + // I don't like the fact credentials need to be set on each builder, why not in the manager? |
| 71 | + var requestBuilder = requestManager.PowerDmsRestApiClient.Groups |
| 72 | + .GetGroupRequestBuilder(groupId); |
| 73 | + |
| 74 | + var result = await requestManager |
| 75 | + .SendAsync(requestBuilder) |
| 76 | + .AwaitGetServiceResponse<GroupDto>(); |
| 77 | + |
| 78 | + if (!result.IsSuccessful) |
| 79 | + { |
| 80 | + Console.WriteLine($"-- Error! Code: {result.Error.Code}, Message: {result.Error.Messages?.FirstOrDefault()} --"); |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + return result.Data; |
| 85 | + } |
| 86 | + |
| 87 | + private static async Task<GroupDto> NewGroup(HttpRequestManager requestManager, GroupDto groupDto) |
| 88 | + { |
| 89 | + var requestBuilder = requestManager.PowerDmsRestApiClient.Groups |
| 90 | + .PostGroupRequestBuilder(groupDto); |
| 91 | + |
| 92 | + var result = await requestManager |
| 93 | + .SendAsync(requestBuilder) |
| 94 | + .AwaitGetServiceResponse<GroupDto>(); |
| 95 | + |
| 96 | + if (!result.IsSuccessful) |
| 97 | + { |
| 98 | + Console.WriteLine($"-- Error! Code: {result.Error.Code}, Message: {result.Error.Messages?.FirstOrDefault()} --"); |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + return result.Data; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments