Skip to content

StefH/FreecurrencyAPI

Repository files navigation

Freecurrency-API

Unofficial RestEase C# Client for freecurrencyapi which uses IMemoryCache to cache the results.

NuGet

NuGet Badge

Configuration

You will need your ApiKey to use freecurrencyapi, you can get one https://app.freecurrencyapi.com/register.

Register the client via Dependency Injection:

services.AddFreecurrencyAPI(o =>
    o.ApiKey = "[YOUR_API_KEY]"
);

Usage

Status

Returns your current quota

IFreecurrencyClient client = // get from DI
var statusResponse = await client.GetStatusAsync();

Latest Exchange Rates

Returns the latest exchange rates. The default base currency is USD.

IFreecurrencyClient client = // get from DI
var rates = await _client.GetLatestExchangeRatesAsync(CurrencyCodes.USD, new [ CurrencyCodes.EUR, CurrencyCodes.AUD ]);

var rate = await _client.GetLatestExchangeRateAsync(CurrencyCodes.USD, CurrencyCodes.EUR);

Historical Exchange Rates

Returns the latest exchange rates. The default base currency is USD.

// todo

Currencies

Returns all supported currencies

IFreecurrencyClient client = // get from DI
var currencyResponse = await client.GetCurrency("EUR");

var currenciesResponse = await client.GetCurrencies(new [] { "EUR", "USD" });

var allCurrenciesResponse = await client.GetCurrencies();

Options

public class FreecurrencyAPIOptions
{
    /// <summary>
    /// The required BaseAddress.
    /// </summary>
    [Required]
    public Uri BaseAddress { get; set; } = new("https://api.freecurrencyapi.com/v1");

    [Required]
    public string ApiKey { get; set; } = null!;

    /// <summary>
    /// Optional HttpClient name to use.
    /// </summary>
    public string? HttpClientName { get; set; }

    /// <summary>
    /// This timeout in seconds defines the timeout on the HttpClient which is used to call the BaseAddress.
    /// Default value is 60 seconds.
    /// </summary>
    [Range(1, int.MaxValue)]
    public int TimeoutInSeconds { get; set; } = 60;

    /// <summary>
    /// The maximum number of retries.
    /// </summary>
    [Range(0, 99)]
    public int MaxRetries { get; set; } = 3;

    /// <summary>
    /// In addition to Network failures, TaskCanceledException, HTTP 5XX and HTTP 408. Also retry these <see cref="HttpStatusCode"/>s. [Optional]
    /// </summary>
    public HttpStatusCode[]? HttpStatusCodesToRetry { get; set; }

    /// <summary>
    /// The cache expiration time in minutes for the latest exchange rates.
    /// Default value is 60 minutes.
    /// </summary>
    [Range(0, int.MaxValue)]
    public int GetLatestExchangeRatesCacheExpirationInMinutes { get; set; } = 60;

    /// <summary>
    /// The cache expiration time in hours for the currencies.
    /// Default value is 24 hours.
    /// </summary>
    [Range(0, int.MaxValue)]
    public int GetCurrenciesCacheExpirationInHours { get; set; } = 24;
}

References