Browse Source

Fix Options pattern

master
Kenneth Bruen 3 months ago
parent
commit
f00b602e71
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 4
      server/Models/ProxySettings.cs
  2. 11
      server/Services/Implementations/DataManager.cs

4
server/Models/ProxySettings.cs

@ -1,7 +1,7 @@
namespace Server.Models; namespace Server.Models;
public record ProxySettings(string Url, ProxyCredentials? Credentials = null) { public record ProxySettings(bool UseProxy, string Url, ProxyCredentials? Credentials = null) {
public ProxySettings() : this("") { } public ProxySettings() : this(false, "") { }
} }
public record ProxyCredentials(string Username, string Password) { public record ProxyCredentials(string Username, string Password) {

11
server/Services/Implementations/DataManager.cs

@ -8,6 +8,7 @@ using InfoferScraper;
using InfoferScraper.Models.Station; using InfoferScraper.Models.Station;
using InfoferScraper.Models.Train; using InfoferScraper.Models.Train;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using scraper.Models.Itinerary; using scraper.Models.Itinerary;
using Server.Models; using Server.Models;
using Server.Services.Interfaces; using Server.Services.Interfaces;
@ -21,15 +22,15 @@ namespace Server.Services.Implementations {
private NodaTime.IDateTimeZoneProvider TzProvider { get; } private NodaTime.IDateTimeZoneProvider TzProvider { get; }
private NodaTime.DateTimeZone CfrTimeZone => TzProvider["Europe/Bucharest"]; private NodaTime.DateTimeZone CfrTimeZone => TzProvider["Europe/Bucharest"];
public DataManager(NodaTime.IDateTimeZoneProvider tzProvider, IDatabase database, ILogger<DataManager> logger, ProxySettings? proxySettings) { public DataManager(NodaTime.IDateTimeZoneProvider tzProvider, IDatabase database, ILogger<DataManager> logger, IOptions<ProxySettings> proxySettings) {
this.TzProvider = tzProvider; this.TzProvider = tzProvider;
this.Database = database; this.Database = database;
this.Logger = logger; this.Logger = logger;
HttpClientHandler httpClientHandler = new (){ HttpClientHandler httpClientHandler = new() {
UseProxy = proxySettings != null, UseProxy = proxySettings.Value.UseProxy,
Proxy = proxySettings == null ? null : new WebProxy(proxySettings.Url), Proxy = proxySettings.Value.UseProxy ? new WebProxy(proxySettings.Value.Url) : null,
DefaultProxyCredentials = proxySettings?.Credentials == null ? null : new NetworkCredential(proxySettings.Credentials.Username, proxySettings.Credentials.Password), DefaultProxyCredentials = string.IsNullOrEmpty(proxySettings.Value.Credentials?.Username) ? null : new NetworkCredential(proxySettings.Value.Credentials.Username, proxySettings.Value.Credentials.Password),
}; };
InfoferScraper.Scrapers.StationScraper stationScraper = new(httpClientHandler); InfoferScraper.Scrapers.StationScraper stationScraper = new(httpClientHandler);
InfoferScraper.Scrapers.TrainScraper trainScraper = new(httpClientHandler); InfoferScraper.Scrapers.TrainScraper trainScraper = new(httpClientHandler);

Loading…
Cancel
Save