-
Notifications
You must be signed in to change notification settings - Fork 1
Feature - Contacts API (#126) #139
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
8 commits
Select commit
Hold shift + click to select a range
bb9a8fe
Feature - Contacts API (#126)
dr-3lo 4c393cf
Feature - Contacts API (#126)
dr-3lo 96ebfb2
Feature - Contacts API (#126)
dr-3lo 8854edd
Feature - Contacts API (#126)
dr-3lo 023377a
Update src/Mailtrap.Abstractions/Contacts/IContactResource.cs
dr-3lo 82ccca3
Feature - Contacts API (#126)
dr-3lo c662a33
Merge branch 'main' into featureI/126-Contacts
dr-3lo 8db4820
Feature - Contacts API (#126)
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.Contact/Mailtrap.Example.Contact.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,81 @@ | ||
using Mailtrap; | ||
using Mailtrap.Accounts; | ||
using Mailtrap.Contacts; | ||
using Mailtrap.Contacts.Models; | ||
using Mailtrap.Contacts.Requests; | ||
using Mailtrap.Contacts.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 contactEmail = "[email protected]"; | ||
|
||
// Get resource for contacts collection | ||
IContactCollectionResource contactsResource = accountResource.Contacts(); | ||
|
||
// Get all contacts for account | ||
IList<Contact> contacts = await contactsResource.GetAll(); | ||
|
||
Contact? contact = contacts | ||
.FirstOrDefault(p => string.Equals(p.Email, contactEmail, StringComparison.OrdinalIgnoreCase)); | ||
|
||
|
||
if (contact is null) | ||
{ | ||
logger.LogWarning("No contact found. Creating."); | ||
|
||
// Create contact | ||
var createContactRequest = new CreateContactRequest(contactEmail); | ||
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."); | ||
} | ||
|
||
// Get resource for specific contact | ||
IContactResource contactResource = accountResource.Contact(contact.Id); | ||
|
||
// Get details | ||
ContactResponse contactResponse = await contactResource.GetDetails(); | ||
Contact contactDetails = contactResponse.Contact; | ||
logger.LogInformation("Contact: {Contact}", contactDetails); | ||
|
||
// Update contact details | ||
var updateContactRequest = new UpdateContactRequest("[email protected]"); | ||
UpdateContactResponse updateContactResponse = await contactResource.Update(updateContactRequest); | ||
Contact updatedContact = updateContactResponse.Contact; | ||
logger.LogInformation("Updated Contact: {Contact}", updatedContact); | ||
|
||
// Delete contact | ||
// Beware that contact resource becomes invalid after deletion and should not be used anymore | ||
await contactResource.Delete(); | ||
logger.LogInformation("Contact Deleted."); | ||
} | ||
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.Contact/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>" | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/Mailtrap.Abstractions/Contacts/Converters/DateTimeToUnixMsJsonConverter.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,34 @@ | ||
namespace Mailtrap.Contacts.Converters; | ||
|
||
/// <summary> | ||
/// Converts DateTimeOffset to Unix time milliseconds for JSON serialization. | ||
/// </summary> | ||
internal sealed class DateTimeToUnixMsNullableJsonConverter : JsonConverter<DateTimeOffset?> | ||
{ | ||
public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.Number) | ||
{ | ||
throw new JsonException($"Expected number for Unix time milliseconds but got {reader.TokenType}."); | ||
} | ||
|
||
var ms = reader.GetInt64(); | ||
return DateTimeOffset.FromUnixTimeMilliseconds(ms); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options) | ||
{ | ||
if (value is null) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
} | ||
|
||
writer.WriteNumberValue(value.Value.ToUnixTimeMilliseconds()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Mailtrap.Abstractions/Contacts/IContactCollectionResource.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,37 @@ | ||
namespace Mailtrap.Contacts; | ||
|
||
/// <summary> | ||
/// Represents Contacts collection resource.. | ||
/// </summary> | ||
public interface IContactCollectionResource : IRestResource | ||
{ | ||
/// <summary> | ||
/// Gets contacts. | ||
/// </summary> | ||
/// | ||
/// <param name="cancellationToken"> | ||
/// Token to control operation cancellation. | ||
/// </param> | ||
/// | ||
/// <returns> | ||
/// Collection of contact details. | ||
/// </returns> | ||
public Task<IList<Contact>> GetAll(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Creates a new contact with details specified by <paramref name="request"/>. | ||
/// </summary> | ||
/// | ||
/// <param name="request"> | ||
/// Request containing contact details for creation. | ||
/// </param> | ||
/// | ||
/// <param name="cancellationToken"> | ||
/// <inheritdoc cref="GetAll(CancellationToken)" path="/param[@name='cancellationToken']"/> | ||
/// </param> | ||
/// | ||
/// <returns> | ||
/// Created contact details. | ||
/// </returns> | ||
public Task<CreateContactResponse> Create(CreateContactRequest request, CancellationToken cancellationToken = default); | ||
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.