Ecinetwireless Backend Code Bulk
Ecinetwireless Backend Code Bulk
import xmlrpc.client
import xmltodict
import os
import logging
import re
import pandas as pd
# Configure logging
logging.basicConfig(level=logging.DEBUG)
if isinstance(result, str):
# Use xmltodict for simplified XML parsing
try:
response_data = xmltodict.parse(result)
return response_data
except Exception as e:
logging.error(f"Failed to parse XML: {e}")
return None
else:
logging.error(f"Unexpected response from Econet API: {result}")
return None
except xmlrpc.client.Fault as e:
logging.error(f"XML-RPC Fault: {e}")
return None
except Exception as e:
logging.error(f"An error occurred: {e}")
return None
@app.route("/api/airtime/load", methods=["POST"])
def load_airtime():
"""Loads airtime to a specified MSISDN."""
data = request.get_json()
msisdn = data.get("msisdn")
amount = data.get("amount")
reference = data.get("reference")
currency = data.get("currency", 840) # Default to USD (840) if not provided
api_params = {
"MSISDN": str(msisdn),
"Amount": amount,
"Reference": str(reference),
"Currency": currency # Defaults to 840 (USD)
}
@app.route("/api/airtime/bulk/load", methods=["POST"])
def bulk_load_airtime():
"""Handles bulk airtime loading from an Excel file containing multiple
MSISDNs."""
if "file" not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No selected file"}), 400
try:
# Read the Excel file using pandas
df = pd.read_excel(file)
if invalid_msisdns:
return jsonify({"error": f"Invalid MSISDN formats: {',
'.join(invalid_msisdns)}"}), 400
except Exception as e:
logging.error(f"Error processing bulk airtime load: {e}")
return jsonify({"error": "An error occurred while processing the bulk
load"}), 500
if __name__ == "__main__":
app.run(debug=True)