Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
ExchangeRate - Remove obsolete method WebRequest
KrzysztofPajak3286db09001f
diff --git a/src/Plugins/ExchangeRate.McExchange/Constant.cs b/src/Plugins/ExchangeRate.McExchange/Constant.csnew file mode 100644index 000000000..1df5239cb--- /dev/null+++ b/src/Plugins/ExchangeRate.McExchange/Constant.cs@@ -0,0 +1,9 @@+namespace ExchangeRate.McExchange+{+ public static class Constant+ {+ public static string DefaultHttpClientName => "ExchangeRateHttpClient";+ public static string EcbUrl => "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";+ public static string NbpUrl => "http://api.nbp.pl/api/exchangerates/tables/A/";+ }+}diff --git a/src/Plugins/ExchangeRate.McExchange/EcbExchange.cs b/src/Plugins/ExchangeRate.McExchange/EcbExchange.csindex 50aea1fe3..00bd3040d 100644--- a/src/Plugins/ExchangeRate.McExchange/EcbExchange.cs+++ b/src/Plugins/ExchangeRate.McExchange/EcbExchange.cs@@ -2,44 +2,49 @@ using System;using System.Collections.Generic;using System.Globalization;using System.Net;+using System.Net.Http;using System.Threading.Tasks;using System.Xml;-namespace Grand.Plugin.ExchangeRate.McExchange+namespace ExchangeRate.McExchange{internal class EcbExchange : IRateProvider{- public async Task<IList<Domain.Directory.ExchangeRate>> GetCurrencyLiveRates()+ private readonly IHttpClientFactory _httpClientFactory;++ public EcbExchange(IHttpClientFactory httpClientFactory){- var request = (HttpWebRequest)WebRequest.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");- using (var response = await request.GetResponseAsync())- {- var document = new XmlDocument();- document.Load(response.GetResponseStream());- var nsmgr = new XmlNamespaceManager(document.NameTable);- nsmgr.AddNamespace("ns", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");- nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");+ _httpClientFactory = httpClientFactory;+ }- var node = document.SelectSingleNode("gesmes:Envelope/ns:Cube/ns:Cube", nsmgr);- var updateDate = DateTime.ParseExact(node.Attributes["time"].Value, "yyyy-MM-dd", null);+ public async Task<IList<Grand.Domain.Directory.ExchangeRate>> GetCurrencyLiveRates()+ {+ var httpClient = _httpClientFactory.CreateClient(Constant.DefaultHttpClientName);+ using var response = await httpClient.GetStreamAsync(Constant.EcbUrl);+ var document = new XmlDocument();+ document.Load(response);+ var nsmgr = new XmlNamespaceManager(document.NameTable);+ nsmgr.AddNamespace("ns", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");+ nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");++ var node = document.SelectSingleNode("gesmes:Envelope/ns:Cube/ns:Cube", nsmgr);+ var updateDate = DateTime.ParseExact(node.Attributes["time"].Value, "yyyy-MM-dd", null);- var provider = new NumberFormatInfo();- provider.NumberDecimalSeparator = ".";- provider.NumberGroupSeparator = "";+ var provider = new NumberFormatInfo();+ provider.NumberDecimalSeparator = ".";+ provider.NumberGroupSeparator = "";- var exchangeRates = new List<Grand.Domain.Directory.ExchangeRate>();- foreach (XmlNode node2 in node.ChildNodes)- {- exchangeRates.Add(new Domain.Directory.ExchangeRate()- {- CurrencyCode = node2.Attributes["currency"].Value,- Rate = double.Parse(node2.Attributes["rate"].Value, provider),- UpdatedOn = updateDate- }- );+ var exchangeRates = new List<Grand.Domain.Directory.ExchangeRate>();+ foreach (XmlNode node2 in node.ChildNodes)+ {+ exchangeRates.Add(new Grand.Domain.Directory.ExchangeRate() {+ CurrencyCode = node2.Attributes["currency"].Value,+ Rate = double.Parse(node2.Attributes["rate"].Value, provider),+ UpdatedOn = updateDate}- return exchangeRates;+ );}+ return exchangeRates;}}}diff --git a/src/Plugins/ExchangeRate.McExchange/IRateProvider.cs b/src/Plugins/ExchangeRate.McExchange/IRateProvider.csindex 3e210f632..f7a9170f2 100644--- a/src/Plugins/ExchangeRate.McExchange/IRateProvider.cs+++ b/src/Plugins/ExchangeRate.McExchange/IRateProvider.cs@@ -1,10 +1,10 @@using System.Collections.Generic;using System.Threading.Tasks;-namespace Grand.Plugin.ExchangeRate.McExchange+namespace ExchangeRate.McExchange{internal interface IRateProvider{- Task<IList<Domain.Directory.ExchangeRate>> GetCurrencyLiveRates();+ Task<IList<Grand.Domain.Directory.ExchangeRate>> GetCurrencyLiveRates();}}diff --git a/src/Plugins/ExchangeRate.McExchange/McExchangeRatePlugin.cs b/src/Plugins/ExchangeRate.McExchange/McExchangeRatePlugin.csindex b33197bef..f03f98fbb 100644--- a/src/Plugins/ExchangeRate.McExchange/McExchangeRatePlugin.cs+++ b/src/Plugins/ExchangeRate.McExchange/McExchangeRatePlugin.cs@@ -4,7 +4,7 @@ using System.Threading.Tasks;[assembly: InternalsVisibleTo("Grand.Plugin.Tests")]-namespace Grand.Plugin.ExchangeRate.McExchange+namespace ExchangeRate.McExchange{public class McExchangeRatePlugin : BasePlugin{diff --git a/src/Plugins/ExchangeRate.McExchange/McExchangeRateProvider.cs b/src/Plugins/ExchangeRate.McExchange/McExchangeRateProvider.csindex 19d087daa..ebb027717 100644--- a/src/Plugins/ExchangeRate.McExchange/McExchangeRateProvider.cs+++ b/src/Plugins/ExchangeRate.McExchange/McExchangeRateProvider.cs@@ -2,21 +2,25 @@using Grand.SharedKernel;using System;using System.Collections.Generic;+using System.Net.Http;using System.Threading.Tasks;-namespace Grand.Plugin.ExchangeRate.McExchange+namespace ExchangeRate.McExchange{public class McExchangeRateProvider : IExchangeRateProvider{+ private readonly IHttpClientFactory _httpClientFactory;- private readonly IDictionary<string, IRateProvider> _providers = new Dictionary<string, IRateProvider> {- {"eur", new EcbExchange()},- {"pln", new NbpExchange()},- };+ private readonly IDictionary<string, IRateProvider> _providers;- public McExchangeRateProvider()+ public McExchangeRateProvider(IHttpClientFactory httpClientFactory){+ _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));+ _providers = new Dictionary<string, IRateProvider> {+ {"eur", new EcbExchange(_httpClientFactory)},+ {"pln", new NbpExchange(_httpClientFactory)}+ };}public string ConfigurationUrl => "";@@ -36,7 +40,7 @@ namespace Grand.Plugin.ExchangeRate.McExchange/// </summary>/// <param name="exchangeRateCurrencyCode">Exchange rate currency code</param>/// <returns>Exchange rates</returns>- public Task<IList<Domain.Directory.ExchangeRate>> GetCurrencyLiveRates(string exchangeRateCurrencyCode)+ public Task<IList<Grand.Domain.Directory.ExchangeRate>> GetCurrencyLiveRates(string exchangeRateCurrencyCode){if (string.IsNullOrEmpty(exchangeRateCurrencyCode))throw new ArgumentNullException(nameof(exchangeRateCurrencyCode));diff --git a/src/Plugins/ExchangeRate.McExchange/NbpExchange.cs b/src/Plugins/ExchangeRate.McExchange/NbpExchange.csindex cca423e27..b7f344d80 100644--- a/src/Plugins/ExchangeRate.McExchange/NbpExchange.cs+++ b/src/Plugins/ExchangeRate.McExchange/NbpExchange.cs@@ -2,49 +2,54 @@ using System;using System.Collections.Generic;using System.Globalization;using System.Linq;-using System.Net;+using System.Net.Http;using System.Net.Mime;using System.Threading.Tasks;using System.Xml;-namespace Grand.Plugin.ExchangeRate.McExchange+namespace ExchangeRate.McExchange{internal class NbpExchange : IRateProvider{- public async Task<IList<Domain.Directory.ExchangeRate>> GetCurrencyLiveRates()+ private readonly IHttpClientFactory _httpClientFactory;++ public NbpExchange(IHttpClientFactory httpClientFactory)+ {+ _httpClientFactory = httpClientFactory;+ }+ public async Task<IList<Grand.Domain.Directory.ExchangeRate>> GetCurrencyLiveRates(){var currentDate = DateTime.Today.AddDays(-1);- var request = (HttpWebRequest)WebRequest.Create($"http://api.nbp.pl/api/exchangerates/tables/A/{currentDate.AddDays(-7):yyyy-MM-dd}/{currentDate:yyyy-MM-dd}");- request.Headers.Add("Accept", MediaTypeNames.Application.Xml);- using (var response = await request.GetResponseAsync())- {- var document = new XmlDocument();- document.Load(response.GetResponseStream());+ var httpClient = _httpClientFactory.CreateClient(Constant.DefaultHttpClientName);+ httpClient.DefaultRequestHeaders.Add("Accept", MediaTypeNames.Application.Xml);+ using var response = await httpClient.GetStreamAsync($"{Constant.NbpUrl}{currentDate.AddDays(-7):yyyy-MM-dd}/{currentDate:yyyy-MM-dd}");+ var document = new XmlDocument();+ document.Load(response);- var node = document.SelectNodes("//EffectiveDate")- .Cast<XmlElement>()- .OrderByDescending(x => x.InnerText)- .First();+ var node = document.SelectNodes("//EffectiveDate")+ .Cast<XmlElement>()+ .OrderByDescending(x => x.InnerText)+ .First();- var updateDate = DateTime.ParseExact(node.InnerText, "yyyy-MM-dd", null);- var ratesNode = node.ParentNode.SelectSingleNode("Rates");+ var updateDate = DateTime.ParseExact(node.InnerText, "yyyy-MM-dd", null);+ var ratesNode = node.ParentNode.SelectSingleNode("Rates");- var provider = new NumberFormatInfo();- provider.CurrencyDecimalSeparator = ".";- provider.NumberGroupSeparator = "";+ var provider = new NumberFormatInfo();+ provider.CurrencyDecimalSeparator = ".";+ provider.NumberGroupSeparator = "";- var exchangeRates = new List<Domain.Directory.ExchangeRate>();- foreach (XmlNode node2 in ratesNode.ChildNodes)- {- var rate = double.Parse(node2.SelectSingleNode("Mid").InnerText, provider);- exchangeRates.Add(new Domain.Directory.ExchangeRate {- CurrencyCode = node2.SelectSingleNode("Code").InnerText,- Rate = Math.Round(1 / rate, 4, MidpointRounding.AwayFromZero),- UpdatedOn = updateDate- });- }- return exchangeRates;+ var exchangeRates = new List<Grand.Domain.Directory.ExchangeRate>();+ foreach (XmlNode node2 in ratesNode.ChildNodes)+ {+ var rate = double.Parse(node2.SelectSingleNode("Mid").InnerText, provider);+ exchangeRates.Add(new Grand.Domain.Directory.ExchangeRate {+ CurrencyCode = node2.SelectSingleNode("Code").InnerText,+ Rate = Math.Round(1 / rate, 4, MidpointRounding.AwayFromZero),+ UpdatedOn = updateDate+ });}+ return exchangeRates;+}}}diff --git a/src/Plugins/ExchangeRate.McExchange/StartupApplication.cs b/src/Plugins/ExchangeRate.McExchange/StartupApplication.csindex e71b47816..1c510c5c9 100644--- a/src/Plugins/ExchangeRate.McExchange/StartupApplication.cs+++ b/src/Plugins/ExchangeRate.McExchange/StartupApplication.cs@@ -1,12 +1,11 @@using Grand.Business.Common.Interfaces.Providers;using Grand.Infrastructure;-using Grand.Plugin.ExchangeRate.McExchange;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;-namespace ExchangeRate.EcbExchange+namespace ExchangeRate.McExchange{public class StartupApplication : IStartupApplication{