Skip to content

amonin7/currency-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Currency converter

Summary

This is a reactive REST endpoint to handle the currency conversion.

Installation (running) guide

  1. Create application.properties file in src/main/resources directory
  2. Add the property dotioclient.access_key and assign it with access key for the http://api.exchangeratesapi.io API.
    In simple case your application.properties should look like this:
dotioclient.access_key=your_own_access_key

3a. Build and run the app using one of the following commands (just run in terminal from the project directory):

  • gradle clean build bootRun
  • ./gradlew clean build bootRun

3b. If you use IDE and just imported the project - you need to run the src/main/java/com/onlym/converter/ConverterApplication.java psvm method by clicking the "play" / "run" button next to it.

Details

The REST endpoint responds to a POST request to the URI /currency/convert with a body of type ConversionRequest, that will contain three fields: from, to, amount.

The fields from and to are three-letter strings representing the currencies, like USD or EUR and amount field contains the decimal number, representing the quantity to be converted, e.g. 123.45.

The result of this POST request will be an object of type ConversionResponse, containing the conversion result in these four fields:

  • from contains the starting currency from where we are converting
  • to contains the destination currency
  • amount contains the original amount to be converted
  • converted contains the converted result in the destination currency

The external providers which provide the currency conversion rates are two:

These two providers offer a free currency conversion rates endpoint that can be used to retrieve the actual rate and perform the conversion.

The next approach is followed, when receiving the conversion request: one provider is randomly chosen from the two, and the conversion rates are got from it.

  • If the chosen provider is offline or is having problem, the app switches to another provider.
    • If another provider is offline as well, the app returns the error, indicating that there are no providers available.
  • If the chosen provider is online and provides the conversion rate, then it is used to convert the requested amount.

Serialization format

JSON is used as the serialization format for the ConversionRequest and ConversionResponse objects.

Example

Assuming a POST request to /currency/convert with the following body:

{
  "from": "EUR",
  "to": "USD",
  "amount": 123.45
}

and a conversion rate of 1.1 from EUR to USD (obtained from the external provider of currency rates), the result should be:

{
  "from": "EUR",
  "to": "USD",
  "amount": 123.45,
  "converted": 135.8
}

where 135.78 is the result of 123.45 * 1.1, amount times the conversion rate.

Requisites

  • Java 11
  • Spring WebFlux and Spring Boot 2
  • Spring Reactive WebClient
  • Project Reactor (Mono, Flux, etc), included in Spring WebFlux

About

A reactive REST endpoint to handle the currency conversion.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages