def test_api_authenticate(self): api_key = "12345678912345678902" login_with_api = Account.api_authenticate(api_key) self.assertIsInstance( login_with_api, Account, 'checks that an instantiated Account class is returned after authenticating api' )
def set_routing_number(api_key): api_login_attempt = Account.api_authenticate(api_key) if not request.json: return jsonify({'error': 'bad request'}), 400 if 'set_routing_number' not in request.json: return jsonify({'error': 'bad request'}), 400 if api_login_attempt != None: api_login_attempt.set_routing_number( request.json['set_routing_number']) return jsonify( {'routing_number': api_login_attempt.values['routing_number']}) #pep3333 --> wsgi | uwhiskey --> uses binary language (not py) #using nginx instead of apache (open-source and free, apache is bad for scaling will crash after 10k users at the same time) #swap reassign some requests from RAM to disk temporarily #ADD UNIT TESTS FOR ALL ROUTES #CRON --> linux scheduler to check for updates and run codes at certain times #^ good to test that your flask app is running #selenium will move the mouse and click on shit for you --> can do "headless" as well #push to github prior to deploying
def buy_stock(api_key): api_login_attempt = Account.api_authenticate(api_key) if not request.json: return jsonify({'error': 'bad request'}), 400 if 'ticker' not in request.json or 'shares' not in request.json: return jsonify({'error': 'bad request'}), 400 ticker = request.json['ticker'] shares = request.json['shares'] api_login_attempt.buy(ticker, shares) new_position = api_login_attempt.get_position_for(ticker) return jsonify( {"purchase successful, new position is": new_position.json()}) #pep3333 --> wsgi | uwhiskey --> uses binary language (not py) #using nginx instead of apache (open-source and free, apache is bad for scaling will crash after 10k users at the same time) #swap reassign some requests from RAM to disk temporarily #ADD UNIT TESTS FOR ALL ROUTES #CRON --> linux scheduler to check for updates and run codes at certain times #^ good to test that your flask app is running #selenium will move the mouse and click on shit for you --> can do "headless" as well #push to github prior to deploying
def get_trades_for(api_key, ticker): api_login_attempt = Account.api_authenticate(api_key) if api_login_attempt != None: trades = api_login_attempt.trades_for(ticker) return jsonify({"trades": [trade.json() for trade in trades] }) #should i remove pk from response? YES else: return jsonify({"error": "404"})
def get_positions(api_key): api_login_attempt = Account.api_authenticate(api_key) if api_login_attempt != None: positions = api_login_attempt.get_positions() return jsonify({ "positions": [position.json() for position in positions] }) #should i remove pk from response? else: return jsonify({"error": "404"})
def get_routing_number(api_key): api_login_attempt = Account.api_authenticate(api_key) if api_login_attempt != None: return jsonify(({ 'routing_number': api_login_attempt.values['routing_number'] })) else: return jsonify({"error": "404"})
def get_account_info(api_key): api_login_attempt = Account.api_authenticate(api_key) if api_login_attempt != None: return jsonify({ "username": api_login_attempt.values['username'], "balance": api_login_attempt.values['balance'], "first_name": api_login_attempt.values['first'], "last_name": api_login_attempt.values['last'] }) else: return jsonify({"error": "404"})
def position_for(api_key, symbol): account = Account.api_authenticate(api_key) if not account: return jsonify(COULD_NOT_AUTHENTICATE), 401 pos = account.position_for(symbol) try: return jsonify({**pos.json(), "current value": pos.current_value()}) except trade.SymbolNotFound: return jsonify(NO_SUCH_SYMBOL), 404 except trade.APINetworkError: return jsonify(API_NETWORK_ERROR), 500
def set_checking_account_number(api_key): api_login_attempt = Account.api_authenticate(api_key) if not request.json: return jsonify({'error': 'bad request'}), 400 if 'set_checking_account_number' not in request.json: return jsonify({'error': 'bad request'}), 400 if api_login_attempt != None: api_login_attempt.set_checking_account_number( request.json['set_checking_account_number']) return jsonify({ 'checking_account_number': api_login_attempt.values['checking_account_number'] })
def sell_stock(api_key): api_login_attempt = Account.api_authenticate(api_key) if not request.json: return jsonify({'error': 'bad request'}), 400 if 'ticker' not in request.json or 'shares' not in request.json: return jsonify({'error': 'bad request'}), 400 ticker = request.json['ticker'] shares = request.json['shares'] api_login_attempt.sell(ticker, shares) new_position = api_login_attempt.get_position_for(ticker) return jsonify({"sale successful, new position is": new_position.json()})
def put_deposit(api_key): api_login_attempt = Account.api_authenticate(api_key) if not request.json: return jsonify({'error': 'bad request'}), 400 if 'deposit' not in request.json: #if 'deposit' in request.json and type(request.json['deposit']) != bytes: return jsonify({'error': 'bad request'}), 400 if api_login_attempt != None: current_bal = api_login_attempt.values['balance'] new_bal = current_bal + float(request.json['deposit']) api_login_attempt.values['balance'] = new_bal api_login_attempt.save() return jsonify({'balance': api_login_attempt.values['balance']})
def buy(api_key): account = Account.api_authenticate(api_key) if not account: return jsonify(COULD_NOT_AUTHENTICATE), 401 if not request.json or "symbol" not in request.json or "volume" not in request.json: return jsonify(BAD_REQUEST), 400 if not isinstance(request.json["volume"], int): return jsonify(BAD_REQUEST) try: account.buy(request.json["symbol"], request.json["volume"]) except trade.InsufficientFundsError: return jsonify(INSUFFICIENT_FUNDS), 400 except trade.SymbolNotFound: return jsonify(NO_SUCH_SYMBOL), 400 pos = account.position_for(request.json["symbol"]) trd = account.trades_for(request.json["symbol"])[-1] return jsonify({ "buy": { "user": account.json(), "trade": trd.json(), "position": pos.json() } })
def get_balance(api_key): api_login_attempt = Account.api_authenticate(api_key) if api_login_attempt != None: return jsonify(({'balance': api_login_attempt.values['balance']})) else: return jsonify({"error": "404"})
def trades_for(api_key, symbol): account = Account.api_authenticate(api_key) if not account: return jsonify(COULD_NOT_AUTHENTICATE), 401 tradelist = [trade.json() for trade in account.trades_for(symbol)] return jsonify(tradelist)
def positions(api_key): account = Account.api_authenticate(api_key) if not account: return jsonify(COULD_NOT_AUTHENTICATE), 401 poslist = [pos.json() for pos in account.positions()] return jsonify(poslist)
def user_info(api_key): account = Account.api_authenticate(api_key) if not account: return jsonify(COULD_NOT_AUTHENTICATE), 401 return jsonify(account.json())