forked from atx-crypto-club/atxcf
/
webapi.py
73 lines (55 loc) · 1.82 KB
/
webapi.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
import argparse
from flask import Flask
from flask.ext.cors import CORS
#from flask import jsonify
import prices
import PriceNetwork
class ProxySource(PriceNetwork.PriceNetwork):
"""
Gets price info from a price source web API.
TODO: finish!
"""
pass
app = Flask(__name__)
CORS(app)
_source = None
@app.route('/')
def index():
return '~~ atxcf-bot ~~'
@app.route('/prices')
@app.route('/get_fund_asset_prices')
def get_fund_asset_prices():
price_d = prices.get_fund_asset_prices()
output = "<p>"
for key, val in price_d.iteritems():
if isinstance(val, str):
output += "{0}: {1}<br />".format(key, val)
else:
output += "{0}: {1:.8f}<br />".format(key, val)
output += "</p>"
return output
@app.route('/get_symbols')
def get_symbols():
if _source == None:
raise PriceNetwork.PriceSource.PriceSourceError("No price source set")
symbols = _source.get_symbols()
return " ".join(sorted(symbols))
@app.route('/get_price/<from_asset>/<to_asset>', defaults={'value': 1.0})
@app.route('/get_price/<from_asset>/<to_asset>/<value>')
def get_price(from_asset, to_asset, value):
if _source == None:
raise PriceNetwork.PriceSource.PriceSourceError("No price source set")
price = _source.get_price(from_asset, to_asset, value)
return str(price)
@app.route('/get_markets')
def get_markets():
if _source == None:
raise PriceNetwork.PriceSource.PriceSourceError("No price source set")
mkts = _source.get_markets()
return " ".join(sorted(mkts))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Launches atxcf price API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
_source = PriceNetwork.PriceNetwork()
app.run(host='0.0.0.0', port=1337, threaded=True)