Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

A currency exchange rate microservice designed for @jay2002shah's CS 361 course project.

License

parasiticfrisk/CS361_Microservice

Repository files navigation

Oregon State University Logo.

CS361: Microservice — Currency Exchange Rate

Note: this repository is not maintained. It is only here for archival purposes. If you would like to use this microservice, please fork this repository.

GitHub release (latest by date) Last Updated Not Maintained

About

Microservice created for @jay2002shah's course project in Software Engineering 1 (CS 361) at Oregon State University. This microservice uses the Open Access EchangeRate-API endpoint to generate currency exchange rate information.

Getting Started

These instructions will get you a copy of the microservice up and running on your local machine.

Prerequisites

You need to have a machine with Python 3.10+ installed.

$ python --version
Python 3.10

This microservice requires requests 2.28+. This can be installed globally on your machine or alternatively using pipenv.

References

Installation

All installation is handled using pip or pipenv. The following steps will install the microservice dependencies.

Build and prepare the Microservice

# Terminal 1
$ git clone https://github.com/4N0NYM0U5MY7H/CS361_Microservice
$ cd CS361_Microservice

# Option 1: Install depencies globally
$ python -m pip install requests

# Option 2: Install dependencies using pipenv
$ pipenv install

# Run the microservice
$ cd exchange_rate
$ python main.py

Test the Microservice is working correctly

# Terminal 2
$ cd CS361_Microservice
$ python example.py

# Expected output
The exchange rate from USD to EUR is 0.932527.

Using the Microservice

Sending a Request

Send a request to this microservice by updating data/requests.txt with a request string containing two ISO 4217 Three Letter Currency Codes — e.g. USD for US Dollars, EUR for Euro etc. — separated by a comma.

# Example client-side request using Python
with open("data/reqeusts.txt", w) as request:
    request.write("usd,eur")

Note: Here's the list of supported currency codes.

Receiving a Response

This microservice sends responses to data/response.txt with an exchange rate string — e.g. request: USD,EUR. response: 0.932527.

Visual Request/Response Demo

Sample Client in Python

# Simple example client to run with the Currency Exchange Microservice
import time
import re

# <path_to_requests_file> points to the request file in the data directory
# NOTE: This example assumes the client is at the project root
path_to_requests_file = "data/requests.txt"

# <path_to_response_file> points to the request file in the data directory
# NOTE: This example assumes the client is at the project root
path_to_response_file = "data/response.txt"

# example currencies to exchange
base_currency = "usd"
target_currency = "eur"

# Send the request to the <path_to_request_file>
with open(path_to_requests_file, "w") as out_file:
    out_file.write(f"{base_currency},{target_currency}")

# Poll the <path_to_response_file> until you receive a response
while True:
    time.sleep(1)
    # Receive the response from the <path_to_response_file>
    with open(path_to_response_file, "r") as in_file:
        exchange_rate = in_file.readline()

    # No response received
    if exchange_rate == "":
        continue

    # Valid reponse received
    if re.search("^(0|[1-9]\d*)?(\.\d+)?(?<=\d)$", exchange_rate):
        # acknowledge valid response in <path_to_response_file>
        with open(path_to_response_file, "a") as out_file:
            out_file.write("\nResponse Received")
        break

# View the results
print(f"The exchange rate from USD to EUR is {exchange_rate}.")
# Expected Output
The exchange rate from USD to EUR is 0.932527.

Sequence Diagram

Project Integration

Option 1: Move or copy the data and exchange_rate directories to the root of your project.

# Unix systems
$ cd CS361_Microservice
$ mv exchange_rate data <path-to-project-root>
# windows
> cd CS361_Microservice
> move exchange_rate <path-to-project-root>
> move data <path-to-project-root>
# Run the microservice
$ cd <path-to-project-root>/exchange_rate
$ python main.py
  • You can now send requests to the microservice by sending a valid request string to data/requests.txt.
  • You can now recieve responses from the microservice by reading the information in data/response.txt.

Option 2: Clone this microservice to your project root.

Note: see Getting Started.

Optional: Configure the exchange_rate.py file.

* Open `exchange_rate.py` in a text editor.
* Change the values of any of the `internal variables` section to suit your needs.

Note: Any changes made here will need to be reflected in your client program when communicating with the microservice.

# Run the microservice
$ cd <path-to-project-root>/CS361_Microservice/exchange_rate
$ python main.py
  • You can now send requests to the microservice by sending a valid request string to:
    • No changes made to exchange_rate.py:
      • CS361_Microservice/data/requests.txt.
    • Changes made to exchange_rate.py:
      • <your-changed-directory>/<your-changed-request-file>.
  • You can now recieve responses from the microservice by reading the information from:
    • No changes made to exchange_rate.py:
      • CS361_Microservice/data/response.txt.
    • Changes made to exchange_rate.py:
      • <your-changed-directory>/<your-changed-response-file>.

Note: The microservice must be running BEFORE the client sends a request.

Troubleshooting

  • Check that you have python 3.10+ installed on your local machine

    $ python --version
    Python 3.10.0
  • Check that you have requests 2.28+ installed on your local machine

    $ python -m pip install requests
  • Check that the exchange_rate and data folders are in the root of your project

    project
    |   README.md
    |   project files
    |   ...
    |___data
    |   |   requests.txt
    |   |   response.txt
    |
    |___exchage_rate
    |   |   exchage_rate.py
    |   |   main.py
    |
    |___folder
        |   folder files
        |   ...
        |
        |___subfolder
            |   subfolder files
            |   ...   
    
    • Run the microservice from the exchange_rate directory.
      $ cd my/project/root
      $ python main.py
      • If you do NOT, then you will encounter issues related to the communication files.
        # DO NOT do this!
        $ cd <project-root>
        $ python exchange_rate/main.py # Will cause issues with "requests.txt" and "response.txt"
  • Check that your program is sending requests to data/requests.txt

  • Check that your program is receiving responses from data/response.txt

    project
    |   ...
    |___data
    |   |   ...
    |   |   requests.txt 
    |   |   response.txt
    
    # from project root
    path_to_requests_file = "data/requests.txt"
    path_to_response_file = "data/response.txt"
    project
    |   ...
    |___data
    |   |   ...
    |   |   requests.txt
    |   |   response.txt
    |
    |___folder
        |   ...  
    
    # from project folder
    path_to_requests_file = "../data/requests.txt"
    path_to_response_file = "../data/response.txt"
    project
    |   ...
    |___data
    |   |   ...
    |   |   requests.txt
    |   |   response.txt
    |
    |___folder
        |   ...
        |
        |___subfolder
            |   ...   
    
    # from project subfolder
    path_to_requests_file = "../../data/requests.txt"
    path_to_response_file = "../../data/response.txt"

    Learn more about paths.

Built With

Python 3.10 Requests 2.28.2 ExchangeRate-API

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A currency exchange rate microservice designed for @jay2002shah's CS 361 course project.

Topics

Resources

License

Stars

Watchers

Forks

Languages