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. 9
      server/Services/Implementations/DataManager.cs

4
server/Models/ProxySettings.cs

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

9
server/Services/Implementations/DataManager.cs

@ -8,6 +8,7 @@ using InfoferScraper;
using InfoferScraper.Models.Station;
using InfoferScraper.Models.Train;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using scraper.Models.Itinerary;
using Server.Models;
using Server.Services.Interfaces;
@ -21,15 +22,15 @@ namespace Server.Services.Implementations {
private NodaTime.IDateTimeZoneProvider TzProvider { get; }
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.Database = database;
this.Logger = logger;
HttpClientHandler httpClientHandler = new() {
UseProxy = proxySettings != null,
Proxy = proxySettings == null ? null : new WebProxy(proxySettings.Url),
DefaultProxyCredentials = proxySettings?.Credentials == null ? null : new NetworkCredential(proxySettings.Credentials.Username, proxySettings.Credentials.Password),
UseProxy = proxySettings.Value.UseProxy,
Proxy = proxySettings.Value.UseProxy ? new WebProxy(proxySettings.Value.Url) : null,
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.TrainScraper trainScraper = new(httpClientHandler);

Loading…
Cancel
Save