Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace NBPAPI.NET.Core.Models
namespace NBPAPI.NET.Core.Models.Currency
{
public class ExchangeRatesSeries
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace NBPAPI.NET.Core.Models
namespace NBPAPI.NET.Core.Models.Currency
{
public class Rate
{
Expand Down
12 changes: 12 additions & 0 deletions NBPCurrencyAPILib/Models/Table/ExchangeRatesTable.cs
Original file line number Diff line number Diff line change
@@ -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<Rate> Rates { get; set; }
}
}
9 changes: 9 additions & 0 deletions NBPCurrencyAPILib/Models/Table/Rate.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
4 changes: 1 addition & 3 deletions NBPCurrencyAPILib/NBPAPI_Currency.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,8 +11,6 @@ namespace NBPCurrencyAPILib
/// </summary>
public static partial class NBPAPI
{
private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };

#region GetCurrency
/// <summary>
/// Gets current Exchange rate from PLN to <paramref name="currencyCode"/> (asynchronous).
Expand Down
7 changes: 4 additions & 3 deletions NBPCurrencyAPILib/NBPAPI_Main.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -19,6 +18,8 @@ public static partial class NBPAPI
/// </summary>
private readonly static string uri = "http://api.nbp.pl/api/";

private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };

/// <param name="tableCode">The table code (in uppercase).</param>
/// <returns>Whether <see cref="TableCodes"/> contains the table code.</returns>
private static bool TableLetterCheck(char tableCode)
Expand Down
121 changes: 49 additions & 72 deletions NBPCurrencyAPILib/NBPAPI_Table.cs
Original file line number Diff line number Diff line change
@@ -1,181 +1,158 @@
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
/// <summary>
/// Gets current table of exchange rates (asynchronous).
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>Async XML/JSON result from NBP API.</returns>
public static Task<string> GetTableAsync(char tableCode, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static async Task<ExchangeRatesTable> 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<ExchangeRatesTable[]>(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault();
}
/// <summary>
/// Gets current table of exchange rates.
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, false if XML.</param>
/// <returns>XML/JSON result from NBP API.</returns>
public static string GetTable(char tableCode, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static ExchangeRatesTable GetTable(char tableCode)
{
return GetTableAsync(tableCode, isJSON).Result;
return GetTableAsync(tableCode).Result;
}


/// <summary>
/// Gets table of exchange rates from <paramref name="date"/> (asynchronous).
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <param name="date"> the date.</param>
/// <returns>Async XML/JSON result from NBP API.</returns>
public static Task<string> GetTableAsync(char tableCode, DateTime date, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static async Task<ExchangeRatesTable> 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<ExchangeRatesTable[]>(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault();
}
/// <summary>
/// Gets table of exchange rates from <paramref name="date"/>.
/// </summary>
/// <param name="date">The Date.</param>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, false if XML.</param>
/// <returns>XML/JSON result from NBP API.</returns>
public static string GetTable(char tableCode, DateTime date, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static ExchangeRatesTable GetTable(char tableCode, DateTime date)
{
return GetTableAsync(tableCode, date, isJSON).Result;
return GetTableAsync(tableCode, date).Result;
}


/// <summary>
/// Gets table of exchange rates published today (asynchronous).
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>Async XML/JSON result from NBP API.</returns>
public static Task<string> GetTableTodayAsync(char tableCode, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static async Task<ExchangeRatesTable> 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<ExchangeRatesTable[]>(await result.Content.ReadAsStringAsync(), JsonOptions).SingleOrDefault();
}
/// <summary>
/// Gets table of exchange rates published today.
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, false if XML.</param>
/// <returns>XML/JSON result from NBP API.</returns>
public static string GetTableToday(char tableCode, bool isJSON = true)
/// <returns>Exchange rates table.</returns>
public static ExchangeRatesTable GetTableToday(char tableCode)
{
return GetTableTodayAsync(tableCode, isJSON).Result;
return GetTableTodayAsync(tableCode).Result;
}
#endregion GetTable



#region GetTables
/// <summary>
/// Gets last <paramref name="topCount"/> of tables (asynchronous).
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="topCount">Amount of tables to return.</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>Async XML/JSON result from NBP API.</returns>
public static Task<string> GetTablesAsync(char tableCode, int topCount, bool isJSON = true)
/// <returns>Array of exchange rates tables.</returns>
public static async Task<ExchangeRatesTable[]> 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<ExchangeRatesTable[]>(await result.Content.ReadAsStringAsync(), JsonOptions);
}
/// <summary>
/// Gets last <paramref name="topCount"/> of tables.
/// </summary>
/// <param name="topCount">Amount of tables to return.</param>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>XML/JSON result from NBP API.</returns>
public static string GetTables(char tableCode, int topCount, bool isJSON = true)
/// <returns>Array of exchange rates tables.</returns>
public static ExchangeRatesTable[] GetTables(char tableCode, int topCount)
{
return GetTablesAsync(tableCode, topCount, isJSON).Result;
return GetTablesAsync(tableCode, topCount).Result;
}
/// <summary>
/// Gets tables from <paramref name="startDate"/> to <paramref name="endDate"/> (asynchronous).
/// </summary>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="startDate">Start date.</param>
/// <param name="endDate">End date.</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>Async XML/JSON result from NBP API.</returns>
public static Task<string> GetTablesAsync(char tableCode, DateTime startDate, DateTime endDate, bool isJSON = true)
/// <returns>Array of exchange rates tables.</returns>
public static async Task<ExchangeRatesTable[]> 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<ExchangeRatesTable[]>(await result.Content.ReadAsStringAsync(), JsonOptions);
}
/// <summary>
/// Gets tables from <paramref name="startDate"/> to <paramref name="endDate"/>.
/// </summary>
/// <param name="startDate">Start date.</param>
/// <param name="endDate">End date.</param>
/// <param name="tableCode">Table Code (Capital A-C).</param>
/// <param name="isJSON"><c>true</c> if returned string will be JSON, <c>false</c> if XML.</param>
/// <returns>XML/JSON result from NBP API.</returns>
public static string GetTables(char tableCode, DateTime startDate, DateTime endDate, bool isJSON = true)
/// <returns>Array of exchange rates tables.</returns>
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
}
Expand Down
16 changes: 7 additions & 9 deletions NBPCurrencyAPILibTests/NBPAPITableTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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"));
}
}
}