def cancelTransaction(number): res = "" res_code = 400 if db.cancelTransaction(number) == 0: res = {'number': number} res_code = 200 return niceJson(res, res_code)
def transactionsIndex(): accNum = request.args.get('accNum') if accNum: res = db.getAllTransactionsForAcc(accNum, json=True) else: res = db.getAllTransactions() return niceJson(res, 200)
def removeUser(userID): if db.isUserID(userID): db.removeUserID(userID) res = "User (userID=%s) removed" % userID else: res = "User (userID=%s) non existent" % userID return niceJson({'Result': res}), 201
def accountsPost(): if not request.json or not 'userID' in request.json: abort(400) res = {} res_code = 400 try: userID = int(request.json['userID']) accNum = db.createAccountForUserId(userID, DEFAULT_BALANCE) if accNum != 1: res = {'accNum': accNum} res_code = 200 except ValueError: msg = "UserID expected integer, received: %s" % request.json['userID'] logger.warning(msg) res = {'msg': msg} logger.info("niceJson(res): %s, %s" % (niceJson(res, res_code), res)) return niceJson(res, res_code)
def usersIndex(): res_code = 400 username = request.args.get('username') if username: res = {'id': db.getByUsername(username, json=True)['id']} else: res = db.getAllUsers(json=True) if res: res_code = 200 return niceJson(res, res_code)
def accountsGet(): userID = request.args.get('userID') if userID: res = db.getAccountsByUserId(userID, json=True) else: res = db.getAllAccounts() if res: res_code = 200 else: res = {} res_code = 400 return niceJson(res, res_code)
def accountsAccNumDel(accNum): res = {} res_code = 400 try: accNum = int(accNum) if db.closeAccount(accNum) == 0: res = request.json res_code = 200 except ValueError: msg = "accNum expected integer, received: %s" % request.json['accNum'] logger.warning(msg) res = {'msg': msg} return niceJson(res, res_code)
def accountsAccNumGet(accNum): res = {} res_code = 400 try: accNum = int(accNum) res = db.getAccountByNum(accNum, json=True) if res: res_code = 200 except ValueError: msg = "accNum expected integer, received: %s" % request.json['accNum'] logger.warning(msg) res = {'msg': msg} return niceJson(res, res_code)
def postTransaction(): if (not request.json or not 'fromAccNum' in request.json or not 'toAccNum' in request.json or not 'amount' in request.json): abort(400) fromAccNum = request.json['fromAccNum'] toAccNum = request.json['toAccNum'] amount = int(request.json['amount']) res = "" res_code = 400 transNum = db.addTransactionBetweenAccs(fromAccNum, toAccNum, amount) if transNum != 0: res = {'number': transNum} res_code = 200 return niceJson(res, res_code)
def addUser(): if not request.json or not 'username' in request.json or \ not 'pwd' in request.json: abort(400) username = request.json['username'] res = "" res_code = 400 if not db.isUserName(username): pwd = request.json['pwd'] userJson = db.createUser(username, pwd) if userJson != 1 and userJson != 2 and 'id' in userJson: res = {'id': userJson['id']} res_code = 200 else: res = "User already exists" return niceJson(res, res_code)
def login(): if not request.json or not 'username' in request.json or \ not 'pwd' in request.json: abort(400) res = {} res_code = 403 username = request.json['username'] pwd = request.json['pwd'] isAllowedCode = db.isUserAllowed(username, pwd) if isAllowedCode == 0: if TOKEN_REQUIRED: accessToken = requests.securityToken.getToken(username) if accessToken: res = {'access_token': accessToken} res_code = 201 return niceJson(res, res_code)
def accountsAccNumPost(accNum): if not request.json or not 'amount' in request.json: abort(400) res = {} res_code = 400 try: accNum = int(accNum) amount = int(request.json['amount']) new_balance = db.updateAccount(accNum, amount) if new_balance: res = {'balance': new_balance} res_code = 200 except ValueError: msg = "Expected integers: accNum=%s, amount=%s" \ % (request.json['accNum'], request.json['amount']) logger.warning(msg) res = {'msg': msg} return niceJson(res, res_code)
def pay(): if (not request.json or not 'fromAccNum' in request.json or not 'toAccNum' in request.json or not 'amount' in request.json): abort(400) fromAccNum = request.json['fromAccNum'] toAccNum = request.json['toAccNum'] amount = float(request.json['amount']) res = "" res_code = 400 transNum = postTransaction(fromAccNum, toAccNum, amount) try: if transNum: if (not updateAccount(fromAccNum, -amount) or not updateAccount(toAccNum, amount)): cancelTransaction(transNum) else: res_code = 200 except Exception as e: logger.error("Payment: %s" % e) return niceJson(res, res_code)
def hello(): return niceJson({"subresource_uris": allLinks(app)}, 200)
def getTransaction(number): return niceJson(db.getTransactionByNum(number, json=True)), 200