|
| 1 | +using Mailtrap; |
| 2 | +using Mailtrap.Accounts; |
| 3 | +using Mailtrap.ApiTokens; |
| 4 | +using Mailtrap.ApiTokens.Models; |
| 5 | +using Mailtrap.ApiTokens.Requests; |
| 6 | +using Mailtrap.ApiTokens.Responses; |
| 7 | +using Mailtrap.Core.Models; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Hosting; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | + |
| 14 | +HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(args); |
| 15 | + |
| 16 | +IConfigurationSection config = hostBuilder.Configuration.GetSection("Mailtrap"); |
| 17 | + |
| 18 | +hostBuilder.Services.AddMailtrapClient(config); |
| 19 | + |
| 20 | +using IHost host = hostBuilder.Build(); |
| 21 | + |
| 22 | +ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>(); |
| 23 | +IMailtrapClient mailtrapClient = host.Services.GetRequiredService<IMailtrapClient>(); |
| 24 | + |
| 25 | +try |
| 26 | +{ |
| 27 | + var accountId = 12345; |
| 28 | + var inboxId = 67890; |
| 29 | + IAccountResource accountResource = mailtrapClient.Account(accountId); |
| 30 | + |
| 31 | + // Get resource for API tokens collection |
| 32 | + IApiTokenCollectionResource apiTokensResource = accountResource.ApiTokens(); |
| 33 | + |
| 34 | + // List all API tokens visible to the current API token |
| 35 | + IList<ApiToken> apiTokens = await apiTokensResource.GetAll(); |
| 36 | + logger.LogInformation("Found {Count} API token(s).", apiTokens.Count); |
| 37 | + |
| 38 | + // Create a new API token scoped to a specific inbox with Viewer access |
| 39 | + var createRequest = new CreateApiTokenRequest |
| 40 | + { |
| 41 | + Name = "Demo Viewer Token" |
| 42 | + }; |
| 43 | + createRequest.Resources.Add(new ApiTokenAccessRequest(ResourceType.Inbox, inboxId, AccessLevel.Viewer)); |
| 44 | + |
| 45 | + CreateApiTokenResponse createdToken = await apiTokensResource.Create(createRequest); |
| 46 | + |
| 47 | + // The full token value is only returned at creation time - store it securely |
| 48 | + logger.LogInformation( |
| 49 | + "Created API Token: Id={Id}, Name={Name}, Last4={Last4}", |
| 50 | + createdToken.Id, |
| 51 | + createdToken.Name, |
| 52 | + createdToken.Last4Digits); |
| 53 | + logger.LogInformation("Full token value (store securely, returned only once): {Token}", createdToken.Token); |
| 54 | + |
| 55 | + // Get resource for the specific API token |
| 56 | + IApiTokenResource apiTokenResource = accountResource.ApiToken(createdToken.Id); |
| 57 | + |
| 58 | + // Get details of the API token |
| 59 | + ApiToken tokenDetails = await apiTokenResource.GetDetails(); |
| 60 | + logger.LogInformation("Token details: Id={Id}, Name={Name}, CreatedBy={CreatedBy}", |
| 61 | + tokenDetails.Id, |
| 62 | + tokenDetails.Name, |
| 63 | + tokenDetails.CreatedBy); |
| 64 | + |
| 65 | + // Reset the API token - expires the current token and returns a new one with the same permissions |
| 66 | + ApiTokenResetResponse resetResponse = await apiTokenResource.Reset(); |
| 67 | + logger.LogInformation( |
| 68 | + "Reset API Token: Id={Id}, NewLast4={Last4}", |
| 69 | + resetResponse.Id, |
| 70 | + resetResponse.Last4Digits); |
| 71 | + logger.LogInformation("New token value (store securely, returned only once): {Token}", resetResponse.Token); |
| 72 | + |
| 73 | + // Delete the API token |
| 74 | + // The API token resource becomes invalid after deletion and should not be used anymore |
| 75 | + await apiTokenResource.Delete(); |
| 76 | + logger.LogInformation("API Token Deleted."); |
| 77 | +} |
| 78 | +catch (Exception ex) |
| 79 | +{ |
| 80 | + logger.LogError(ex, "An error occurred during API call."); |
| 81 | + Environment.ExitCode = 1; |
| 82 | + return; |
| 83 | +} |
0 commit comments