Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more common functions to the hubspot_api app #106

Open
mbertrand opened this issue Jan 18, 2023 · 0 comments
Open

Add more common functions to the hubspot_api app #106

mbertrand opened this issue Jan 18, 2023 · 0 comments
Labels
Enhancement New feature or request

Comments

@mbertrand
Copy link
Member

mbertrand commented Jan 18, 2023

There are some functions and tasks in each of our hubspot-enabled applications that are almost identical but that reference the application's specific functions for handling its object models. Some of these could be made more generic (via adding the app_label and model name as input parameters) and centralized here instead, reducing code duplication.

For example, sync_contact_with_hubspot, sync_deal_with_hubspot, and sync_product_with_hubspot exist in xpro, mitxonline, and bootcamps, but could potentially be replaced by something like this in ol-django/hubspot_api:

def get_hubspot_function(func_name: str) -> Callable:
    """Given a string like my_app.api.do_something, return that function"""
    func_parts = func_name.split(".")
    func = apps.get_app_config(func_parts[0]).module
    for i in range(1, len(func_parts)):
        func = getattr(func, func_parts[i])
    return func


def sync_object_with_hubspot(
        object_id: int,
        msg_func: str,
        app_label: str,
        model_name: str,
        hubspot_type: str
) -> SimplePublicObject:
    """
    Sync a django object with a hubspot object

    Args:
        object_id(int): The django object id
        msg_func(str): The name of function that will generate the request message body (<app>.<module>.<function>)
        app_label(str): The app label for the object model
        model_name(str): The object model name
        hubspot_type(str): The hubspot object type (contact, deal, etc)

    Returns:
        SimplePublicObject: The hubspot object
    """
    body = get_hubspot_function(msg_func)(object_id)
    content_type = ContentType.objects.get_by_natural_key(app_label, model_name)

    return upsert_object_request(
        content_type, hubspot_type, object_id=object_id, body=body
    )
@mbertrand mbertrand added the Enhancement New feature or request label Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant