def create_contact(contact, connection): property_type_by_property_name = \ get_property_type_by_property_name(connection) contact_data = format_contact_data_for_saving( contact, property_type_by_property_name, ) response = connection.send_post_request( _CONTACT_CREATING_URL_PATH, contact_data, ) return response["vid"]
def update_contact(contact, connection): property_type_by_property_name = \ get_property_type_by_property_name(connection) properties_data = format_contact_properties_for_saving( contact.properties, property_type_by_property_name, ) path = _CONTACT_UPDATING_URL_TEMPLATE.format(contact_id=contact.vid) connection.send_post_request( path, properties_data, )
def _get_contacts_from_all_pages(path_info, connection, property_names): property_type_by_property_name = \ get_property_type_by_property_name(connection) contacts_data = _get_contacts_data( connection, path_info, ['vid-offset'], property_names, ) contacts = \ _build_contacts_from_data(contacts_data, property_type_by_property_name) return contacts
def save_contacts(contacts, connection): """ Request the creation and/or update of the ``contacts``. :param iterable contacts: The contacts to be created/updated :return: ``None`` :raises hubspot.connection.exc.HubspotException: :raises hubspot.contacts.exc.HubspotPropertyValueError: If one of the property values on a contact is invalid. For each contact, only its email address and properties are passed to HubSpot. Any other datum (e.g., the VID) is ignored. As at this writing, this end-point does not process the requested changes immediately. Instead, it **partially** validates the input and, if it's all correct, the requested changes are queued. End-point documentation: http://developers.hubspot.com/docs/methods/contacts/batch_create_or_update """ contacts_batches = ipaginate(contacts, BATCH_SAVING_SIZE_LIMIT) contacts_first_batch = next(contacts_batches, None) if not contacts_first_batch: return property_type_by_property_name = \ get_property_type_by_property_name(connection) for contacts_batch in chain([contacts_first_batch], contacts_batches): contacts_batch_data = format_contacts_data_for_saving( contacts_batch, property_type_by_property_name, ) connection.send_post_request( _CONTACTS_SAVING_URL_PATH, contacts_batch_data, )
def _get_contacts_from_all_pages_by_recency( contact_list_id, connection, property_names=(), cutoff_datetime=None, ): contacts_data = _get_contacts_data( connection, '/lists/{}/contacts/recent'.format(contact_list_id), ('vid-offset', 'time-offset'), property_names, ) if cutoff_datetime: cutoff_timestamp = \ convert_date_to_timestamp_in_milliseconds(cutoff_datetime) else: cutoff_timestamp = None property_type_by_property_name = \ get_property_type_by_property_name(connection) seen_contact_vids = set() for contact_data in contacts_data: contact = _build_contact_from_data( contact_data, property_type_by_property_name, ) if contact.vid in seen_contact_vids: continue seen_contact_vids.add(contact.vid) if cutoff_timestamp and contact_data['addedAt'] < cutoff_timestamp: raise StopIteration() yield contact