-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/125 contact imports #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dr-3lo
wants to merge
6
commits into
main
Choose a base branch
from
feature/125-Contact-Imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c3512f
Feature - Contact Imports API (#125)
dr-3lo 100dc03
Feature - Contact Imports API (#125)
dr-3lo e8d8ecd
Feature - Contact Imports API (#125)
dr-3lo 57b2eed
Feature - Contact Imports API (#125)
dr-z1o 1348709
Feature - Contact Imports API (#125)
dr-3lo db46467
Feature - Contact Imports API (#125)
dr-3lo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
examples/Mailtrap.Example.ContactImports/Mailtrap.Example.ContactImports.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" VersionOverride="9.0.8" /> | ||
<PackageReference Include="System.Text.Json" VersionOverride="9.0.7" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Mailtrap; | ||
using Mailtrap.Accounts; | ||
using Mailtrap.Contacts; | ||
using Mailtrap.Contacts.Requests; | ||
using Mailtrap.ContactImports; | ||
using Mailtrap.ContactImports.Models; | ||
using Mailtrap.ContactImports.Requests; | ||
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); | ||
|
||
// Get resource for contacts collection | ||
IContactCollectionResource contactsResource = accountResource.Contacts(); | ||
|
||
//Get resource for contact imports collection | ||
IContactsImportCollectionResource contactsImportsResource = contactsResource.Imports(); | ||
|
||
// Prepare list of contacts to import | ||
var contactImportList = new List<ContactImportRequest> | ||
{ | ||
new("alice@mailtrap.io"), | ||
new("bob@mailtrap.io"), | ||
new("charlie@mailtrap.io"), | ||
}; | ||
|
||
// Create contacts import request | ||
var importRequest = new ContactsImportRequest(contactImportList); | ||
|
||
// Import contacts in bulk | ||
ContactsImport importResponse = await contactsImportsResource.Create(importRequest); | ||
logger.LogInformation("Created contact import: {Import}", importResponse); | ||
|
||
// Get resource for specific contact import | ||
IContactsImportResource contactsImportResource = contactsResource.Import(importResponse.Id); | ||
|
||
// Get details of specific contact import | ||
ContactsImport contactsImportDetails = await contactsImportResource.GetDetails(); | ||
logger.LogInformation("Contacts Import Details: {Details}", contactsImportDetails); | ||
|
||
if (contactsImportDetails.Status == ContactsImportStatus.Failed) | ||
{ | ||
logger.LogWarning("Import failed!"); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "An error occurred during API call."); | ||
Environment.FailFast(ex.Message); | ||
throw; | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/Mailtrap.Example.ContactImports/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"Project": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"System": "Warning", | ||
"Microsoft": "Warning" | ||
}, | ||
"Debug": { | ||
"LogLevel": { | ||
"Default": "Debug" | ||
} | ||
} | ||
}, | ||
"Mailtrap": { | ||
"ApiToken": "<API_KEY>" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Mailtrap.Abstractions/ContactImports/IContactsImportCollectionResource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Mailtrap.ContactImports; | ||
|
||
/// <summary> | ||
/// Represents Contact imports collection resource. | ||
/// </summary> | ||
public interface IContactsImportCollectionResource : IRestResource | ||
{ | ||
/// <summary> | ||
/// Import contacts in bulk with support for custom fields and list management. | ||
/// Existing contacts with matching email addresses will be updated automatically. | ||
/// You can import up to 50,000 contacts per request. | ||
/// The import process runs asynchronously - use the returned | ||
/// import ID to check the status and results. | ||
/// List all contacts with details into <paramref name="request"/>. | ||
/// </summary> | ||
/// | ||
/// <param name="request"> | ||
/// Request containing contact list for import. | ||
/// </param> | ||
/// | ||
/// <param name="cancellationToken"> | ||
/// Token to control operation cancellation. | ||
/// </param> | ||
/// | ||
/// <returns> | ||
/// Contact import id and status. | ||
/// </returns> | ||
public Task<ContactsImport> Create(ContactsImportRequest request, CancellationToken cancellationToken = default); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Mailtrap.Abstractions/ContactImports/IContactsImportResource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Mailtrap.ContactImports; | ||
|
||
/// <summary> | ||
/// Represents Contact import resource. | ||
/// </summary> | ||
public interface IContactsImportResource : IRestResource | ||
{ | ||
/// <summary> | ||
/// Gets details of the contact import, represented by the current resource instance. | ||
/// </summary> | ||
/// | ||
/// <param name="cancellationToken"> | ||
/// Token to control operation cancellation. | ||
/// </param> | ||
/// | ||
/// <returns> | ||
/// Requested contact import details. | ||
/// </returns> | ||
public Task<ContactsImport> GetDetails(CancellationToken cancellationToken = default); | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Mailtrap.Abstractions/ContactImports/Models/ContactsImport.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Mailtrap.ContactImports.Models; | ||
|
||
/// <summary> | ||
/// Generic response object for contact imports operations. | ||
/// </summary> | ||
public record ContactsImport | ||
{ | ||
/// <summary> | ||
/// Gets created contact imports identifier. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Contact imports identifier. | ||
/// </value> | ||
[JsonPropertyName("id")] | ||
[JsonPropertyOrder(1)] | ||
[JsonRequired] | ||
public long Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets contact imports status. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Contact imports status. | ||
/// </value> | ||
[JsonPropertyName("status")] | ||
[JsonPropertyOrder(2)] | ||
public ContactsImportStatus Status { get; set; } = ContactsImportStatus.Unknown; | ||
|
||
/// <summary> | ||
/// Gets count of created contacts. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Count of created contacts. | ||
/// </value> | ||
[JsonPropertyName("created_contacts_count")] | ||
[JsonPropertyOrder(3)] | ||
public long? CreatedContactsCount { get; set; } | ||
|
||
/// <summary> | ||
/// Gets count of updated contacts. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Count of updated contacts. | ||
/// </value> | ||
[JsonPropertyName("updated_contacts_count")] | ||
[JsonPropertyOrder(4)] | ||
public long? UpdatedContactsCount { get; set; } | ||
|
||
/// <summary> | ||
/// Gets count of contacts over limit. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Count of contacts over limit. | ||
/// </value> | ||
[JsonPropertyName("contacts_over_limit_count")] | ||
[JsonPropertyOrder(5)] | ||
public long? ContactsOverLimitCount { get; set; } | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Mailtrap.Abstractions/ContactImports/Models/ContactsImportStatus.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace Mailtrap.ContactImports.Models; | ||
|
||
|
||
/// <summary> | ||
/// Represents status of the contact imports. | ||
/// </summary> | ||
public sealed record ContactsImportStatus : StringEnum<ContactsImportStatus> | ||
{ | ||
/// <summary> | ||
/// Gets the value representing "created" status. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Represents "created" status. | ||
/// </value> | ||
public static ContactsImportStatus Created { get; } = Define("created"); | ||
|
||
/// <summary> | ||
/// Gets the value representing "started" status. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Represents "started" status. | ||
/// </value> | ||
public static ContactsImportStatus Started { get; } = Define("started"); | ||
|
||
/// <summary> | ||
/// Gets the value representing "finished" status. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Represents "finished" status. | ||
/// </value> | ||
public static ContactsImportStatus Finished { get; } = Define("finished"); | ||
|
||
/// <summary> | ||
/// Gets the value representing "failed" status. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Represents "failed" status. | ||
/// </value> | ||
public static ContactsImportStatus Failed { get; } = Define("failed"); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Mailtrap.Abstractions/ContactImports/Requests/ContactsImportRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Mailtrap.ContactImports.Requests; | ||
|
||
/// <summary> | ||
/// Generic request object for contact CRUD operations. | ||
/// </summary> | ||
public record ContactsImportRequest : IValidatable | ||
{ | ||
/// <summary> | ||
/// Gets contact collection for import. | ||
/// </summary> | ||
/// | ||
/// <value> | ||
/// Contact collection for import. | ||
/// </value> | ||
[JsonPropertyName("contacts")] | ||
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] | ||
public IList<ContactImportRequest> Contacts { get; } = []; | ||
|
||
/// <summary> | ||
/// Primary instance constructor. | ||
/// </summary> | ||
/// | ||
/// <param name="contacts"> | ||
/// Collection of contacts to import. | ||
/// </param> | ||
/// | ||
/// /// <remarks> | ||
/// Each contact in the <paramref name="contacts"/> must include a valid email. | ||
/// Size and item-level constraints are validated by <see cref="Validate"/>. | ||
/// </remarks> | ||
/// | ||
/// <exception cref="ArgumentNullException"> | ||
/// When <paramref name="contacts"/> is <see langword="null"/> or empty. | ||
/// </exception> | ||
/// <para> | ||
/// Use <see cref="Validate"/> to ensure the count is within the allowed range | ||
/// (currently 1–50,000) and each contact satisfies per-item rules. | ||
/// </para> | ||
public ContactsImportRequest(IEnumerable<ContactImportRequest> contacts) | ||
{ | ||
Ensure.NotNullOrEmpty(contacts, nameof(contacts)); | ||
|
||
// Defensive copy to prevent post-ctor mutation. | ||
Contacts = contacts is List<ContactImportRequest> list | ||
? new List<ContactImportRequest>(list) // defensive copy when already a List<T> | ||
: new List<ContactImportRequest>(contacts); // otherwise enumerate once | ||
} | ||
|
||
/// <summary> | ||
/// Parameterless instance constructor for serializers. | ||
/// </summary> | ||
public ContactsImportRequest() { } | ||
|
||
/// <inheritdoc/> | ||
public ValidationResult Validate() | ||
{ | ||
return ContactsImportRequestValidator.Instance | ||
.Validate(this) | ||
.ToMailtrapValidationResult(); | ||
} | ||
dr-3lo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/Mailtrap.Abstractions/ContactImports/Validators/ContactsImportRequestValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Mailtrap.ContactImports.Validators; | ||
|
||
|
||
/// <summary> | ||
/// Validator for Create/Update contact requests.<br /> | ||
/// Ensures contact's email is not empty and length is within the allowed range. | ||
/// </summary> | ||
internal sealed class ContactsImportRequestValidator : AbstractValidator<ContactsImportRequest> | ||
{ | ||
public const int MaxContactsPerRequest = 50_000; | ||
public const int MinContactsPerRequest = 1; | ||
/// <summary> | ||
/// Static validator instance for reuse. | ||
/// </summary> | ||
public static ContactsImportRequestValidator Instance { get; } = new(); | ||
|
||
/// <summary> | ||
/// Primary constructor. | ||
/// </summary> | ||
public ContactsImportRequestValidator() | ||
{ | ||
RuleFor(r => r.Contacts) | ||
.Cascade(CascadeMode.Stop) | ||
.NotEmpty() | ||
.Must(list => list != null && list.Count is >= MinContactsPerRequest and <= MaxContactsPerRequest); | ||
|
||
RuleForEach(r => r.Contacts) | ||
.NotNull() | ||
.SetValidator(ContactRequestValidator.Instance); | ||
} | ||
dr-3lo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.