Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/hubspot_api/mitol/hubspot_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
SimplePublicObject,
SimplePublicObjectInput,
)
from hubspot.crm.properties import ApiException as PropertiesApiException
from hubspot.crm.properties.exceptions import ApiException as PropertiesApiException
from urllib3 import Retry

from mitol.common.utils.collections import replace_null_values
Expand Down Expand Up @@ -317,7 +317,11 @@ def upsert_object_request(


def associate_objects_request(
from_type: str, from_id: str, to_type: str, to_id: str, assoc_type: str
from_type: str,
from_id: str,
to_type: str,
to_id: str,
assoc_type: str, # noqa: ARG001
) -> SimplePublicObject:
"""
Make an association between two objects
Expand All @@ -332,8 +336,11 @@ def associate_objects_request(
Returns:
SimplePublicObject: The Hubspot association object returned from the API
"""
return HubspotApi().crm.objects.associations_api.create(
from_type, from_id, to_type, to_id, assoc_type
return HubspotApi().crm.associations.v4.basic_api.create_default(
from_object_type=from_type,
from_object_id=from_id,
to_object_type=to_type,
to_object_id=to_id,
)


Expand Down Expand Up @@ -660,13 +667,15 @@ def get_line_items_for_deal(hubspot_id: str) -> list[SimplePublicObject]:

"""
client = HubspotApi()
line_items = []
associations = client.crm.deals.associations_api.get_all(
hubspot_id, HubspotObjectType.LINES.value
associations = client.crm.associations.v4.basic_api.get_page(
object_type="deals",
object_id=hubspot_id,
to_object_type=HubspotObjectType.LINES.value,
).results
for association in associations:
line_items.append(client.crm.line_items.basic_api.get_by_id(association.id)) # noqa: PERF401
return line_items
return [
client.crm.line_items.basic_api.get_by_id(association.to_object_id)
for association in associations
]


def find_line_item(
Expand Down
2 changes: 1 addition & 1 deletion src/hubspot_api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies = [
"django>=3.0",
"djangorestframework>=3.0.0",
"factory-boy~=3.2",
"hubspot-api-client==6.1.0",
"hubspot-api-client==12.0.0",
"mitol-django-common",
"requests>=2.20.0",
"urllib3>=1.26.5",
Expand Down
31 changes: 18 additions & 13 deletions tests/hubspot_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from faker import Faker
from hubspot.crm.objects import (
ApiException,
AssociatedId,
SimplePublicObject,
SimplePublicObjectInput,
)
Expand Down Expand Up @@ -431,10 +430,17 @@ def test_associate_objects_request(mock_hubspot_api):
to_type = api.HubspotObjectType.CONTACTS.value
to_id = 456
assoc_type = api.HubspotAssociationType.DEAL_CONTACT.value
mock_create = mock_hubspot_api.return_value.crm.objects.associations_api.create
mock_create = (
mock_hubspot_api.return_value.crm.associations.v4.basic_api.create_default
)

api.associate_objects_request(from_type, from_id, to_type, to_id, assoc_type)
mock_create.assert_called_once_with(from_type, from_id, to_type, to_id, assoc_type)
mock_create.assert_called_once_with(
from_object_type=from_type,
from_object_id=from_id,
to_object_type=to_type,
to_object_id=to_id,
)


def test_make_object_properties_message(mocker):
Expand Down Expand Up @@ -667,22 +673,21 @@ def test_find_line_item(mocker, product_id, quantity, raise_error):
def test_get_line_items_for_deal(mocker, mock_hubspot_api):
"""get_line_items_for_deal should make expected api calls and return expected results""" # noqa: E501
mock_lines = SimplePublicObjectFactory.create_batch(2)
mock_hubspot_api.return_value.crm.deals.associations_api.get_all.return_value = (
mocker.Mock(
results=[
AssociatedId(id=line.id, type="deal_to_line_item")
for line in mock_lines
]
)
)
mock_associations = [
mocker.Mock(to_object_id=line.id, association_types=[]) for line in mock_lines
]
mock_get_page = mock_hubspot_api.return_value.crm.associations.v4.basic_api.get_page
mock_get_page.return_value = mocker.Mock(results=mock_associations)
mock_hubspot_api.return_value.crm.line_items.basic_api.get_by_id.side_effect = (
mock_lines[0],
mock_lines[1],
)
deal_id = "111123"
results = api.get_line_items_for_deal(deal_id)
mock_hubspot_api.return_value.crm.deals.associations_api.get_all.assert_called_once_with(
deal_id, api.HubspotObjectType.LINES.value
mock_get_page.assert_called_once_with(
object_type="deals",
object_id=deal_id,
to_object_type=api.HubspotObjectType.LINES.value,
)
assert (
mock_hubspot_api.return_value.crm.line_items.basic_api.get_by_id.call_count == 2 # noqa: PLR2004
Expand Down
9 changes: 5 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.