Group bulk handler
Retrieving the bulk handler
- class GroupBulkhandlerMixin
- get_group_bulkhandler(feedback=False, create_groups=False, callback=None)
Perform bulk “update” and/or “insert” operations on groups. This functions returns a
BulkHandler
object that can be used to perform the bulk operations.- Parameters:
- Returns:
BulkHandler for groups
- Return type:
BulkHandler
Using the bulk handler
- class BulkHandler
- flush()
Closes the stream and writes the changes to BlueConic.
- open_stream()
Open a stream to BlueConic.
- write(writable_object)
Writes the changes in a bulkhandler object back to BlueConic.
- Parameters:
writable_object (ProfileOperation, Profile (deprecated), Group) – The object to be updated in BlueConic
- Raises:
ValueError – The temporary file is not open.
TypeError – The variable ‘writable_object’ is not of type
HashMap
.
The with statement simplifies the writing and ensures that clean-up code is executed. It is possible to write groups to BlueConic without the with statement. The following code block provides such an example.
>>> import blueconic
>>> bc = blueconic.Client()
>>> group_bulkhandler = bc.get_group_bulkhandler(feedback=True)
>>> group_bulkhandler.open_stream()
>>> example_group = bc.get_group(group_id='group_0', group_type_id='company')
>>> example_group.add_value('nr_of_employees', 42)
>>> group_bulkhandler.write(example_group)
>>> group_bulkhandler.flush()
[{'groupId': 'company_group_0', 'state': 'MODIFIED'}]
<Response [200]>
Importing data from an external source
The bulk handler can also be used to create or update groups without retrieving
them from BlueConic first. This allows to more efficiently import data from an
external data source. The following code would import group data into BlueConic,
update a group if the given group_id
exists, and otherwise create a new group.
>>> from blueconic import Client, Group
>>> bc = Client()
>>> group_type_id = "company"
>>> with bc.get_group_bulkhandler(create_groups=True) as handler:
... for group in group_data:
... group = Group(group['id'], group_type_id)
... group.set_value('nr_of_employees', 42)
... handler.write(group)
If create_groups=False
is passed, and an existing group cannot be found,
the data is ignored.