-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency_api.py
49 lines (34 loc) · 1.17 KB
/
currency_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
from dotenv import load_dotenv
import pandas as pd
from tools import ZipDownloader, Unzipper, CurrencyExtractor
import logging
from flask import Flask, request, jsonify
load_dotenv()
logging.basicConfig(
filename=os.getenv("LOG_FILE"),
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
app = Flask(__name__)
# create zipDownloader instance
zip_downloader = ZipDownloader(os.getenv("ZIP_URL"))
# create unzipper instance
unzipper = Unzipper(os.getenv("FILE_TO_EXTRACT"), zip_downloader.get_file())
# create dataframe
df = pd.read_csv(unzipper.get_data())
# drop columns where all values = nan
df.dropna(axis=1, how='all', inplace=True)
@app.route("/<date>/<currency>", methods=['GET'])
def endpoint(date, currency):
try:
# Create currency extractor instance
currency_extractor = CurrencyExtractor(date, currency, df)
result = currency_extractor.get_value()
return jsonify(result)
except BaseException:
error = {
'error': 'Something wrong happend, please read the log for more information',
}
return jsonify(error), 404
if __name__ == '__main__':
app.run()