From efa1afc972722da03570df0e6e7484c3004579a3 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 23 Aug 2017 03:16:16 -0400 Subject: [PATCH] changes Signed-off-by: Kenneth Reitz --- server.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index c808ea7..e146f17 100644 --- a/server.py +++ b/server.py @@ -7,8 +7,10 @@ session = requests.Session() MARKETCAP_ALL_URL = 'https://coinmarketcap-nexuist.rhcloud.com/api/all' MARKETCAP_COIN_TEMPLATE = 'https://coinmarketcap-nexuist.rhcloud.com/api/{ticker}' + class Coin(): """A Coin, unlike Mario's.""" + def __init__(self, ticker): self.ticker = ticker self.name = None @@ -17,7 +19,6 @@ class Coin(): self.update() - def _get(self): url = MARKETCAP_COIN_TEMPLATE.format(ticker=self.ticker) r = session.get(url) @@ -67,8 +68,9 @@ def hello(): return jsonify(urls=[ {'/coins': 'Returns all known coins.'}, {'/:coin': 'Returns current value and rank of given coin.'}, - {'/:coin/:coin': 'Returns current exchange rate of two given coins.'}, - {'/:coin/:coin/:n': 'Returns the current value n coins, in any other coin.'}, + {'/:coin/:n': 'Returns current value of n coins.'}, + {'/:coin/to/:coin': 'Returns current exchange rate of two given coins.'}, + {'/:coin/:n/to/:coin/': 'Returns the current value n coins, in any other coin.'}, ]) @@ -84,27 +86,48 @@ def get_coin(coin): 'name': c.name, 'ticker': c.ticker, 'rank': c.rank, - 'value': c.usd + 'value': c.usd, + 'value.currency': 'USD' }) -@app.route('//') +@app.route('//') +def get_value(coin, n): + c = Coin(coin) + return jsonify(coin={ + 'value': c.usd * n, + 'value.currency': 'USD', + 'exchange_rate': c.usd + }) + + +@app.route('//') +def get_value_int(coin, n): + return get_value(coin, n) + + +@app.route('//to/') def get_exchange(coin1, coin2): c = Coin(coin1) return jsonify(coin={ # 'name': c.name, # 'ticker': c.ticker, - 'value': c.value(coin2), - 'value.coin': coin2 + 'exchange_rate': c.value(coin2), }) -@app.route('///') + +@app.route('///to//') def get_exchange_value(coin1, coin2, n): - n = float(n) c = Coin(coin1) return jsonify(coin={ # 'name': c.name, # 'ticker': c.ticker, 'value': c.value(coin2) * n, + 'value.currency': 'USD', 'exchange_rate': c.value(coin2) - }) \ No newline at end of file + }) + + +@app.route('///to//') +def get_exchange_value_int(coin1, coin2, n): + return get_exchange_value(coin1, coin2, n)