|
| 1 | +# api.py |
| 2 | + |
| 3 | +from flask import Flask, request, jsonify |
| 4 | +from flask_restful import Api, Resource |
| 5 | +from gfsi import GlobalFinancialSystemIntegration |
| 6 | + |
| 7 | +app = Flask(__name__) |
| 8 | +api = Api(app) |
| 9 | + |
| 10 | +gfsi = GlobalFinancialSystemIntegration(private_key=GlobalFinancialSystemIntegration().generate_private_key(), network_id='my_network') |
| 11 | + |
| 12 | +class Account(Resource): |
| 13 | + def post(self): |
| 14 | + account_holder = request.json['account_holder'] |
| 15 | + initial_balance = request.json['initial_balance'] |
| 16 | + account_data = gfsi.create_account(account_holder, initial_balance) |
| 17 | + return jsonify(account_data) |
| 18 | + |
| 19 | + def get(self, account_id): |
| 20 | + account_data = gfsi.get_account(account_id) |
| 21 | + if account_data: |
| 22 | + return jsonify(account_data) |
| 23 | + else: |
| 24 | + return jsonify({'error': 'Account not found'}), 404 |
| 25 | + |
| 26 | +class Transaction(Resource): |
| 27 | + def post(self): |
| 28 | + sender_account_id = request.json['sender_account_id'] |
| 29 | + recipient_account_id = request.json['recipient_account_id'] |
| 30 | + amount = request.json['amount'] |
| 31 | + transaction_data = gfsi.create_transaction(sender_account_id, recipient_account_id, amount) |
| 32 | + signature = gfsi.sign_transaction(transaction_data) |
| 33 | + return jsonify({'transaction_id': transaction_data['id'], 'signature': signature.hex()}) |
| 34 | + |
| 35 | + def put(self, transaction_id): |
| 36 | + signature = request.json['signature'] |
| 37 | + transaction_data = gfsi.transactions[transaction_id] |
| 38 | + if gfsi.verify_signature(transaction_data, bytes.fromhex(signature)): |
| 39 | + gfsi.execute_transaction(transaction_data, bytes.fromhex(signature)) |
| 40 | + return jsonify({'message': 'Transaction executed successfully'}) |
| 41 | + else: |
| 42 | + return jsonify({'error': 'Invalid signature'}), 401 |
| 43 | + |
| 44 | +class ExternalSystemIntegration(Resource): |
| 45 | + def post(self): |
| 46 | + external_system_url = request.json['external_system_url'] |
| 47 | + transaction_id = request.json['transaction_id'] |
| 48 | + transaction_data = gfsi.transactions[transaction_id] |
| 49 | + gfsi.integrate_with_external_system(external_system_url, transaction_data) |
| 50 | + return jsonify({'message': 'Transaction integrated with external system successfully'}) |
| 51 | + |
| 52 | +api.add_resource(Account, '/account', '/account/<string:account_id>') |
| 53 | +api.add_resource(Transaction, '/transaction', '/transaction/<string:transaction_id>') |
| 54 | +api.add_resource(ExternalSystemIntegration, '/external-system') |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + app.run(debug=True) |
0 commit comments