Update connectors/hubspot_connector.py
Browse files- connectors/hubspot_connector.py +18 -14
connectors/hubspot_connector.py
CHANGED
|
@@ -1,25 +1,29 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from hubspot import HubSpot
|
| 6 |
from hubspot.crm.contacts import ApiException as HSContactsError
|
| 7 |
|
| 8 |
-
#
|
| 9 |
HUBSPOT_TOKEN = os.getenv("HUBSPOT_TOKEN")
|
| 10 |
-
hs_client = HubSpot(access_token=HUBSPOT_TOKEN)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
items = [r.to_dict() for r in page.results]
|
| 21 |
-
return json.dumps(items)
|
| 22 |
-
except HSContactsError as he:
|
| 23 |
-
return json.dumps({"error": he.body})
|
| 24 |
except Exception as e:
|
| 25 |
return json.dumps({"error": str(e)})
|
|
|
|
| 1 |
+
# connectors/hubspot_connector.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from hubspot import HubSpot
|
| 6 |
from hubspot.crm.contacts import ApiException as HSContactsError
|
| 7 |
|
| 8 |
+
# Get HubSpot token from env
|
| 9 |
HUBSPOT_TOKEN = os.getenv("HUBSPOT_TOKEN")
|
|
|
|
| 10 |
|
| 11 |
+
# Setup client
|
| 12 |
+
client = HubSpot(access_token=HUBSPOT_TOKEN)
|
| 13 |
+
|
| 14 |
+
def list_contacts(limit=100):
|
| 15 |
+
"""Return first N contacts as JSON."""
|
| 16 |
+
try:
|
| 17 |
+
response = client.crm.contacts.basic_api.get_page(limit=limit)
|
| 18 |
+
return json.dumps([r.to_dict() for r in response.results])
|
| 19 |
+
except HSContactsError as e:
|
| 20 |
+
return json.dumps({"error": e.body})
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return json.dumps({"error": str(e)})
|
| 23 |
+
|
| 24 |
+
def list_companies(limit=100):
|
| 25 |
try:
|
| 26 |
+
response = client.crm.companies.basic_api.get_page(limit=limit)
|
| 27 |
+
return json.dumps([r.to_dict() for r in response.results])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
return json.dumps({"error": str(e)})
|