Fix Options pattern
This commit is contained in:
parent
441546fb93
commit
f00b602e71
2 changed files with 117 additions and 116 deletions
|
@ -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) {
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue