-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepStoneExtensions.cs
69 lines (66 loc) · 3.28 KB
/
StepStoneExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace Microsoft.Extensions.DependencyInjection
{
using Evolution.StepStone;
using Evolution.StepStone.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Linq;
/// <summary>Extensions to inject various stores for the core service.</summary>
static class StepStoneExtensions
{
/// <summary>Register the StepStone API service for dependency injection.
/// <example>Add a config section for each provider to appsettings.json:
/// <code>
/// "StepStone": [
/// {
/// "Name": "Optional, fallback to Url",
/// "Url": "https://recruiter.{StepStone brand site}",
/// "ClientID": "████████-████-████-████-████████████",
/// "ClientSecret": "████████-████-████-████-████████████",
/// "RecruiterUsername": "████████",
/// "RecruiterPassword": "████████"
/// },
/// ...
/// ]</code>
/// <para>The "Name" properties are used in <see cref="IStepStoneService"/> to distinguish between providers.</para>
/// Pass the whole section:
/// <code>
/// public class Startup { ...
/// ConfigureServices(IServiceCollection services) { ...
/// services.AddStepStoneService("App Name", this.Configuration.GetSection("StepStone"));
/// </code>
/// </example>
/// </summary>
/// <param name="services">The service collection to extend.</param>
/// <param name="application">An application name to pass with requests.</param>
/// <param name="config">The config section for the StepStone brands available.</param>
/// <returns>The expanded service</returns>
public static IServiceCollection AddStepStoneService(this IServiceCollection services, string application, IConfigurationSection config)
{
if (config == null)
return services;
// Parse config services and exclude any incomplete
var settings =
from c in config.GetChildren()
let s = new ServiceSettings
{
Name = c["Name"] ?? c["Url"],
Url = c["Url"],
ClientID = c["ClientID"],
ClientSecret = c["ClientSecret"],
RecruiterUsername = c["RecruiterUsername"],
RecruiterPassword = c["RecruiterPassword"]
}
where
!string.IsNullOrEmpty(s.Url) &&
!string.IsNullOrEmpty(s.ClientID) &&
!string.IsNullOrEmpty(s.ClientSecret) &&
!string.IsNullOrEmpty(s.RecruiterUsername) &&
!string.IsNullOrEmpty(s.RecruiterPassword)
select s;
// Add StepStone.co.uk API service
return services.AddSingleton<IStepStoneService>(
sp => new StepStoneService(sp.GetService<ILoggerFactory>(), application ?? "Evolution", settings.ToArray()));
}
}
}