Now, I want to see that I can read from the database the exchange rates. For this, in Azure WebAPP I go to Settings => Configuration and add my connection string.
But now I have a problem: I must read from the configuration. So back on DI with InMemory class – I inject from ASP.NET Core the Configuration and in the tests I construct directly with null.
IConfiguration configuration;
public InMemoryDB(IConfiguration config)
{
this.configuration = config;
}
private string GetConRead(string name)
{
return configuration?.GetConnectionString(name);
}
Also, for easy retrieving from URL , I modify from
public async Task<ExchangeRates[]> Rates(string bank,string fromDate, string toDate)
( that generates the url : https://infovalutar.azurewebsites.net/api/v1.0/FromDB/Rates?bank=bnr&fromDate=20010101&toDate=20500101 )
to
[HttpGet(“{bank}/{fromDate}/{toDate}”)]
public async Task<ExchangeRates[]> Rates([FromRoute] string bank, [FromRoute]string fromDate, [FromRoute]string toDate)
( that generates the URL: https://infovalutar.azurewebsites.net/api/v1.0/rates/bnr/20010101/20100101 )
( I have also modified the route on the controller to [Route(“api/v{version:apiVersion}/rates”)] )
The modifications are at https://github.com/ignatandrei/InfoValutar/commit/aeda74e071a333e800a36936d2d3fc685b4b3ac8 .
The bad part – the deploy takes now 7 minutes…( running tests first …)
I have to implement now the following (
www.infovalutar.ro/bnr/2004/10/10/EUR
www.infovalutar.ro/bnr/azi/eur
www.infovalutar.ro/2004/6/4/eur.bnr
http://www.infovalutar.ro/bnr/rss
http://infovalutar.ro/curs.asmx?wsdl
I will implement first into the rates API , then I will redirect.
For example, the first one is like this: www.infovalutar.ro/bnr/2004/10/10/EUR and www.infovalutar.ro/2004/6/4/eur.bnr can be implemented like this:
[HttpGet(“{year}/{month}/{day}/{exchange}.{bank}”)]
[HttpGet(“{bank}/{year}/{month}/{day}/{exchange}”)]
public async Task<ExchangeRates> RatesOnDate([FromRoute] string bank,
[FromRoute]int year, [FromRoute]int month,
[FromRoute] int day, [FromRoute] string exchange)
What about the rss ? There is a package called System.ServiceModel.Syndication in .NET Core – and we can use for RSS like this:
[HttpGet(“{bank}/rss”)]
public async Task<IActionResult> GetRssFeed(string bank)
{
var data= await ret.TodayRates(bank);
var items = data
.Select(it =>
new SyndicationItem(
it.ExchangeTo,
it.ExchangeValue.ToString(),
new Uri($”http://www.infovalutar.ro/bnr/{it.Date.Year}/{it.Date.Month}/{it.Date.Day}/{it.ExchangeTo}”))
)
.ToArray();
var feed = new SyndicationFeed(
“Curs Valutar”,
“CursValutar, case, banci”,
new Uri( “http://www.infovalutar.ro/”),
items
);
feed.Language = “ro-ro”;
feed.TimeToLive = TimeSpan.FromSeconds(30);
using var sw = new StringWriter();
using var rssWriter = XmlWriter.Create(sw);
var rssFormatter = new Rss20FeedFormatter(feed,false);
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();
return Content(sw.ToString(), “text/xml”);
}
What about http://infovalutar.ro/curs.asmx?wsdl ? Found https://devblogs.microsoft.com/dotnet/custom-asp-net-core-middleware-example/ article , that says”it has no support for message security, WSDL generation, duplex channels, non-HTTP transports” – so I do not want to do. Maybe something should be left…
So , now , how I can now intercept calls like
www.infovalutar.ro/bnr/2004/10/10/EUR
www.infovalutar.ro/bnr/azi/eur
www.infovalutar.ro/2004/6/4/eur.bnr
http://www.infovalutar.ro/bnr/rss
? There is no controller ! So – middleware to help!
Infovalutar
And one hour passes...
(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )