Skip to content

Commit e694089

Browse files
Merge pull request #334 from willicab/master
Create methods for leads
2 parents 2ea25af + 407da5b commit e694089

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

navigator/actions/hubspot.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
from hubspot import HubSpot as HubSpot
1+
from hubspot import HubSpot
2+
from hubspot.crm.properties.exceptions import ApiException as PropertiesApiException, UnauthorizedException as PropertiesUnauthorizedException
3+
# Contacts
24
from hubspot.crm.contacts import SimplePublicObjectInput as ContactSimplePublicObjectInput
5+
from hubspot.crm.contacts.exceptions import ApiException as ContactsApiException, UnauthorizedException as ContactsUnauthorizedException
6+
# Companies
37
from hubspot.crm.companies import SimplePublicObjectInput as CompanySimplePublicObjectInput
8+
from hubspot.crm.companies.exceptions import ApiException as CompaniesApiException, UnauthorizedException as CompaniesUnauthorizedException
9+
# Leads
410
from hubspot.crm.objects.leads import SimplePublicObjectInputForCreate as LeadSimplePublicObjectInputForCreate
511
from hubspot.crm.objects.leads.exceptions import ApiException as LeadsApiException, UnauthorizedException as LeadsUnauthorizedException
6-
from hubspot.crm.contacts.exceptions import ApiException as ContactsApiException, UnauthorizedException as ContactsUnauthorizedException
7-
from hubspot.crm.companies.exceptions import ApiException as CompaniesApiException, UnauthorizedException as CompaniesUnauthorizedException
8-
from hubspot.crm.properties.exceptions import ApiException as PropertiesApiException, UnauthorizedException as PropertiesUnauthorizedException
12+
# Associations
13+
from hubspot.crm.associations.models.public_object_id import PublicObjectId
14+
from hubspot.crm.associations.models.public_association import PublicAssociation
15+
from hubspot.crm.associations.models.batch_input_public_association import BatchInputPublicAssociation
16+
from hubspot.crm.associations.exceptions import ApiException
917
from .abstract import AbstractAction
1018
from navconfig import config
1119
from ..exceptions import ConfigError, FailedAuth
@@ -115,7 +123,32 @@ async def get_contact_properties(self):
115123
raise FailedAuth(f"Hubspot: Unauthorized to list contact properties: {e.body}") from e
116124
except PropertiesApiException as e:
117125
raise ConfigError(f"Hubspot: Error listing contact properties: {e.body}") from e
118-
126+
127+
async def update_contact_lifecyclestage(self, contact_id, lifecyclestage="lead"):
128+
"""
129+
Update the lifecyclestage of a contact.
130+
131+
:param contact_id: The ID of the contact to update.
132+
:param lifecyclestage: The new lifecyclestage value (default is "lead").
133+
:return: The updated contact object.
134+
"""
135+
try:
136+
client = HubSpot(access_token=self.token)
137+
# Definir las propiedades a actualizar
138+
properties = {"lifecyclestage": lifecyclestage}
139+
contact_update_input = ContactSimplePublicObjectInput(properties=properties)
140+
141+
# Actualizar el contacto
142+
response = client.crm.contacts.basic_api.update(
143+
contact_id=contact_id,
144+
simple_public_object_input=contact_update_input
145+
)
146+
return response.to_dict()
147+
except ContactsUnauthorizedException as e:
148+
raise FailedAuth(f"Hubspot: Unauthorized to update contact lifecyclestage: {e.body}") from e
149+
except ContactsApiException as e:
150+
raise ConfigError(f"Hubspot: Error updating contact lifecyclestage: {e.body}") from e
151+
119152
async def get_contact_lifecyclestage(self):
120153
"""
121154
List all contact lifecyclestages available in HubSpot.
@@ -135,6 +168,39 @@ async def get_contact_lifecyclestage(self):
135168
except PropertiesApiException as e:
136169
raise ConfigError(f"Hubspot: Error listing contact lifecyclestage: {e.body}") from e
137170

171+
async def create_association(self, contact_id, company_id):
172+
"""
173+
Create an association between a contact and a company.
174+
175+
:param contact_id: The ID of the contact.
176+
:param company_id: The ID of the company.
177+
:param association_type: The type of association (e.g., 1).
178+
:return: Response from the API.
179+
"""
180+
try:
181+
client = HubSpot(access_token=self.token)
182+
# Create object of origin and destination
183+
from_object = PublicObjectId(id=contact_id)
184+
to_object = PublicObjectId(id=company_id)
185+
186+
# Create the association obbject
187+
association = PublicAssociation(
188+
_from=from_object,
189+
to=to_object,
190+
type=1
191+
)
192+
batch_input = BatchInputPublicAssociation(inputs=[association])
193+
194+
# Create the association
195+
response = client.crm.associations.batch_api.create(
196+
from_object_type="contacts",
197+
to_object_type="companies",
198+
batch_input_public_association=batch_input
199+
)
200+
return response.to_dict()
201+
except ApiException as e:
202+
raise ConfigError(f"Hubspot: Exception when creating association: {e}")
203+
138204
async def get_companies(self):
139205
"""
140206
Retrieve details of all companies.

0 commit comments

Comments
 (0)