Skip to content

Commit 472f1dc

Browse files
committed
PublicAPI. Client app
1 parent 46ae60a commit 472f1dc

12 files changed

Lines changed: 162 additions & 6 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<UserSecretsId>f38081dd-cff7-4d02-b211-8d019ec1c3d3</UserSecretsId>
6+
<RootNamespace>RTUITLab.EmailService.Client</RootNamespace>
7+
<PackageId>RTUITLab.EmailService.Client</PackageId>
8+
<Authors>RTUITLab</Authors>
9+
<Product>EmailService.Client</Product>
10+
<AssemblyName>RTUITLab.EmailService.Client</AssemblyName>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
15+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\PublicAPI\PublicAPI.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System;
3+
4+
namespace RTUITLab.EmailService.Client
5+
{
6+
public static class EmailSenderExtension
7+
{
8+
public static IServiceCollection AddEmailSender(this IServiceCollection services, EmailSenderOptions emailSenderOptions)
9+
{
10+
if (services == null)
11+
{
12+
throw new ArgumentNullException(nameof(services));
13+
}
14+
15+
services.AddHttpClient(HttpEmailSender.HttpClientName, cfg =>
16+
{
17+
cfg.BaseAddress = new Uri(emailSenderOptions.BaseAddress);
18+
cfg.DefaultRequestHeaders.Add("Authorization", emailSenderOptions.Key);
19+
});
20+
return services.AddTransient<IEmailSender, HttpEmailSender>();
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RTUITLab.EmailService.Client
2+
{
3+
public class EmailSenderOptions
4+
{
5+
public string BaseAddress { get; set; }
6+
public string Key { get; set; }
7+
}
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using RTUITLab.EmailService.PublicAPI.Requests;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace RTUITLab.EmailService.Client
10+
{
11+
internal class HttpEmailSender : IEmailSender
12+
{
13+
public const string HttpClientName = nameof(HttpEmailSender) + nameof(HttpClientName);
14+
private readonly HttpClient client;
15+
16+
public HttpEmailSender(IHttpClientFactory httpClientFactory)
17+
{
18+
client = httpClientFactory.CreateClient(HttpClientName);
19+
}
20+
21+
public async Task SendEmailAsync(string email, string subject, string body)
22+
{
23+
try
24+
{
25+
var request = await client.PostAsync("/api/email/send", new StringContent(
26+
JsonConvert.SerializeObject(
27+
new SendEmailRequest
28+
{
29+
Email = email,
30+
Subject = subject,
31+
Body = body
32+
}
33+
), Encoding.UTF8, "application/json")
34+
);
35+
if (request.StatusCode == System.Net.HttpStatusCode.OK)
36+
return;
37+
throw new Exception(await request.Content.ReadAsStringAsync());
38+
}
39+
catch (Exception ex)
40+
{
41+
throw new Exception("Something went wrong while sending email", ex);
42+
}
43+
}
44+
}
45+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace RTUITLab.EmailService.Client
4+
{
5+
public interface IEmailSender
6+
{
7+
Task SendEmailAsync(string email, string subject, string body);
8+
}
9+
}

src/EmailService.sln

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29905.134
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailService", "EmailService\EmailService.csproj", "{B5045CB1-D33E-452C-8B1A-A515CBC0CF67}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmailService", "EmailService\EmailService.csproj", "{B5045CB1-D33E-452C-8B1A-A515CBC0CF67}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Models", "Models\Models.csproj", "{F3C853FD-5509-4803-AEF4-820BF87F640E}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Models", "Models\Models.csproj", "{F3C853FD-5509-4803-AEF4-820BF87F640E}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "EmailService.Client\Client.csproj", "{090F3095-5A51-436B-B539-46041D63E1A8}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PublicAPI", "PublicAPI\PublicAPI.csproj", "{D092C687-8707-4D0C-AD8A-F4F1FB1718F2}"
913
EndProject
1014
Global
1115
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +25,14 @@ Global
2125
{F3C853FD-5509-4803-AEF4-820BF87F640E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2226
{F3C853FD-5509-4803-AEF4-820BF87F640E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2327
{F3C853FD-5509-4803-AEF4-820BF87F640E}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{090F3095-5A51-436B-B539-46041D63E1A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{090F3095-5A51-436B-B539-46041D63E1A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{090F3095-5A51-436B-B539-46041D63E1A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{090F3095-5A51-436B-B539-46041D63E1A8}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{D092C687-8707-4D0C-AD8A-F4F1FB1718F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{D092C687-8707-4D0C-AD8A-F4F1FB1718F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{D092C687-8707-4D0C-AD8A-F4F1FB1718F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{D092C687-8707-4D0C-AD8A-F4F1FB1718F2}.Release|Any CPU.Build.0 = Release|Any CPU
2436
EndGlobalSection
2537
GlobalSection(SolutionProperties) = preSolution
2638
HideSolutionNode = FALSE

src/EmailService/Controllers/EmailController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace EmailService.Controllers
1313
[ApiController]
1414
public class EmailController : ControllerBase
1515
{
16-
private readonly EmailSenderOptions options;
16+
private readonly EmailServiceOptions options;
1717

18-
public EmailController(IOptions<EmailSenderOptions> emailSenderOptions)
18+
public EmailController(IOptions<EmailServiceOptions> emailSenderOptions)
1919
{
2020
this.options = emailSenderOptions.Value;
2121
}

src/EmailService/EmailService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13+
<ProjectReference Include="..\EmailService.Client\Client.csproj" />
1314
<ProjectReference Include="..\Models\Models.csproj" />
1415
</ItemGroup>
1516

src/EmailService/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Hosting;
77
using Models.Options;
8+
using RTUITLab.EmailService.Client;
89

910
namespace EmailService
1011
{
@@ -22,7 +23,11 @@ public void ConfigureServices(IServiceCollection services)
2223
{
2324
services.AddControllers();
2425

25-
services.Configure<EmailSenderOptions>(Configuration.GetSection(nameof(EmailSenderOptions)));
26+
services.Configure<EmailServiceOptions>(Configuration.GetSection(nameof(EmailServiceOptions)));
27+
28+
services.AddEmailSender(Configuration
29+
.GetSection(nameof(EmailSenderOptions))
30+
.Get<EmailSenderOptions>());
2631
}
2732

2833
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Models.Options
22
{
3-
public class EmailSenderOptions
3+
public class EmailServiceOptions
44
{
55
public string Email { get; set; }
66
public string Password { get; set; }

0 commit comments

Comments
 (0)