-
Couldn't load subscription status.
- Fork 1
Feature – Contact Events API (Issue #151) #152
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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.ContactEvents/Mailtrap.Example.ContactEvents.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" /> | ||
| <PackageReference Include="System.Text.Json" /> | ||
| </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,79 @@ | ||
| using Mailtrap; | ||
| using Mailtrap.Accounts; | ||
| using Mailtrap.Contacts; | ||
| using Mailtrap.Contacts.Models; | ||
| using Mailtrap.Contacts.Requests; | ||
| using Mailtrap.Contacts.Responses; | ||
| using Mailtrap.ContactEvents; | ||
| using Mailtrap.ContactEvents.Models; | ||
| using Mailtrap.ContactEvents.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 all contacts for account | ||
| IList<Contact> contacts = await contactsResource.GetAll(); | ||
|
|
||
| Contact? contact = contacts.Count > 0 ? contacts[0] : null; | ||
| if (contact is null) | ||
| { | ||
| logger.LogWarning("No contact found. Creating."); | ||
|
|
||
| // Create contact | ||
| var createContactRequest = new CreateContactRequest("email@example.com"); | ||
| createContactRequest.Fields.Add("first_name", "John"); | ||
| createContactRequest.Fields.Add("last_name", "Doe"); | ||
| CreateContactResponse createContactResponse = await contactsResource.Create(createContactRequest); | ||
| contact = createContactResponse.Contact; | ||
| } | ||
| else | ||
| { | ||
| logger.LogInformation("Contact found with Id {ContactId} and Email {ContactEmail}.", contact.Id, contact.Email); | ||
| } | ||
|
|
||
| // Get resource for contact events collection | ||
| IContactsEventCollectionResource contactsEventsResource = contactsResource.Events(contact.Id); | ||
|
|
||
| // Create contacts event | ||
| var createContactsEventRequest = new CreateContactsEventRequest("MyFirstContactsEvent"); | ||
| createContactsEventRequest.Params.Add("user_id", 101); | ||
| createContactsEventRequest.Params.Add("user_name", "John Smith"); | ||
| createContactsEventRequest.Params.Add("is_active", true); | ||
| createContactsEventRequest.Params.Add("last_seen", null); | ||
| ContactsEvent contactsEvent = await contactsEventsResource.Create(createContactsEventRequest); | ||
|
|
||
| logger.LogInformation("Contacts Event created: {Name}", contactsEvent.Name); | ||
| logger.LogInformation("ID: {ContactId}", contactsEvent.ContactId); | ||
| logger.LogInformation("Created At: {ContactEmail}", contactsEvent.ContactEmail); | ||
| foreach (KeyValuePair<string, object?> param in contactsEvent.Params) | ||
| { | ||
| logger.LogInformation("Param: {ParamKey} = {ParamValue}", param.Key, param.Value); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "An error occurred during API call."); | ||
| Environment.ExitCode = 1; | ||
| return; | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
examples/Mailtrap.Example.ContactEvents/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>" | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/Mailtrap.Abstractions/ContactEvents/IContactsEventCollectionResource.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,24 @@ | ||
| namespace Mailtrap.ContactEvents; | ||
|
|
||
| /// <summary> | ||
| /// Represents contacts event collection resource. | ||
| /// </summary> | ||
| public interface IContactsEventCollectionResource : IRestResource | ||
dr-3lo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Creates a new contacts event with details specified by <paramref name="request"/>. | ||
| /// </summary> | ||
| /// | ||
| /// <param name="request"> | ||
| /// Request containing contacts event details for creation. | ||
| /// </param> | ||
| /// | ||
| /// <param name="cancellationToken"> | ||
| /// Token to control operation cancellation. | ||
| /// </param> | ||
| /// | ||
| /// <returns> | ||
| /// Created contacts event details. | ||
| /// </returns> | ||
| public Task<ContactsEvent> Create(CreateContactsEventRequest request, CancellationToken cancellationToken = default); | ||
dr-3lo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/Mailtrap.Abstractions/ContactEvents/Models/ContactsEvent.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,48 @@ | ||
| namespace Mailtrap.ContactEvents.Models; | ||
|
|
||
| /// <summary> | ||
| /// Represents Contacts Event details. | ||
| /// </summary> | ||
| public sealed record ContactsEvent | ||
| { | ||
| /// <summary> | ||
| /// Gets Contact identifier. | ||
| /// </summary> | ||
| /// | ||
| /// <value> | ||
| /// Contact identifier. | ||
| /// </value> | ||
| [JsonPropertyName("contact_id")] | ||
| public string ContactId { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Gets Contact email. | ||
| /// </summary> | ||
| /// | ||
| /// <value> | ||
| /// Contact email. | ||
| /// </value> | ||
| [JsonPropertyName("contact_email")] | ||
| public string ContactEmail { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Gets Contacts Event name. | ||
| /// </summary> | ||
| /// | ||
| /// <value> | ||
| /// Contacts Event name. | ||
| /// </value> | ||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Gets Contacts event params. | ||
| /// </summary> | ||
| /// | ||
| /// <value> | ||
| /// Contacts event params. | ||
| /// </value> | ||
| [JsonPropertyName("params")] | ||
| [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] | ||
| public IDictionary<string, object?> Params { get; } = new Dictionary<string, object?>(); | ||
| } |
84 changes: 84 additions & 0 deletions
84
src/Mailtrap.Abstractions/ContactEvents/Requests/CreateContactsEventRequest.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,84 @@ | ||
| namespace Mailtrap.ContactEvents.Requests; | ||
|
|
||
| /// <summary> | ||
| /// Request object for creating a contacts event. | ||
| /// </summary> | ||
| public sealed record CreateContactsEventRequest : IValidatable | ||
| { | ||
| /// <summary> | ||
| /// Gets contacts event name. | ||
| /// </summary> | ||
| /// | ||
| /// <value> | ||
| /// Contacts event name. | ||
| /// </value> | ||
| [JsonPropertyName("name")] | ||
| [JsonRequired] | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets Contacts event params. | ||
| /// </summary> | ||
| /// | ||
| /// <remarks> | ||
| /// <inheritdoc cref="CreateContactsEventRequest" path="/param[@name=name]"/>. | ||
| /// </remarks> | ||
| /// | ||
| /// <value> | ||
| /// Contacts event params. | ||
| /// </value> | ||
| [JsonPropertyName("params")] | ||
| [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] | ||
| public IDictionary<string, object?> Params { get; } = new Dictionary<string, object?>(); | ||
|
|
||
| /// <summary> | ||
| /// Primary instance constructor. | ||
| /// </summary> | ||
| /// | ||
| /// <param name="name"> | ||
| /// Contact event name. | ||
| /// </param> | ||
| /// | ||
| /// <param name="parameters"> | ||
| /// Contacts event parameters. | ||
| /// </param> | ||
| /// | ||
| /// <remarks> | ||
| /// <paramref name="name"/> must be 1-255 characters long. | ||
| /// </remarks> | ||
| /// | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// When <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>. | ||
| /// </exception> | ||
| public CreateContactsEventRequest(string name, IDictionary<string, object?>? parameters = null) | ||
| { | ||
| Ensure.NotNullOrEmpty(name, nameof(name)); | ||
|
|
||
| Name = name; | ||
|
|
||
| if (parameters is not null) | ||
| { | ||
| // Defensive copy to prevent post-ctor mutation. | ||
| Params = parameters is Dictionary<string, object?> dict | ||
| ? new Dictionary<string, object?>(dict) // defensive copy when already a Dictionary<TKey, TValue> | ||
| : new Dictionary<string, object?>(parameters); // otherwise enumerate once | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parameterless constructor for serializers. | ||
| /// </summary> | ||
| [JsonConstructor] | ||
| public CreateContactsEventRequest() | ||
| { | ||
| Name = string.Empty; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public ValidationResult Validate() | ||
| { | ||
| return CreateContactsEventRequestValidator.Instance | ||
| .Validate(this) | ||
| .ToMailtrapValidationResult(); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/Mailtrap.Abstractions/ContactEvents/Validators/CreateContactsEventRequestValidator.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.ContactEvents.Validators; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Validator for Create contacts event requests.<br /> | ||
| /// Ensures contacts event's Name is not empty and length is within the allowed range.<br/> | ||
| /// Ensures all parameter keys (if any) are non-empty, and within the allowed length. | ||
| /// </summary> | ||
| public sealed class CreateContactsEventRequestValidator : AbstractValidator<CreateContactsEventRequest> | ||
| { | ||
| /// <summary> | ||
| /// Static validator instance for reuse. | ||
| /// </summary> | ||
| public static CreateContactsEventRequestValidator Instance { get; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// Primary constructor. | ||
| /// </summary> | ||
| public CreateContactsEventRequestValidator() | ||
| { | ||
| RuleFor(r => r.Name).NotEmpty().Length(1, 255); | ||
|
|
||
| When(r => r.Params != null && r.Params.Count > 0, () => | ||
| { | ||
| RuleForEach(r => r.Params.Keys) | ||
| .Cascade(CascadeMode.Stop) | ||
| .NotEmpty() | ||
| .MaximumLength(255); | ||
| }); | ||
| } | ||
| } |
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
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
13 changes: 13 additions & 0 deletions
13
src/Mailtrap/ContactEvents/ContactsEventCollectionResource.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,13 @@ | ||
| namespace Mailtrap.ContactEvents; | ||
|
|
||
| /// <summary> | ||
| /// Implementation of Contacts Event Collection resource. | ||
| /// </summary> | ||
| internal sealed class ContactsEventCollectionResource : RestResource, IContactsEventCollectionResource | ||
| { | ||
| public ContactsEventCollectionResource(IRestResourceCommandFactory restResourceCommandFactory, Uri resourceUri) | ||
| : base(restResourceCommandFactory, resourceUri) { } | ||
|
|
||
| public async Task<ContactsEvent> Create(CreateContactsEventRequest request, CancellationToken cancellationToken = default) | ||
| => await Create<CreateContactsEventRequest, ContactsEvent>(request, cancellationToken).ConfigureAwait(false); | ||
| } |
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.