-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProject.cs
More file actions
75 lines (58 loc) · 2.45 KB
/
Project.cs
File metadata and controls
75 lines (58 loc) · 2.45 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
using Mailtrap;
using Mailtrap.Accounts;
using Mailtrap.Projects;
using Mailtrap.Projects.Models;
using Mailtrap.Projects.Requests;
using Mailtrap.Projects.Responses;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(args);
IConfigurationSection config = hostBuilder.Configuration.GetSection("Mailtrap");
hostBuilder.Services.AddMailtrapClient(config);
using IHost host = hostBuilder.Build();
ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
IMailtrapClient mailtrapClient = host.Services.GetRequiredService<IMailtrapClient>();
try
{
var accountId = 12345;
IAccountResource accountResource = mailtrapClient.Account(accountId);
var projectName = "My Test Project";
// Get resource for projects collection
IProjectCollectionResource projectsResource = accountResource.Projects();
// Get all projects for account
IList<Project> projects = await projectsResource.GetAll();
Project? project = projects
.FirstOrDefault(p => string.Equals(p.Name, projectName, StringComparison.OrdinalIgnoreCase));
if (project is null)
{
logger.LogWarning("No project found. Creating.");
// Create project
var createProjectRequest = new CreateProjectRequest(projectName);
project = await projectsResource.Create(createProjectRequest);
}
else
{
logger.LogInformation("Project found.");
}
// Get resource for specific project
IProjectResource projectResource = accountResource.Project(project.Id);
// Get details
project = await projectResource.GetDetails();
logger.LogInformation("Project: {Project}", project);
// Update project details
var updateProjectRequest = new UpdateProjectRequest("Updated Project Name");
Project updatedProject = await projectResource.Update(updateProjectRequest);
logger.LogInformation("Updated Project: {Project}", updatedProject);
// Delete project
// Beware that project resource becomes invalid after deletion and should not be used anymore
DeleteProjectResponse deletedProject = await projectResource.Delete();
logger.LogInformation("Deleted Project: {Project}", deletedProject);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during API call.");
Environment.FailFast(ex.Message);
throw;
}