DEV Community

Tech Tim (@TechTim42)
Tech Tim (@TechTim42)

Posted on

Redis Hackathon - Sanic Currency Exchange API

Overview of My Submission

api endpoint for Sanic Currency Exchange API

This submission is to switch my application (Sanic Currency Exchange API) from using PostgresDB to be able to use Redis Stack(RedisJSON) as data store, as shown in the diagram, it contains 3 parts of work.

refactor and data store switch

  1. Refactoring, to implement Dependency Injection to split Database logic and Application logic.
  2. Auto Test/Regression Test, to guarantee that before and after db switch the API behaves the same, this part of work was done in Postman, and the test scripts were shared in repo.
  3. Switch Data Store to RedisJson, to implement a Redis repository which keep the same behaviour as Postgres repository, to make the data store switch happen easily and smoothly.

The Idea

I cloned this Currency API (Github link below) about 3 years ago and kept maintaining it. The initial thoughts were maintaining the dependencies to avoid some security problems. Gradually, I started to re-write the application codes to make it more maintainable, readable. (This is a long way to go, I am sure it has not been finished yet, 😀)

This API is to fetch currency rates from Europe Central Bank, and offer the rates in RESTful API.

Since I saw the Redis hackathon announcement, I got this idea to switch data store from Postgres to Redis JSON. I used Redis before as a caching layer for application, but this is my first time to use the Redis modules. This implementation probably have a lot to improve, please feel free to leave comments to make it better and help me to learn more about Redis JSON and Redis Stack modules.

say thanks

Submission Category: MEAN/MERN Mavericks

  • Use Redis to the RESTful API to add caching and advanced searching capabilities using JSON module.
  • Use Redis as a primary database instead of Postgres.
  • Redis Stack docker image was used here instead of Redis Cloud, to keep the Postgres and Redis Stack have the same hardware spec.

How the data is stored:

The data is store as a document, for each day of the currency rates.

{
   "2020-01-01": {
      "USD": 1,
      "EUR": 1.1,
      "AUD": 0.97
   }
}
Enter fullscreen mode Exit fullscreen mode

How the data is accessed:

In python codes, the data is accessed through a simple get

r.json().get('2020-01-01')
Enter fullscreen mode Exit fullscreen mode

Language Used

  • Python (Sanic Framework)
  • Javascript

Link to Code

GitHub logo tim-hub / sanic-currency-exchange-rates-api

This is a self hosted, free, open source Python Currency Exchange Rate API fork.

Sanic Currency Exchange Rates Api

CodeFactor

This is a self hosted, free, currency exchange rate api, free demo at exchange-rate.bai.uno.

The current and historical foreign exchange rates data are from European Central Bank .

Usage

Latest date will be returned, if the date was not in Euro Central Bank history records

Latest & specific date rates

Get the latest foreign exchange rates.

GET /api/latest
Enter fullscreen mode Exit fullscreen mode

Get historical rates for any day since 1999.

GET /api/2018-03-26
Enter fullscreen mode Exit fullscreen mode

Rates are quoted against the Euro by default. Quote against a different currency by setting the base parameter in your request.

GET /api/latest?base=USD
Enter fullscreen mode Exit fullscreen mode

Request specific exchange rates by setting the symbols parameter.

GET /api/latest?symbols=USD,GBP
Enter fullscreen mode Exit fullscreen mode

Rates history

Get historical rates for a time period.

GET /api/history?start_at=2018-01-01&end_at=2018-09-01
Enter fullscreen mode Exit fullscreen mode

Limit results to specific exchange rates to save bandwidth with the symbols parameter.

GET /api/history?start_at=2018-01-01&end_at=2018-09-01&symbols=ILS,JPY
Enter fullscreen mode Exit fullscreen mode

Quote the historical rates against a different currency.

GET /api/history?start_at=2018-01-01&end_at=2018-09-01&base=USD
Enter fullscreen mode Exit fullscreen mode

Client side

Additional Resources / Info

Collaborators

This is a solo submission.

the end


Top comments (0)