Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have currency converter implementation using any free and reliable online service #13

Open
zpbappi opened this issue Feb 18, 2018 · 2 comments

Comments

@zpbappi
Copy link
Owner

zpbappi commented Feb 18, 2018

Provide an implementation of ICurrencyConverter using an online service (free + reliable). This can be optionally consumed by the users. I would like to get your thoughts on:

  • Should such an implementation be in the package with a namespace like Money.Contrib.CurrencyConverter?
  • Should I publish the currency converters as a separate contrib package all together?
@RocketSquirrel
Copy link

Finding a free and reliable API provider that doesn't limit API calls to 1,000 per month seems to be problematic: https://www.quora.com/Is-there-an-API-for-real-time-currency-conversion
Others that I found followed the same pattern.

I'm inclined to think the currency converter should live in a separate package for extensibility purposes.

@hasankaplan-github
Copy link

Hi,
The issue opened long time ago, but I found a free API and implemented in my own project. Firstly I converted Money project to .Net version 8.0 (I use this version) and implemented the API. The implementation is below:

namespace Money
{
    public class FrankfurterApiConvertedDto
    {
        public decimal Amount { get; set; }
        public string Base { get; set; }
        public DateTime Date { get; set; }
        public Dictionary<Currency, decimal> Rates { get; set; }
    }


    public class FrankfurterApiCurrencyConverter<T> : ICurrencyConverter<T> 
        where T : struct
    {
        private readonly HttpClient _httpClient;

        public FrankfurterApiCurrencyConverter()
        {
            _httpClient = new HttpClient();
            _httpClient.BaseAddress = new Uri("https://api.frankfurter.app/");
        }

        public T Convert(T fromAmount, Currency fromCurrency, Currency toCurrency)
        {
            if (fromCurrency == toCurrency)
            {
                return fromAmount;
            }

            var converterAddress = $"latest?amount=1&from={fromCurrency}&to={toCurrency}";
            var rate = _httpClient.GetFromJsonAsync<FrankfurterApiConvertedDto>(converterAddress).GetAwaiter().GetResult();

            var rateConvertedToT = NumericTypeHelper.ConvertTo<T>(rate.Rates[toCurrency]);

            return BinaryOperationHelper.MultiplyChecked(fromAmount, rateConvertedToT);
        }
    }
}

The API does not support all of the currencies, a check is needed, API has a supported currencies method. Also as I remember, rates are updated once in a day, you can look at the documentation.

HttpClient usage may be wrong, I generally use Typed Http Clients in web projects. It is just an idea of a free API usage.

I don't totaly use Money project as is, I was inspired by your project, thank you for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants