def testAPIAuthenticate(self): wrongapi = User.api_authenticate("00000000000000000000") self.assertIsNone(wrongapi, "bad api key return the None object") mike = User.api_authenticate("11111111111111111111") self.assertEqual(mike.real_name, "Mike Bloom", "good credentials retrieve User object")
def testApiKeyAuth(self): mike = User.from_pk(1) mike_from_api_key = User.api_authenticate(mike.api_key) self.assertEqual(mike.realname, mike_from_api_key.realname, "got the same mike from two ways") jack = User.api_authenticate('0000000') self.assertIsNone(jack, "no user is found")
def get_position(ticker, api_key): # curl http://localhost:5000/api/position/pfe/32641019545724871837 """ return a json object of a user's position for a given stock symbol """ user = User.api_authenticate(api_key) position = user.positions_for_stock(ticker) return jsonify(position.json())
def get_positions(api_key): """ return a json list of dictionaries of the user's positions """ user = User.api_authenticate(api_key) if user is None: abort(404) return jsonify( {"positions": [position.json() for position in user.all_positions()]})
def get_trades_for(ticker, api_key): """ return a json list of dictionaries representing all trades for a given stock """ user = User.api_authenticate(api_key) if user is None: abort(404) return jsonify( {"trades": [trade.json() for trade in user.trades_for(ticker)]})
def testApi_authenticate(self): notauser = User.api_authenticate("notmypassword") self.assertIsNone(notauser, "bad api key returns the None object") jack = User( **{ "username": "******", "realname": "Jack Smith", "balance": 10000000.0, "password": "******" }) jack.generate_api_key() jack.save() test1 = User.api_authenticate(jack.api_key) self.assertEqual(test1.username, "jackcoin", "good api key retrieves User object")
def deposit(api_key): if not request.json or "amount" not in request.json: return jsonify({"error": "deposit requires json with 'amount' key"}), 400 amount = request.json["amount"] user = User.api_authenticate(api_key) user.balance += amount user.save() return jsonify(user.json()), 201
def deposit(api_key): # curl -i -H "Content-Type: application/json" -X POST -d '{"amount":200.0}' http://localhost:5000/api/deposit/32641019545724871837 if not request.json or "amount" not in request.json: return jsonify({"error": "deposit requires json with 'amount' key"}), 400 amount = request.json["amount"] user = User.api_authenticate(api_key) user.balance += amount user.save() return jsonify({"deposit":amount}), 201
def get_positions(api_key): # curl http://localhost:5000/api/positions/32641019545724871837 """ return a json list of dictionaries of the user's positions """ user = User.api_authenticate(api_key) if user is None: abort(404) positions = user.all_positions() if positions is None: return {"User has no positions"} return jsonify([position.json() for position in positions])
def sell(api_key): """ sell a certain amount of a certain stock, amount & symbol provided in a json POST request """ if not request.json or "ticker" not in request.json or 'amount' not in request.json: return jsonify( {"error": "deposit requires json with 'ticker' and 'amount key"}), 400 user = User.api_authenticate(api_key) if user is None: abort(404) user.sell(request.json['ticker'], request.json['amount']) return jsonify({"balance": user.balance})
def buy(api_key): """ buy a certain amount of a certain stock, amount & symbol provided in a json POST request """ if not request.json or "ticker" not in request.json or 'amount' not in request.json: return jsonify( {"error": "deposit requires json with 'ticker' and 'amount key"}), 400 user = User.api_authenticate(api_key) if user is None: abort(404) user.buy(request.json['ticker'], request.json['amount']) return jsonify( {"position": user.position_for_stock(request.json['ticker']).json()})
def sell(api_key): # curl -i -H "Content-Type: application/json" -X POST -d '{"shares":2, "ticker":"nvda"}' http://localhost:5000/api/sell/32641019545724871837 """ sell a certain amount of a certain stock, amount & symbol provided in a json POST request """ if not request.json: return jsonify({"error": "requires json with 'shares' & 'ticker' key"}), 400 if "ticker" not in request.json or "shares" not in request.json: return jsonify({"error": "requires json with 'shares' & 'ticker' key"}), 400 shares = request.json["shares"] ticker = request.json["ticker"] user = User.api_authenticate(api_key) try: sell_shares = user.sell(ticker, shares) return jsonify(sell_shares.json()) except InsufficientSharesError: return jsonify({"error": "Insufficient Shares"})
def sell(api_key): """ sell a certain amount of a certain stock, amount & symbol provided in a json POST request """ if not request.json or "ticker" not in request.json or 'amount' not in request.json: return jsonify( {"error": "deposit requires json with 'ticker' and 'amount key"}), 400 user = User.api_authenticate(api_key) if user is None: abort(404) try: user.sell(request.json['ticker'], request.json['amount']) except InsufficientSharesError: return jsonify({"invalid": "amount"}) if user.position_for_stock(request.json['ticker']) is None: return jsonify({"invalid": "amount"}) return jsonify( {"position": user.position_for_stock(request.json['ticker']).json()})
def get_trades_for(ticker, api_key): # curl http://localhost:5000/api/trades/tsla/32641019545724871837 """ return a json list of dictionaries representing all trades for a given stock """ user = User.api_authenticate(api_key) trades = user.trades_for(ticker) return jsonify([trade.json() for trade in trades])
def get_trades(api_key): """ return a json list of dictionaries representing all of a user's trades """ user = User.api_authenticate(api_key) if user is None: abort(404) return jsonify({"trades": [trade.json() for trade in user.all_trades()]})
def returnUser(api_key): user = User.api_authenticate(api_key) if user is None: abort(404) return jsonify(user.json())
def get_position(ticker, api_key): """ return a json object of a user's position for a given stock symbol """ user = User.api_authenticate(api_key) if user is None: abort(404) return jsonify({"positions": user.position_for_stock(ticker).json()})
def get_trades(api_key): # curl http://localhost:5000/api/trades/32641019545724871837 """ return a json list of dictionaries representing all of a user's trades """ user = User.api_authenticate(api_key) trades = user.all_trades() return jsonify([trade.json() for trade in trades])