This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathapi.py
73 lines (54 loc) · 2.04 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
trading-server is a multi-asset, multi-strategy, event-driven execution
and backtesting platform (OEMS) for trading common markets.
Copyright (C) 2020 Sam Breznikar <[email protected]>
Licensed under GNU General Public License 3.0 or later.
Some rights reserved. See LICENSE.md, AUTHORS.md.
"""
from pymongo import MongoClient, errors
from threading import Thread
from flask import Flask, Response, request
from time import sleep
import logging
import sys
import json
DB_URL = 'mongodb://127.0.0.1:27017/'
DB_PRICES = 'asset_price_master'
DB_OTHER = 'holdings_trades_signals_master'
DB_TIMEOUT_MS = 30
db_client = MongoClient(DB_URL, serverSelectionTimeoutMS=DB_TIMEOUT_MS)
db_prices = db_client[DB_PRICES]
db_other = db_client[DB_OTHER]
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s:%(levelname)s:%(module)s - %(message)s",
datefmt="%d-%m-%Y %H:%M:%S")
ch.setFormatter(formatter)
logger.addHandler(ch)
app = Flask(__name__)
# Portfolio data route
@app.route("/portfolio", methods=['GET'])
def return_portfolio():
if request.method == 'GET':
portfolio = db_other['portfolio'].find_one({"id": 1}, {"_id": 0})
if portfolio:
return json.dumps(portfolio), 200, {'ContentType':'application/json'}
else:
return json.dumps({'success': False, 'message': 'Not found'}),
404, {'ContentType':'application/json'}
else:
return json.dumps({'success': False, 'message': 'Invalid method'}),
403, {'ContentType':'application/json'}
# Portfolio settings route
@app.route("/portfolio/settings/<new_state>", methods=['POST'])
def change_portfolio_settings():
if request.method == 'POST':
return " Posted to /portfolio/settings/ successfully"
# TODO: set new portfolio settings
else:
return json.dumps({'success': False, 'message': 'Invalid method'}),
403, {'ContentType':'application/json'}
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=False)