From aef63f59b642bfd9aedaa7d9f696eedb48c4cf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Przyby=C5=82ek?= Date: Wed, 20 Apr 2022 16:44:17 +0200 Subject: [PATCH] Implement table models parsing --- .../{ => Currency}/ExchangeRatesSeries.cs | 2 +- .../Models/{ => Currency}/Rate.cs | 2 +- .../Models/Table/ExchangeRatesTable.cs | 12 ++ NBPCurrencyAPILib/Models/Table/Rate.cs | 9 ++ NBPCurrencyAPILib/NBPAPI_Currency.cs | 4 +- NBPCurrencyAPILib/NBPAPI_Main.cs | 7 +- NBPCurrencyAPILib/NBPAPI_Table.cs | 121 +++++++----------- NBPCurrencyAPILibTests/NBPAPITableTests.cs | 16 +-- 8 files changed, 84 insertions(+), 89 deletions(-) rename NBPCurrencyAPILib/Models/{ => Currency}/ExchangeRatesSeries.cs (86%) rename NBPCurrencyAPILib/Models/{ => Currency}/Rate.cs (88%) create mode 100644 NBPCurrencyAPILib/Models/Table/ExchangeRatesTable.cs create mode 100644 NBPCurrencyAPILib/Models/Table/Rate.cs diff --git a/NBPCurrencyAPILib/Models/ExchangeRatesSeries.cs b/NBPCurrencyAPILib/Models/Currency/ExchangeRatesSeries.cs similarity index 86% rename from NBPCurrencyAPILib/Models/ExchangeRatesSeries.cs rename to NBPCurrencyAPILib/Models/Currency/ExchangeRatesSeries.cs index 029b098..d8755bd 100644 --- a/NBPCurrencyAPILib/Models/ExchangeRatesSeries.cs +++ b/NBPCurrencyAPILib/Models/Currency/ExchangeRatesSeries.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace NBPAPI.NET.Core.Models +namespace NBPAPI.NET.Core.Models.Currency { public class ExchangeRatesSeries { diff --git a/NBPCurrencyAPILib/Models/Rate.cs b/NBPCurrencyAPILib/Models/Currency/Rate.cs similarity index 88% rename from NBPCurrencyAPILib/Models/Rate.cs rename to NBPCurrencyAPILib/Models/Currency/Rate.cs index f66951a..b1f08a9 100644 --- a/NBPCurrencyAPILib/Models/Rate.cs +++ b/NBPCurrencyAPILib/Models/Currency/Rate.cs @@ -1,6 +1,6 @@ using System; -namespace NBPAPI.NET.Core.Models +namespace NBPAPI.NET.Core.Models.Currency { public class Rate { diff --git a/NBPCurrencyAPILib/Models/Table/ExchangeRatesTable.cs b/NBPCurrencyAPILib/Models/Table/ExchangeRatesTable.cs new file mode 100644 index 0000000..4a31736 --- /dev/null +++ b/NBPCurrencyAPILib/Models/Table/ExchangeRatesTable.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace NBPAPI.NET.Core.Models.Table +{ + public class ExchangeRatesTable + { + public string Table { get; set; } + public string No { get; set; } + public string EffectiveDate { get; set; } + public IEnumerable Rates { get; set; } + } +} diff --git a/NBPCurrencyAPILib/Models/Table/Rate.cs b/NBPCurrencyAPILib/Models/Table/Rate.cs new file mode 100644 index 0000000..a3d20b4 --- /dev/null +++ b/NBPCurrencyAPILib/Models/Table/Rate.cs @@ -0,0 +1,9 @@ +namespace NBPAPI.NET.Core.Models.Table +{ + public class Rate + { + public string Currency { get; set; } + public string Code { get; set; } + public float Mid { get; set; } + } +} \ No newline at end of file diff --git a/NBPCurrencyAPILib/NBPAPI_Currency.cs b/NBPCurrencyAPILib/NBPAPI_Currency.cs index 6b9bdaf..277edd1 100644 --- a/NBPCurrencyAPILib/NBPAPI_Currency.cs +++ b/NBPCurrencyAPILib/NBPAPI_Currency.cs @@ -1,4 +1,4 @@ -using NBPAPI.NET.Core.Models; +using NBPAPI.NET.Core.Models.Currency; using System; using System.Net.Http; using System.Text.Json; @@ -11,8 +11,6 @@ namespace NBPCurrencyAPILib /// public static partial class NBPAPI { - private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; - #region GetCurrency /// /// Gets current Exchange rate from PLN to (asynchronous). diff --git a/NBPCurrencyAPILib/NBPAPI_Main.cs b/NBPCurrencyAPILib/NBPAPI_Main.cs index 08b186e..4ccb778 100644 --- a/NBPCurrencyAPILib/NBPAPI_Main.cs +++ b/NBPCurrencyAPILib/NBPAPI_Main.cs @@ -1,8 +1,7 @@ using System; -using System.Net.Http; -using System.Threading.Tasks; -using System.Threading; using System.Linq; +using System.Net.Http; +using System.Text.Json; namespace NBPCurrencyAPILib { @@ -19,6 +18,8 @@ public static partial class NBPAPI /// private readonly static string uri = "http://api.nbp.pl/api/"; + private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; + /// The table code (in uppercase). /// Whether contains the table code. private static bool TableLetterCheck(char tableCode) diff --git a/NBPCurrencyAPILib/NBPAPI_Table.cs b/NBPCurrencyAPILib/NBPAPI_Table.cs index b0163f2..a57b020 100644 --- a/NBPCurrencyAPILib/NBPAPI_Table.cs +++ b/NBPCurrencyAPILib/NBPAPI_Table.cs @@ -1,78 +1,75 @@ -using System; -using System.Collections.Generic; +using NBPAPI.NET.Core.Models.Table; +using System; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Net.Http; +using System.Text.Json; +using System.Threading.Tasks; namespace NBPCurrencyAPILib { public static partial class NBPAPI { + private static string ParseTableLetter(char tableCode) + { + var tableLetter = tableCode.ToString().ToUpper(); + + if (!TableLetterCheck(tableLetter[0])) + throw new ArgumentException("Incorrect table letter!"); + + return tableLetter; + } + #region GetTable /// /// Gets current table of exchange rates (asynchronous). /// /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// Async XML/JSON result from NBP API. - public static Task GetTableAsync(char tableCode, bool isJSON = true) + /// Exchange rates table. + public static async Task GetTableAsync(char tableCode) { - string tableLetter = tableCode.ToString().ToUpper(); - - if (!TableLetterCheck(tableLetter[0])) throw new ArgumentException("Incorrect table letter!"); - - string getpath = uri + $"exchangerates/tables/{tableLetter}?format={(isJSON ? "json" : "xml")}"; + string getpath = uri + $"exchangerates/tables/{ParseTableLetter(tableCode)}?format=json"; HttpResponseMessage result = httpClient.GetAsync(getpath).Result; if (!result.IsSuccessStatusCode) throw Error(result); - return result.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault(); } /// /// Gets current table of exchange rates. /// /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// XML/JSON result from NBP API. - public static string GetTable(char tableCode, bool isJSON = true) + /// Exchange rates table. + public static ExchangeRatesTable GetTable(char tableCode) { - return GetTableAsync(tableCode, isJSON).Result; + return GetTableAsync(tableCode).Result; } - /// /// Gets table of exchange rates from (asynchronous). /// /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. /// the date. - /// Async XML/JSON result from NBP API. - public static Task GetTableAsync(char tableCode, DateTime date, bool isJSON = true) + /// Exchange rates table. + public static async Task GetTableAsync(char tableCode, DateTime date) { - string tableLetter = tableCode.ToString().ToUpper(); - - if (!TableLetterCheck(tableLetter[0])) throw new ArgumentException("Incorrect table letter!"); - - string getpath = uri + $"exchangerates/tables/{tableLetter}/{date:yyyy-MM-dd}?format={(isJSON ? "json" : "xml")}"; + string getpath = uri + $"exchangerates/tables/{ParseTableLetter(tableCode)}/{date:yyyy-MM-dd}?format=json"; HttpResponseMessage result = httpClient.GetAsync(getpath).Result; if (!result.IsSuccessStatusCode) throw Error(result); - return result.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault(); } /// /// Gets table of exchange rates from . /// /// The Date. /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// XML/JSON result from NBP API. - public static string GetTable(char tableCode, DateTime date, bool isJSON = true) + /// Exchange rates table. + public static ExchangeRatesTable GetTable(char tableCode, DateTime date) { - return GetTableAsync(tableCode, date, isJSON).Result; + return GetTableAsync(tableCode, date).Result; } @@ -80,68 +77,54 @@ public static string GetTable(char tableCode, DateTime date, bool isJSON = true) /// Gets table of exchange rates published today (asynchronous). /// /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// Async XML/JSON result from NBP API. - public static Task GetTableTodayAsync(char tableCode, bool isJSON = true) + /// Exchange rates table. + public static async Task GetTableTodayAsync(char tableCode) { - string tableLetter = tableCode.ToString().ToUpper(); - - if (!TableLetterCheck(tableLetter[0])) throw new ArgumentException("Incorrect table letter!"); - - string getpath = uri + $"exchangerates/tables/{tableLetter}/today?format={(isJSON ? "json" : "xml")}"; + string getpath = uri + $"exchangerates/tables/{ParseTableLetter(tableCode)}/today?format=json"; HttpResponseMessage result = httpClient.GetAsync(getpath).Result; if (!result.IsSuccessStatusCode) throw Error(result); - return result.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault(); } /// /// Gets table of exchange rates published today. /// /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// XML/JSON result from NBP API. - public static string GetTableToday(char tableCode, bool isJSON = true) + /// Exchange rates table. + public static ExchangeRatesTable GetTableToday(char tableCode) { - return GetTableTodayAsync(tableCode, isJSON).Result; + return GetTableTodayAsync(tableCode).Result; } #endregion GetTable - - #region GetTables /// /// Gets last of tables (asynchronous). /// /// Table Code (Capital A-C). /// Amount of tables to return. - /// true if returned string will be JSON, false if XML. - /// Async XML/JSON result from NBP API. - public static Task GetTablesAsync(char tableCode, int topCount, bool isJSON = true) + /// Array of exchange rates tables. + public static async Task GetTablesAsync(char tableCode, int topCount) { - string tableLetter = tableCode.ToString().ToUpper(); - - if (!TableLetterCheck(tableLetter[0])) throw new ArgumentException("Incorrect table letter!"); - - string getpath = uri + $"exchangerates/tables/{tableLetter}/last/{topCount}?format={(isJSON ? "json" : "xml")}"; + string getpath = uri + $"exchangerates/tables/{ParseTableLetter(tableCode)}/last/{topCount}?format=json"; HttpResponseMessage result = httpClient.GetAsync(getpath).Result; if (!result.IsSuccessStatusCode) throw Error(result); - return result.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(await result.Content.ReadAsStringAsync(), JsonOptions); } /// /// Gets last of tables. /// /// Amount of tables to return. /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// XML/JSON result from NBP API. - public static string GetTables(char tableCode, int topCount, bool isJSON = true) + /// Array of exchange rates tables. + public static ExchangeRatesTable[] GetTables(char tableCode, int topCount) { - return GetTablesAsync(tableCode, topCount, isJSON).Result; + return GetTablesAsync(tableCode, topCount).Result; } /// /// Gets tables from to (asynchronous). @@ -149,21 +132,16 @@ public static string GetTables(char tableCode, int topCount, bool isJSON = true) /// Table Code (Capital A-C). /// Start date. /// End date. - /// true if returned string will be JSON, false if XML. - /// Async XML/JSON result from NBP API. - public static Task GetTablesAsync(char tableCode, DateTime startDate, DateTime endDate, bool isJSON = true) + /// Array of exchange rates tables. + public static async Task GetTablesAsync(char tableCode, DateTime startDate, DateTime endDate) { - string tableLetter = tableCode.ToString().ToUpper(); - - if (!TableLetterCheck(tableLetter[0])) throw new ArgumentException("Incorrect table letter!"); - - string getpath = uri + $"exchangerates/tables/{tableLetter}/{startDate:yyyy-MM-dd}/{endDate:yyyy-MM-dd}?format={(isJSON ? "json" : "xml")}"; + string getpath = uri + $"exchangerates/tables/{ParseTableLetter(tableCode)}/{startDate:yyyy-MM-dd}/{endDate:yyyy-MM-dd}?format=json"; HttpResponseMessage result = httpClient.GetAsync(getpath).Result; if (!result.IsSuccessStatusCode) throw Error(result); - return result.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(await result.Content.ReadAsStringAsync(), JsonOptions); } /// /// Gets tables from to . @@ -171,11 +149,10 @@ public static Task GetTablesAsync(char tableCode, DateTime startDate, Da /// Start date. /// End date. /// Table Code (Capital A-C). - /// true if returned string will be JSON, false if XML. - /// XML/JSON result from NBP API. - public static string GetTables(char tableCode, DateTime startDate, DateTime endDate, bool isJSON = true) + /// Array of exchange rates tables. + public static ExchangeRatesTable[] GetTables(char tableCode, DateTime startDate, DateTime endDate) { - return GetTablesAsync(tableCode, startDate, endDate, isJSON).Result; + return GetTablesAsync(tableCode, startDate, endDate).Result; } #endregion GetTables } diff --git a/NBPCurrencyAPILibTests/NBPAPITableTests.cs b/NBPCurrencyAPILibTests/NBPAPITableTests.cs index e2cc4a8..895cf27 100644 --- a/NBPCurrencyAPILibTests/NBPAPITableTests.cs +++ b/NBPCurrencyAPILibTests/NBPAPITableTests.cs @@ -1,10 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NBPCurrencyAPILib; using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace NBPCurrencyAPILib.Tests { @@ -15,31 +11,33 @@ public class NBPAPITableTests [TestMethod()] public void GetTableAsyncTest() { - Assert.IsTrue(NBPAPI.GetTableAsync('A').Result.Contains("EUR")); + Assert.IsNotNull(NBPAPI.GetTable('A').Rates.SingleOrDefault(r => r.Code == "EUR")); } [TestMethod()] public void GetTableAsyncTestAtDate() { - Assert.IsTrue(NBPAPI.GetTableAsync('A', new DateTime(2021, 07, 20)).Result.Contains("EUR")); + Assert.IsNotNull(NBPAPI.GetTable('A', new DateTime(2021, 07, 20)).Rates.SingleOrDefault(r => r.Code == "EUR")); } [TestMethod()] public void GetTableTodayAsyncTest() { - Assert.IsTrue(NBPAPI.GetTableTodayAsync('A').Result.Contains("EUR")); + Assert.IsNotNull(NBPAPI.GetTableToday('A').Rates.SingleOrDefault(r => r.Code == "EUR")); } [TestMethod()] public void GetTablesAsyncTest() { - Assert.IsTrue(NBPAPI.GetTablesAsync('A', 3).Result.Contains("EUR")); + var table = NBPAPI.GetTables('A', 3); + Assert.IsTrue(table.Count() == 3); + Assert.IsNotNull(table.FirstOrDefault().Rates.SingleOrDefault(r => r.Code == "EUR")); } [TestMethod()] public void GetTablesAsyncTestFromTo() { - Assert.IsTrue(NBPAPI.GetTablesAsync('A', new DateTime(2021, 07, 20), new DateTime(2021, 07, 26)).Result.Contains("EUR")); + Assert.IsNotNull(NBPAPI.GetTables('A', new DateTime(2021, 07, 20), new DateTime(2021, 07, 26)).FirstOrDefault().Rates.SingleOrDefault(r => r.Code == "EUR")); } } } \ No newline at end of file