Skip to content

Commit

Permalink
fix: minor improvements (#136)
Browse files Browse the repository at this point in the history
1. Replace deprecated call
2. Reminder to replace `Exception` with a more specific error type
  • Loading branch information
daabr authored and pashafateev committed Dec 30, 2024
1 parent 063956d commit 9e8d811
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions auth0_to_hubspot/program.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""This program adds new Auth0 users to HubSpot as contacts."""

import datetime
from datetime import UTC, datetime
import os

from autokitteh.auth0 import auth0_client
from autokitteh.hubspot import hubspot_client
from hubspot.crm.contacts import SimplePublicObjectInput


LOOKUP_HOURS = int(os.getenv("HOURS"))

auth0 = auth0_client("auth0_conn")
Expand All @@ -20,16 +21,14 @@ def check_for_new_users(event):
"""
start, end = _get_time_range(LOOKUP_HOURS)
query = f"created_at:[{start} TO {end}]"

response = auth0.users.list(q=query, search_engine="v3")
add_new_users(response["users"])


def _get_time_range(hours):
"""Calculate start and end times for user lookup."""
now = datetime.datetime.utcnow()
now = datetime.now(UTC)
start_time = now - datetime.timedelta(hours=hours)

return (start_time.isoformat() + "Z", now.isoformat() + "Z")


Expand All @@ -39,8 +38,11 @@ def add_new_users(users):
contact = _create_hubspot_contact(user)
try:
hubspot.crm.contacts.basic_api.create(contact)
print(f"Added {user['email']} to HubSpot")
print(f"Added to HubSpot: {user['email']}")
except Exception as e:
# TODO: Replace "Exception" with a specific error for
# conflicts, i.e. don't ignore other types of errors.
# print(f"Contact already exists in HubSpot: {user['email']}")
print(f"Failed to add {user['email']} to HubSpot: {e}")
continue

Expand Down

0 comments on commit 9e8d811

Please sign in to comment.