These 3 APIs allows you to retrieve currency conversion data from an external API service.
https://localhost:7070/api/CurrencyConvertor/
The GetExchangeRates
API endpoint provides the latest exchange rates for a given base currency.
GET https://localhost:7070/api/CurrencyConvertor/GetExchangeRates
from
(query parameter, required): The base currency code for which you want to get the exchange rates. It should be a three-letter alphabetic code (e.g.,USD
for US Dollar).
- Method: GET
- Endpoint:
https://localhost:7070/api/CurrencyConvertor/GetExchangeRates
- Query Parameter:
from
: The three-letter currency code (e.g., USD, EUR).
1- GET - HTTP Response Code: 200
HTTP/1.1 200
Content-Type: application/json
{
"baseCurrency": "USD",
"fetchDate": "05-07-2024",
"currencies": [
{
"currencyCode": "AUD",
"currencyValue": 1.4849
},....
....
{
"currencyCode": "ZAR",
"currencyValue": 18.2346
}]
}
To use this API, send a GET request to the /GetExchangeRates
endpoint with the from
query parameter set to the desired currency code.
Example:
GET /GetExchangeRates?from=USD
The ConvertCurrency
API endpoint converts an amount from one currency to another based on the latest exchange rates.
GET https://localhost:7070/api/CurrencyConvertor/convertcurrency
amount
(query parameter, optional): The amount of money to convert. It should be a positive number.from
(query parameter, optional): The currency code from which the amount will be converted. It should be a three-letter alphabetic code (e.g.,USD
for US Dollar). If no currency provided it will use default currency which isEUR
for conversion.to
(query parameter, required): The currency code to which the amount will be converted. It should be a three-letter alphabetic code (e.g.,GBP
for British Pound).
- Method: GET
- Endpoint:
https://localhost:7070/api/CurrencyConvertor/convertcurrency
- Query Parameter:
amount
: The double valued positive number (e.g., 10.3).from
: The three-letter currency code (e.g., USD, EUR).to
: The three-letter currency code (e.g., USD, EUR).
1- GET - HTTP Response Code: 200
HTTP/1.1 200
Content-Type: application/json
{
"baseCurrency": "USD",
"fetchDate": "05-07-2024",
"currencies": [
{
"currencyCode": "GBP",
"currencyValue": 7.8172
}
]
}
To use this API, send a GET request to the /convertcurrency
endpoint with the from
, to
query parameters set to the desired currency code and amount
query parameter set to desired amount to convert .
Example:
GET /convertcurrency?amount=10&from=usd&to=gbp
The GetHistoricalDataForCurrency
API endpoint retrieves historical exchange rates for a specified currency over a given date range.
GET https://localhost:7070/api/CurrencyConvertor/GetHistoricalDataForCurrency
fromdate
(query parameter, required): The start date for fetching historical data in the formatYYYY-MM-DD
.todate
(query parameter, optional): The end date for fetching historical data in the formatYYYY-MM-DD
. If no ToDate is provided then current date will be used.amount
(query parameter, optional): The amount of money to convert. It should be a positive number. If not provided then it will treated as 1 by default.from
(query parameter, optional): The currency code from which the amount will be converted. It should be a three-letter alphabetic code (e.g.,USD
for US Dollar). If no currency provided it will use default currency which isEUR
for conversion.to
(query parameter, required): The currency code to which the amount will be converted. It should be a three-letter alphabetic code (e.g.,GBP
for British Pound).PageNo
(query parameter, optional): To implement pagination. By Default 1.PageSize
(query parameter, optional): To return the number of records in each request. By Default 10.
- Method: GET
- Endpoint:
https://localhost:7070/api/CurrencyConvertor/GetHistoricalDataForCurrency
- Query Parameter:
fromdate
: The Start Date (e.g., 2020-10-10).todate
: The end date (e.g., 2020-10-15).amount
: The double valued positive number (e.g., 10.3).from
: The three-letter currency code (e.g., USD, EUR).to
: The three-letter currency code (e.g., USD, EUR).Pageno
: The positive integer (e.g., 2)PageSize
: The positive integer (e.g., 5)
1- GET - HTTP Response Code: 200
HTTP/1.1 200
Content-Type: application/json
{
"startDate": "04-01-1999",
"endDate": "27-01-2020",
"amount": 10.0,
"baseCurrency": "EUR",
"historicRates": [
{
"fetchDate": "04-01-1999",
"rates": {
"GBP": 7.0923
}
},
{
"fetchDate": "11-01-1999",
"rates": {
"GBP": 7.059
}
}]
}
To use this API, send a GET request to the /GetHistoricalDataForCurrency
endpoint with the fromdate
, todate
, amount
, from
, to
, pageno
, pagesize
query parameters set to the desired valid values.
Example:
GET /GetHistoricalDataForCurrency?fromdate=xuz&todate=2020-02-01&amount=10&to=gbp&pageno=1&pagesize=2
This API includes rate limiting functionality to prevent abuse and ensure fair usage of resources. Rate limiting restricts the number of requests that clients can make within a specified time period. If the rate limit is exceeded, the API returns a 429 Too Many Requests
status code.
Rate limiting is implemented using middleware in the ASP.NET Core pipeline. The middleware tracks the number of requests made by each client IP address and enforces rate limits based on predefined rules.
The rate limit configuration is defined in the Program.cs
file. You can adjust the following parameters to customize the rate limiting behavior:
- Limit: Maximum number of requests allowed per time span.
- Period: Time span during which the request limit applies (e.g., "10m" for 10 minutes, "1h" for 1 hour).
Example configuration:
services.Configure<IpRateLimitOptions>(options =>
{
options.GeneralRules = new List<RateLimitRule>
{
new RateLimitRule
{
Endpoint = "*",
Limit = 100, // Maximum number of requests per time span
Period = "10m" // Time span (e.g., "10m" for 10 minutes)
}
};
});
Clients should be aware of the rate limits enforced by the API and adjust their request rates accordingly.
If a client receives a 429 Too Many Requests
response, it should wait for the rate limit window to expire before sending additional requests.
The exceptional handler middleware is implemented to handle and log exceptions that occur during the execution of requests in the application. It provides a centralized approach to manage error responses and ensures that the API returns consistent and meaningful error messages to clients.
The middleware class is created that implements IMiddleware
. Then it is injected in the Program.cs
file and is added to the ASP.NET Core pipeline.
To get started with this project:
Please note this project is created using DotNet 7 framework.
- Clone the repository to your local machine.
- Open the project in Visual Studio.
- Install the required nuget packages like NewtonSoft Json, etc.
- Build and run the project.
- Use a tool like Postman or a web browser to send requests to the API endpoint.
This project depends on the following packages:
Newtonsoft.Json
: For handling JSON serialization and deserialization.System.Net.Http
: For making HTTP requests to external APIs.
Contributions are welcome! If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request.