Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,22 +14,9 @@ import requests
|
|
14 |
|
15 |
# Initialize Flask app
|
16 |
app = Flask(__name__)
|
17 |
-
app.secret_key = os.urandom(24)
|
18 |
|
19 |
-
# β
|
20 |
-
app.config["DEBUG"] = True
|
21 |
-
|
22 |
-
# β
Salesforce Connection Setup
|
23 |
-
try:
|
24 |
-
print("Attempting to connect to Salesforce...")
|
25 |
-
sf = Salesforce(username='[email protected]',
|
26 |
-
password='Sati@1020',
|
27 |
-
security_token='sSSjyhInIsUohKpG8sHzty2q')
|
28 |
-
print("β
Connected to Salesforce successfully!")
|
29 |
-
except Exception as e:
|
30 |
-
print(f"β Failed to connect to Salesforce: {str(e)}")
|
31 |
-
|
32 |
-
# β
Print Available Routes on First Request
|
33 |
@app.before_request
|
34 |
def print_routes():
|
35 |
if not hasattr(app, 'printed_routes'):
|
@@ -46,75 +33,36 @@ def list_routes():
|
|
46 |
routes.append({"endpoint": rule.endpoint, "route": str(rule)})
|
47 |
return jsonify({"available_routes": routes})
|
48 |
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
# β
REGISTER API
|
51 |
@app.route("/register", methods=["POST"])
|
52 |
def register():
|
53 |
print("β‘ Register API hit")
|
54 |
data = request.json
|
55 |
-
|
56 |
-
email = data.get("email")
|
57 |
-
phone_number = data.get("phone")
|
58 |
-
|
59 |
-
if not name or not email or not phone_number:
|
60 |
return jsonify({"error": "Missing required fields"}), 400
|
|
|
61 |
|
62 |
-
|
63 |
-
new_customer = sf.Customer_Login__c.create({
|
64 |
-
'Name': name,
|
65 |
-
'Email__c': email,
|
66 |
-
'Phone_Number__c': phone_number
|
67 |
-
})
|
68 |
-
return jsonify({"success": True, "message": "Registration successful", "customer_id": new_customer["id"]})
|
69 |
-
except Exception as e:
|
70 |
-
return jsonify({"error": f"Failed to register: {str(e)}"}), 500
|
71 |
-
|
72 |
-
# β
LOGIN API: Validate user credentials
|
73 |
@app.route("/login", methods=["POST"])
|
74 |
def login():
|
75 |
print("β‘ Login API hit")
|
76 |
data = request.json
|
77 |
-
|
78 |
-
phone_number = data.get("phone")
|
79 |
-
|
80 |
-
if not email or not phone_number:
|
81 |
return jsonify({"error": "Missing email or phone"}), 400
|
|
|
82 |
|
83 |
-
|
84 |
-
query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone_number}'"
|
85 |
-
result = sf.query(query)
|
86 |
-
|
87 |
-
if result["totalSize"] > 0:
|
88 |
-
user = result["records"][0]
|
89 |
-
session["customer_id"] = user["Id"] # Store in session
|
90 |
-
return jsonify({"success": True, "message": "Login successful", "customer_id": user["Id"], "name": user["Name"]})
|
91 |
-
else:
|
92 |
-
return jsonify({"error": "Invalid login credentials"}), 401
|
93 |
-
except Exception as e:
|
94 |
-
return jsonify({"error": f"Login failed: {str(e)}"}), 500
|
95 |
-
|
96 |
-
# β
MENU API: Fetch menu items from Salesforce
|
97 |
@app.route("/menu", methods=["GET"])
|
98 |
def get_menu():
|
99 |
print("β‘ Menu API hit")
|
100 |
-
|
101 |
-
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
102 |
-
result = sf.query(query)
|
103 |
-
|
104 |
-
menu_items = []
|
105 |
-
for item in result["records"]:
|
106 |
-
menu_items.append({
|
107 |
-
"name": item["Name"],
|
108 |
-
"price": item["Price__c"],
|
109 |
-
"ingredients": item["Ingredients__c"],
|
110 |
-
"category": item["Category__c"]
|
111 |
-
})
|
112 |
-
|
113 |
-
return jsonify({"success": True, "menu": menu_items})
|
114 |
-
except Exception as e:
|
115 |
-
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
116 |
|
117 |
-
# β
START
|
118 |
if __name__ == "__main__":
|
119 |
print("β
Starting Flask API Server on port 7860...")
|
120 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
14 |
|
15 |
# Initialize Flask app
|
16 |
app = Flask(__name__)
|
17 |
+
app.secret_key = os.urandom(24)
|
18 |
|
19 |
+
# β
Print Available Routes at Startup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
@app.before_request
|
21 |
def print_routes():
|
22 |
if not hasattr(app, 'printed_routes'):
|
|
|
33 |
routes.append({"endpoint": rule.endpoint, "route": str(rule)})
|
34 |
return jsonify({"available_routes": routes})
|
35 |
|
36 |
+
# β
HOME ROUTE
|
37 |
+
@app.route("/", methods=["GET"])
|
38 |
+
def home():
|
39 |
+
return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, or /routes to check available endpoints."})
|
40 |
|
41 |
+
# β
REGISTER API
|
42 |
@app.route("/register", methods=["POST"])
|
43 |
def register():
|
44 |
print("β‘ Register API hit")
|
45 |
data = request.json
|
46 |
+
if not data or "name" not in data or "email" not in data or "phone" not in data:
|
|
|
|
|
|
|
|
|
47 |
return jsonify({"error": "Missing required fields"}), 400
|
48 |
+
return jsonify({"success": True, "message": "Registration successful"})
|
49 |
|
50 |
+
# β
LOGIN API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
@app.route("/login", methods=["POST"])
|
52 |
def login():
|
53 |
print("β‘ Login API hit")
|
54 |
data = request.json
|
55 |
+
if not data or "email" not in data or "phone" not in data:
|
|
|
|
|
|
|
56 |
return jsonify({"error": "Missing email or phone"}), 400
|
57 |
+
return jsonify({"success": True, "message": "Login successful"})
|
58 |
|
59 |
+
# β
MENU API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
@app.route("/menu", methods=["GET"])
|
61 |
def get_menu():
|
62 |
print("β‘ Menu API hit")
|
63 |
+
return jsonify({"success": True, "menu": [{"name": "Biryani", "price": 10}]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
# β
START SERVER
|
66 |
if __name__ == "__main__":
|
67 |
print("β
Starting Flask API Server on port 7860...")
|
68 |
serve(app, host="0.0.0.0", port=7860)
|