Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,40 @@
|
|
1 |
from flask import Flask, render_template, send_from_directory, request, jsonify
|
2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
5 |
|
6 |
-
# Salesforce
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
@app.route('/')
|
11 |
def index():
|
@@ -17,6 +46,9 @@ def serve_static(filename):
|
|
17 |
|
18 |
@app.route('/get_ingredients', methods=['POST'])
|
19 |
def get_ingredients():
|
|
|
|
|
|
|
20 |
dietary_preference = request.json.get('dietary_preference', '').lower()
|
21 |
|
22 |
# Salesforce SOQL query based on dietary preference
|
@@ -28,7 +60,7 @@ def get_ingredients():
|
|
28 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c LIMIT 100" # Default case
|
29 |
|
30 |
headers = {
|
31 |
-
"Authorization": f"Bearer {
|
32 |
"Content-Type": "application/json"
|
33 |
}
|
34 |
url = f"{SFDC_INSTANCE_URL}/services/data/v57.0/query/?q={soql}"
|
|
|
1 |
from flask import Flask, render_template, send_from_directory, request, jsonify
|
2 |
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load environment variables from .env file
|
7 |
+
load_dotenv()
|
8 |
|
9 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
10 |
|
11 |
+
# Salesforce credentials from environment variables
|
12 |
+
SFDC_USERNAME = os.getenv('SFDC_USERNAME')
|
13 |
+
SFDC_PASSWORD = os.getenv('SFDC_PASSWORD')
|
14 |
+
SFDC_SECURITY_TOKEN = os.getenv('SFDC_SECURITY_TOKEN')
|
15 |
+
SFDC_INSTANCE_URL = os.getenv('SFDC_INSTANCE_URL', 'https://login.salesforce.com')
|
16 |
+
|
17 |
+
# Function to get Salesforce access token
|
18 |
+
def get_salesforce_access_token():
|
19 |
+
auth_url = f"{SFDC_INSTANCE_URL}/services/oauth2/token"
|
20 |
+
data = {
|
21 |
+
'grant_type': 'password',
|
22 |
+
'client_id': 'your_client_id', # Replace with your Connected App Client ID
|
23 |
+
'client_secret': 'your_client_secret', # Replace with your Connected App Client Secret
|
24 |
+
'username': SFDC_USERNAME,
|
25 |
+
'password': f"{SFDC_PASSWORD}{SFDC_SECURITY_TOKEN}"
|
26 |
+
}
|
27 |
+
|
28 |
+
try:
|
29 |
+
response = requests.post(auth_url, data=data)
|
30 |
+
response.raise_for_status()
|
31 |
+
return response.json()['access_token']
|
32 |
+
except requests.exceptions.RequestException as e:
|
33 |
+
print(f"Error getting access token: {e}")
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Cache the access token
|
37 |
+
access_token = get_salesforce_access_token()
|
38 |
|
39 |
@app.route('/')
|
40 |
def index():
|
|
|
46 |
|
47 |
@app.route('/get_ingredients', methods=['POST'])
|
48 |
def get_ingredients():
|
49 |
+
if not access_token:
|
50 |
+
return jsonify({"error": "Failed to authenticate with Salesforce"}), 500
|
51 |
+
|
52 |
dietary_preference = request.json.get('dietary_preference', '').lower()
|
53 |
|
54 |
# Salesforce SOQL query based on dietary preference
|
|
|
60 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c LIMIT 100" # Default case
|
61 |
|
62 |
headers = {
|
63 |
+
"Authorization": f"Bearer {access_token}",
|
64 |
"Content-Type": "application/json"
|
65 |
}
|
66 |
url = f"{SFDC_INSTANCE_URL}/services/data/v57.0/query/?q={soql}"
|