Skip to content

Commit d67bc17

Browse files
author
Ruben Bisharyan
committed
Add AddCommunicator via IServiceCollection with IConfiguration as parameter
1 parent ec7aee0 commit d67bc17

File tree

3 files changed

+182
-3
lines changed

3 files changed

+182
-3
lines changed

Readme.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ Install this NuGet library directly from the IDE package installer or by followi
3131

3232
## 1.3. Setup
3333

34-
To incorporate PandaTech.Communicator into your project, you have a primary method to setup in your `Program.cs`:
34+
To incorporate PandaTech.Communicator into your project, you have 2 primary methods to setup in your `Program.cs`:
3535

36-
`builder.AddCommunicator();`
36+
The first method directly using `WebApplicationBuilder` from which `IConfiguration` is directly used. But for the second method builder is not accessible, so we pass `IConfiguration` into it as parameter.
37+
- `builder.AddCommunicator();`
38+
- `services.AddCommunicator(configuration);`
3739

3840
Configuration options can be specified either in `appsettings.json` or directly in `Program.cs`, with the latter taking precedence.
3941

@@ -65,6 +67,7 @@ For each channel can be setup same provider, but it's recommended to have differ
6567

6668
In case of using SSL by setting UseSsl = true use port number 456, otherwise use 587 for non SSL connection.
6769

70+
#### 1.3.1.1. Using `WebApplicationBuilder`
6871
```csharp
6972
builder.AddCommunicator(options =>
7073
{
@@ -129,6 +132,72 @@ builder.AddCommunicator(options =>
129132

130133
```
131134

135+
#### 1.3.1.2. Useing `IServiceCollection`
136+
137+
```csharp
138+
services.AddCommunicator(configuration, options =>
139+
{
140+
options.SmsFake = false;
141+
options.SmsConfigurations = new Dictionary<string, SmsConfiguration>
142+
{
143+
{
144+
"GeneralSender", new SmsConfiguration
145+
{
146+
Provider = "Dexatel",
147+
From = "sender_name",
148+
Properties = new Dictionary<string, string>
149+
{
150+
{ "X-Dexatel-Key", "key" }
151+
},
152+
TimeoutMs = 10000
153+
}
154+
},
155+
{
156+
"TransactionalSender", new SmsConfiguration
157+
{
158+
Provider = "Twilio",
159+
From = "sender_number",
160+
Properties = new Dictionary<string, string>
161+
{
162+
{ "SID", "key" },
163+
{ "AUTH_TOKEN", "token" }
164+
},
165+
TimeoutMs = 10000
166+
}
167+
}
168+
};
169+
options.EmailFake = false;
170+
options.EmailConfigurations = new Dictionary<string, EmailConfiguration>
171+
{
172+
{
173+
"GeneralSender", new EmailConfiguration
174+
{
175+
SmtpServer = "smtp.test.com",
176+
SmtpPort = 465, // 587
177+
SmtpUsername = "test",
178+
SmtpPassword = "test123",
179+
SenderEmail = "[email protected]",
180+
UseSsl = true, // false
181+
TimeoutMs = 10000
182+
}
183+
},
184+
{
185+
"TransactionalSender", new EmailConfiguration
186+
{
187+
SmtpServer = "smtp.gmail.com",
188+
SmtpPort = 465, // 587
189+
SmtpUsername = "vazgen",
190+
SmtpPassword = "vazgen123",
191+
SenderEmail = "[email protected]",
192+
UseSsl = true, // false
193+
TimeoutMs = 10000
194+
}
195+
}
196+
};
197+
});
198+
199+
```
200+
132201
### 1.3.2. Appsettings.json Example
133202

134203
```json

src/Communicator/Communicator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.0.0</Version>
11+
<Version>1.0.1</Version>
1212
<PackageId>Pandatech.Communicator</PackageId>
1313
<Title>SMS and Email Communication helper</Title>
1414
<PackageTags>Pandatech, library, Sms, Email, Messages</PackageTags>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Communicator.Enums;
2+
using Communicator.Helpers;
3+
using Communicator.Options;
4+
using Communicator.Services.Implementations;
5+
using Communicator.Services.Interfaces;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace Communicator.Extensions;
10+
11+
public static class ServiceCollectionExtensions
12+
{
13+
public static IServiceCollection AddCommunicator(this IServiceCollection services,
14+
IConfiguration configuration,
15+
Action<CommunicatorOptions>? setupAction = null)
16+
{
17+
var setupActionOptions = GetCommunicatorSetupOptions(setupAction);
18+
19+
var configurationOptions = GetCommunicatorConfigurationOptions(configuration);
20+
21+
CommunicatorOptions options;
22+
23+
if (setupAction is not null)
24+
{
25+
options = setupActionOptions!;
26+
}
27+
else if (configurationOptions is not null)
28+
{
29+
options = configurationOptions;
30+
}
31+
else
32+
{
33+
throw new Exception("No any Configuration Option setup.");
34+
}
35+
36+
RegisterSmsHttpClientsFromConfig(services, options);
37+
38+
RegisterServices(services, options);
39+
40+
return services;
41+
}
42+
43+
private static CommunicatorOptions? GetCommunicatorConfigurationOptions(IConfiguration configuration)
44+
{
45+
var configurationOptions = CommunicatorConfigurator.ReadConfigurationOptions(configuration);
46+
47+
if (configurationOptions is null)
48+
{
49+
return null;
50+
}
51+
52+
configurationOptions.Validate();
53+
54+
return configurationOptions;
55+
}
56+
57+
private static CommunicatorOptions? GetCommunicatorSetupOptions(Action<CommunicatorOptions>? setupAction = null)
58+
{
59+
var setupOptions = new CommunicatorOptions();
60+
61+
if (setupAction is null)
62+
{
63+
return null;
64+
}
65+
66+
setupAction.Invoke(setupOptions);
67+
setupOptions.Validate();
68+
69+
return setupOptions;
70+
}
71+
72+
private static void RegisterSmsHttpClientsFromConfig(IServiceCollection services,
73+
CommunicatorOptions communicatorOptions)
74+
{
75+
if (communicatorOptions.SmsFake) return;
76+
77+
foreach (var (key, configValue) in communicatorOptions.SmsConfigurations!)
78+
{
79+
services.AddHttpClient(key, client =>
80+
{
81+
client.BaseAddress = new Uri(SmsProviderIntegrations.BaseUrls[configValue.Provider]);
82+
client.Timeout = TimeSpan.FromMilliseconds(configValue.TimeoutMs);
83+
});
84+
}
85+
}
86+
87+
private static void RegisterServices(IServiceCollection services,
88+
CommunicatorOptions communicatorOptions)
89+
{
90+
if (communicatorOptions.EmailFake)
91+
{
92+
services.AddScoped<IEmailService, FakeEmailService>();
93+
}
94+
else
95+
{
96+
services.AddScoped<IEmailService, EmailService>();
97+
}
98+
99+
if (communicatorOptions.SmsFake)
100+
{
101+
services.AddScoped<ISmsService, FakeSmsService>();
102+
}
103+
else
104+
{
105+
services.AddScoped<ISmsService, SmsService>();
106+
}
107+
108+
services.AddSingleton(communicatorOptions);
109+
}
110+
}

0 commit comments

Comments
 (0)