48 lines
1.1 KiB
Python
48 lines
1.1 KiB
Python
from flask import Flask, request, jsonify
|
|
|
|
app = Flask(__name__)
|
|
|
|
accounts = {
|
|
'1': {'amount': 1000},
|
|
'2': {'amount': 1000},
|
|
}
|
|
|
|
transactions = []
|
|
|
|
|
|
# POST endpoint
|
|
@app.route('/transfer', methods = ['POST'])
|
|
def transfer():
|
|
new_balance = request.get_json()
|
|
from_account = new_balance.get('from_account')
|
|
to_account = new_balance.get('to_account')
|
|
desired_amount = new_balance.get('desired_amount')
|
|
|
|
# validate input
|
|
if from from_account not in accounts or to_account not in accounts:
|
|
return jsontify({"error: Invalid account"}), 404
|
|
|
|
# transfer process
|
|
accounts[from_account]['amount'] -= desired_amount
|
|
accounts[to_account]['amount'] += desired_amount
|
|
transactions.append(accounts)
|
|
|
|
# GET endpoint
|
|
@app.route ('/transfer', method=['GET'])
|
|
def get_information():
|
|
new_balance = request.get_json()
|
|
from_account = new_balance.get('from_account')
|
|
to_account = new_balance.get('to_account')
|
|
|
|
# validate input
|
|
if from from_account not in accounts or to_account not in accounts:
|
|
return jsontify({"error: Unknown account"}), 404
|
|
|
|
# get the information
|
|
return jsontify(transactions)
|
|
|
|
|
|
|
|
|
|
|