Browse Source

feat(stock-market): update exchange_rate endpoint in mock server

nprimo 2 weeks ago
parent
commit
93f5f3a1e3
  1. 21
      subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py
  2. 6
      subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt
  3. 18
      subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py

21
subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py

@ -1,15 +1,16 @@
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from flask_cors import CORS
from random import uniform
import time
from utils import load_historical_data
from utils import load_data
app = Flask(__name__)
CORS(app)
start_time = time.time()
historical_data = load_historical_data()
historical_data = load_data()
@app.route('/exchange_rate/<symbol>')
@ -17,16 +18,14 @@ def get_stock_data(symbol):
if symbol not in list(historical_data.keys()):
return jsonify("Invalid symbol")
current_time = time.time()
last_value = historical_data[symbol].Close[-1]
step = (int(current_time * 10) - int(start_time * 10)
) % len(historical_data[symbol])
try:
return jsonify({
'symbol': 'USD',
'rate': float(historical_data[symbol][step]),
'timestamp': current_time
})
except:
return "Server Error"
return jsonify({
'symbol': 'USD',
'rate': last_value * (1 + uniform(0.05, -0.05) + step * 0.0005),
'timestamp': current_time
})
@app.route('/stocks_list')

6
subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt

@ -5,4 +5,10 @@ Flask-Cors==4.0.0
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.3
numpy==1.26.4
pandas==2.2.2
python-dateutil==2.9.0.post0
pytz==2024.1
six==1.16.0
tzdata==2024.1
Werkzeug==3.0.1

18
subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py

@ -1,5 +1,6 @@
import os
import csv
import pandas as pd
def load_historical_data(directory_path='./sample-stocks/'):
@ -19,6 +20,21 @@ def load_historical_data(directory_path='./sample-stocks/'):
return historical_data
def load_data(directory_path='./sample-stocks/'):
historical_data = {}
file_list = [filename for filename in os.listdir(
directory_path) if filename.endswith(".csv")]
for filename in file_list:
symbol = filename.replace(".csv", "")
file_path = os.path.join(directory_path, filename)
historical_data[symbol] = pd.read_csv(
file_path, index_col=0, parse_dates=True)
return historical_data
if __name__ == "__main__":
result = load_historical_data()
result = load_data()
print(f'keys: {result.keys()}')
print(result["AE"].Close[-1])

Loading…
Cancel
Save