Code Monkey home page Code Monkey logo

currency_exchange_api's Introduction

Symfony Currency Exchange Rate API

This project implements a currency exchange rate API using Symfony 5. The application fetches data from the Open Exchange Rates API, saves these rates into a MySQL database, and leverages Redis for caching to optimize data fetching.

Project Structure

  • Command: The console command that fetches exchange rates from the Open Exchange Rates API is located in the src\Command\CurrencyRatesCommand class.
  • Controller: The endpoint that returns the exchange rates for a given set of currencies is handled in the src\Controller\ExchangeRatesController class.
  • Entity: The src\Entity\CurrencyRate class is an entity class representing a record of a currency exchange rate.
  • Tests: The unit tests for the console command and the endpoint are in the tests\CurrencyRatesCommandTest and tests\ExchangeRatesControllerTest classes respectively.

Setup

  1. Clone this repository

    git clone https://github.com/fvarli/currency_exchange_api.git
  2. Install dependencies

    cd repository
    composer install
  3. Environment Variables: Create a .env file at the project root and populate the following variables:

    DATABASE_URL=mysql://db_user:[email protected]:3306/db_name
    REDIS_URL=redis://localhost
    OPEN_EXCHANGE_RATES_APP_ID=your_open_exchange_rates_app_id
    REDIS_HOST=localhost
    REDIS_PORT=6379
    

    Update db_user, db_password, db_name and your_open_exchange_rates_app_id with your actual database credentials and Open Exchange Rates App ID.

  4. Database: Run the Doctrine migrations to create the database schema:

    php bin/console doctrine:migrations:migrate

Usage

  1. Fetching Exchange Rates: Run the console command to fetch the exchange rates and save them into the MySQL database and Redis:

    php bin/console app:currency:rates USD EUR,JPY,GBP,TRY

    Replace USD, EUR, JPY, GBP , TRY with the currencies you want to fetch.

  2. Getting Exchange Rates: Send a GET request to the endpoint to get the exchange rates for a given set of currencies:

    GET /api/exchange-rates?base_currency=USD&target_currencies=EUR,GBP,JPY,TRY
    

    Replace USD in base_currency and EUR,GBP,JPY,TRY in target_currencies with the currencies you want.

Scheduling the Command

To schedule the command to run automatically, you can add an entry to your system's crontab. The following command will run the fetch command every day at 1 AM server time:

0 1 * * * cd /path/to/project/root && php bin/console app:currency:rates USD EUR,GBP,JPY,TRY >> /var/log/currency_rates_command.log 2>&1

Replace /path/to/project/root with the actual path to your project's root directory. This command also logs the output of the command to /var/log/currency_rates_command.log for monitoring purposes.

Remember to replace EUR,GBP,JPY,TRY with the actual currencies you want to fetch.

Tests

To run the tests, use the following command:

php bin/phpunit

The test classes CurrencyRatesCommandTest and ExchangeRatesControllerTest are available for testing the functionality of the console command and the endpoint respectively.

Screenshots

Console

Console

Redis

Redis

Database

Database

Tests

Tests

Postman

Postman

currency_exchange_api's People

Contributors

fvarli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.