Spaces:
Running
Running
Create Bubble_API_Calls.py
Browse files- Bubble_API_Calls.py +88 -0
Bubble_API_Calls.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# bubble_api_calls.py
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import requests
|
5 |
+
|
6 |
+
def fetch_linkedin_token_from_bubble(url_user_token_str: str):
|
7 |
+
"""
|
8 |
+
Fetches LinkedIn access token from Bubble.io API using the state value (url_user_token_str).
|
9 |
+
The token is expected in a 'Raw_text' field as a JSON string, which is then parsed.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
url_user_token_str: The state value (token from URL) to query Bubble.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
tuple: (parsed_token_dict, status_message)
|
16 |
+
parsed_token_dict is the dictionary containing the token (e.g., {"access_token": "value"})
|
17 |
+
or None if an error occurred or token not found.
|
18 |
+
status_message is a string describing the outcome of the API call.
|
19 |
+
"""
|
20 |
+
bubble_api_key = os.environ.get("Bubble_API")
|
21 |
+
if not bubble_api_key:
|
22 |
+
error_msg = "❌ Bubble API Error: The 'Bubble_API' environment variable is not set."
|
23 |
+
print(error_msg)
|
24 |
+
return None, error_msg
|
25 |
+
|
26 |
+
if not url_user_token_str or "not found" in url_user_token_str or "Could not access" in url_user_token_str:
|
27 |
+
status_msg = f"ℹ️ No valid user token from URL to query Bubble. ({url_user_token_str})"
|
28 |
+
print(status_msg)
|
29 |
+
return None, status_msg
|
30 |
+
|
31 |
+
base_url = "https://app.ingaze.ai/version-test/api/1.1/obj/Linkedin_access"
|
32 |
+
constraints = [{"key": "state", "constraint_type": "equals", "value": url_user_token_str}]
|
33 |
+
params = {'constraints': json.dumps(constraints)}
|
34 |
+
headers = {"Authorization": f"Bearer {bubble_api_key}"}
|
35 |
+
|
36 |
+
status_message = f"Attempting to fetch token from Bubble for state: {url_user_token_str}..."
|
37 |
+
print(status_message)
|
38 |
+
parsed_token_dict = None
|
39 |
+
response = None
|
40 |
+
|
41 |
+
try:
|
42 |
+
response = requests.get(base_url, params=params, headers=headers, timeout=15)
|
43 |
+
response.raise_for_status()
|
44 |
+
|
45 |
+
data = response.json()
|
46 |
+
results = data.get("response", {}).get("results", [])
|
47 |
+
|
48 |
+
if results:
|
49 |
+
raw_text_from_bubble = results[0].get("Raw_text", None)
|
50 |
+
|
51 |
+
if raw_text_from_bubble and isinstance(raw_text_from_bubble, str):
|
52 |
+
try:
|
53 |
+
temp_parsed_dict = json.loads(raw_text_from_bubble)
|
54 |
+
if isinstance(temp_parsed_dict, dict) and "access_token" in temp_parsed_dict:
|
55 |
+
parsed_token_dict = temp_parsed_dict # Successfully parsed and has access_token
|
56 |
+
status_message = f"✅ LinkedIn Token successfully fetched and parsed from Bubble 'Raw_text' for state: {url_user_token_str}"
|
57 |
+
elif isinstance(temp_parsed_dict, dict):
|
58 |
+
status_message = (f"⚠️ Bubble API: 'access_token' key missing in parsed 'Raw_text' dictionary for state: {url_user_token_str}. Parsed: {temp_parsed_dict}")
|
59 |
+
else: # Not a dict
|
60 |
+
status_message = (f"⚠️ Bubble API: 'Raw_text' field did not contain a valid JSON dictionary string. "
|
61 |
+
f"Content type: {type(raw_text_from_bubble)}, Value: {raw_text_from_bubble}")
|
62 |
+
except json.JSONDecodeError as e:
|
63 |
+
status_message = (f"⚠️ Bubble API: Error decoding 'Raw_text' JSON string: {e}. "
|
64 |
+
f"Content: {raw_text_from_bubble}")
|
65 |
+
elif raw_text_from_bubble: # It exists but is not a string
|
66 |
+
status_message = (f"⚠️ Bubble API: 'Raw_text' field was not a string. "
|
67 |
+
f"Type: {type(raw_text_from_bubble)}, Value: {raw_text_from_bubble}")
|
68 |
+
else: # Raw_text not found or null
|
69 |
+
status_message = (f"⚠️ Bubble API: Token field ('Raw_text') "
|
70 |
+
f"not found or is null in response for state: {url_user_token_str}. Result: {results[0]}")
|
71 |
+
else: # No results from Bubble for the given state
|
72 |
+
status_message = f"❌ Bubble API: No results found for state: {url_user_token_str}"
|
73 |
+
|
74 |
+
except requests.exceptions.HTTPError as http_err:
|
75 |
+
error_details = response.text if response else "No response content"
|
76 |
+
status_message = f"❌ Bubble API HTTP error: {http_err} - Response: {error_details}"
|
77 |
+
except requests.exceptions.Timeout:
|
78 |
+
status_message = "❌ Bubble API Request timed out."
|
79 |
+
except requests.exceptions.RequestException as req_err:
|
80 |
+
status_message = f"❌ Bubble API Request error: {req_err}"
|
81 |
+
except json.JSONDecodeError as json_err: # Error decoding the main Bubble response
|
82 |
+
error_details = response.text if response else "No response content"
|
83 |
+
status_message = f"❌ Bubble API main response JSON decode error: {json_err}. Response: {error_details}"
|
84 |
+
except Exception as e:
|
85 |
+
status_message = f"❌ An unexpected error occurred while fetching from Bubble: {str(e)}"
|
86 |
+
|
87 |
+
print(status_message) # Log the final status message
|
88 |
+
return parsed_token_dict, status_message
|