Skip to content
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

Alternative approach. Use IHttpClientFactory for MailGunSender #382

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Senders/FluentEmail.Mailgun/FluentEmail.Mailgun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using FluentEmail.Core.Interfaces;
using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using FluentEmail.Core.Interfaces;
using FluentEmail.Mailgun;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -8,7 +12,28 @@ public static class FluentEmailMailgunBuilderExtensions
{
public static FluentEmailServicesBuilder AddMailGunSender(this FluentEmailServicesBuilder builder, string domainName, string apiKey, MailGunRegion mailGunRegion = MailGunRegion.USA)
{
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailgunSender(domainName, apiKey, mailGunRegion)));
builder.Services.AddHttpClient(nameof(MailgunSender),
client =>
{
string url = string.Empty;
switch (mailGunRegion)
{
case MailGunRegion.USA:
url = $"https://api.mailgun.net/v3/{domainName}/";
break;
case MailGunRegion.EU:
url = $"https://api.eu.mailgun.net/v3/{domainName}/";
break;

default:
throw new ArgumentException($"'{mailGunRegion}' is not a valid value for {nameof(mailGunRegion)}");
}

client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{apiKey}")));
});

builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender, MailgunSender>());
return builder;
}
}
Expand Down
35 changes: 5 additions & 30 deletions src/Senders/FluentEmail.Mailgun/MailgunSender.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentEmail.Core;
Expand All @@ -15,33 +12,11 @@ namespace FluentEmail.Mailgun
{
public class MailgunSender : ISender
{
private readonly string _apiKey;
private readonly string _domainName;
private HttpClient _httpClient;
private readonly HttpClient _httpClient;

public MailgunSender(string domainName, string apiKey, MailGunRegion mailGunRegion = MailGunRegion.USA)
{
_domainName = domainName;
_apiKey = apiKey;
string url = string.Empty;
switch(mailGunRegion)
{
case MailGunRegion.USA:
url = $"https://api.mailgun.net/v3/{_domainName}/";
break;
case MailGunRegion.EU:
url = $"https://api.eu.mailgun.net/v3/{_domainName}/";
break;

default:
throw new ArgumentException($"'{mailGunRegion}' is not a valid value for {nameof(mailGunRegion)}");
}
_httpClient = new HttpClient
{
BaseAddress = new Uri(url)
};

_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{_apiKey}")));
public MailgunSender(IHttpClientFactory httpClientfactory)
{
_httpClient = httpClientfactory.CreateClient(nameof(MailgunSender));
}

public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
Expand Down
1 change: 1 addition & 0 deletions test/FluentEmail.Core.Tests/FluentEmail.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="3.13.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<ProjectReference Include="..\..\src\Senders\FluentEmail.Mailtrap\FluentEmail.Mailtrap.csproj" />
Expand Down
4 changes: 3 additions & 1 deletion test/FluentEmail.Core.Tests/MailgunSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using FluentEmail.Core.Models;
using NUnit.Framework;
using Newtonsoft.Json;
using System.Net.Http;
using Moq;

namespace FluentEmail.Mailgun.Tests
{
Expand All @@ -17,7 +19,7 @@ public class MailgunSenderTests
[SetUp]
public void SetUp()
{
var sender = new MailgunSender("sandboxcf5f41bbf2f84f15a386c60e253b5fe9.mailgun.org", "key-8d32c046d7f14ada8d5ba8253e3e30de");
var sender = new MailgunSender(Mock.Of<IHttpClientFactory>());
Email.DefaultSender = sender;
}

Expand Down