From 2fa4b894e93a8ef2a7178800b257e32226241d30 Mon Sep 17 00:00:00 2001 From: Peter Jenkins <5166653+peterjenkins@users.noreply.github.com> Date: Mon, 18 Mar 2019 16:05:52 -0400 Subject: [PATCH] Add support for sending mpid, das --- LICENSE | 2 +- docs/Batch.md | 2 ++ docs/UserIdentities.md | 3 ++ example_usage.py | 72 +++++++++++++++++++++++++++++++++++++++ mparticle/api_client.py | 2 +- mparticle/models/batch.py | 58 +++++++++++++++++++++++++++++-- setup.py | 2 +- test/test_batch.py | 28 +++++++++++++++ 8 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 example_usage.py diff --git a/LICENSE b/LICENSE index d317706..1bccc65 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2016 mParticle, Inc. +Copyright 2019 mParticle, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/docs/Batch.md b/docs/Batch.md index 22f6bb0..efec426 100644 --- a/docs/Batch.md +++ b/docs/Batch.md @@ -13,6 +13,8 @@ Name | Type | Description | Notes **user_attributes** | **object** | | [optional] **deleted_user_attributes** | **list[str]** | | [optional] **user_identities** | [**UserIdentities**](UserIdentities.md) | | [optional] +**mpid** | **int** | | [optional] +**mp_deviceid** | **str** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/docs/UserIdentities.md b/docs/UserIdentities.md index e0a4a24..2a5fa69 100644 --- a/docs/UserIdentities.md +++ b/docs/UserIdentities.md @@ -13,6 +13,9 @@ Name | Type | Description | Notes **email** | **str** | | [optional] **alias** | **str** | | [optional] **facebook_custom_audience_id** | **str** | | [optional] +**other2** | **str** | | [optional] +**other3** | **str** | | [optional] +**other4** | **str** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/example_usage.py b/example_usage.py new file mode 100644 index 0000000..66e3ead --- /dev/null +++ b/example_usage.py @@ -0,0 +1,72 @@ +import mparticle +batch = mparticle.Batch() +batch.environment = 'development' + +identities = mparticle.UserIdentities() +identities.customerid = '123456' +identities.email = 'user@example.com' +batch.user_identities = identities + +device_info = mparticle.DeviceInformation() +# set any IDs that you have for this user +device_info.ios_advertising_id = '07d2ebaa-e956-407e-a1e6-f05f871bf4e2' +device_info.android_advertising_id = 'a26f9736-c262-47ea-988b-0b0504cee874' +batch.device_info = device_info + +# arbitrary example allowing you to create a segment of users trial users +batch.user_attributes = {'Account type': 'trial', 'TrialEndDate':'2016-12-01'} + +app_event = mparticle.AppEvent('Example', 'navigation') +app_event.timestamp_unixtime_ms = 1552596256103 + +product = mparticle.Product() +product.name = 'Example Product' +product.id = 'sample-sku' +product.price = 19.99 + +product_action = mparticle.ProductAction('purchase') +product_action.products = [product] +product_action.tax_amount = 1.50 +product_action.total_amount = 21.49 + +commerce_event = mparticle.CommerceEvent(product_action) +commerce_event.timestamp_unixtime_ms = 1552596256103 + +session_start = mparticle.SessionStartEvent() +session_start.session_id = 12345678 +session_start.timestamp_unixtime_ms = 1552596256103 + +session_end = mparticle.SessionEndEvent() +session_end.session_id = session_start.session_id # its mandatory that these match +session_end.session_duration_ms = 10000 +session_end.timestamp_unixtime_ms = 1552596266103 + 10000 + +batch.events = [session_start, app_event, commerce_event, session_end] + +batch.mpid = '600868121729048600' +batch.mp_deviceid = '59780f39-d7a0-4ebe-9950-280f937c29e2' + +install = mparticle.ApplicationStateTransitionEvent.create_install_event() +install.timestamp_unixtime_ms = 1552596256103 + +upgrade = mparticle.ApplicationStateTransitionEvent.create_upgrade_event() +upgrade.timestamp_unixtime_ms = 1552596256103 + +foreground = mparticle.ApplicationStateTransitionEvent.create_foreground_event() +foreground.timestamp_unixtime_ms = 1552596256103 + +background = mparticle.ApplicationStateTransitionEvent.create_background_event() +background.timestamp_unixtime_ms = 1552596256103 + +configuration = mparticle.Configuration() +configuration.api_key = 'foo-key' +configuration.api_secret = 'foo-secret' +configuration.debug = True #enable logging of HTTP traffic +api_instance = mparticle.EventsApi(configuration) + +try: + api_instance.upload_events(batch) + # you can also send multiple batches at a time to decrease the amount of network calls + #api_instance.bulk_upload_events([batch, batch]) +except mparticle.rest.ApiException as e: + print "Exception while calling mParticle: %s\n" % e \ No newline at end of file diff --git a/mparticle/api_client.py b/mparticle/api_client.py index 8f919a3..9c2f83e 100644 --- a/mparticle/api_client.py +++ b/mparticle/api_client.py @@ -89,7 +89,7 @@ def __init__(self, host=None, header_name=None, header_value=None, cookie=None, self.host = host self.cookie = cookie # Set default User-Agent. - self.user_agent = 'mParticle Python client/0.10.5' + self.user_agent = 'mParticle Python client/0.10.6' @property def user_agent(self): diff --git a/mparticle/models/batch.py b/mparticle/models/batch.py index 7535e15..0a9cff5 100644 --- a/mparticle/models/batch.py +++ b/mparticle/models/batch.py @@ -60,7 +60,7 @@ class Batch(object): - def __init__(self, events=None, source_request_id=None, environment=None, ip=None, schema_version=None, device_info=None, application_info=None, user_attributes=None, deleted_user_attributes=None, user_identities=None, api_key=None): + def __init__(self, events=None, source_request_id=None, environment=None, ip=None, schema_version=None, device_info=None, application_info=None, user_attributes=None, deleted_user_attributes=None, user_identities=None, api_key=None, mpid=None, mp_deviceid=None): """ Batch - a model defined in Swagger @@ -80,7 +80,9 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non 'user_attributes': 'object', 'deleted_user_attributes': 'list[str]', 'user_identities': 'UserIdentities', - 'api_key':'str' + 'api_key':'str', + 'mpid':'int', + 'mp_deviceid':'str' } self.attribute_map = { @@ -94,7 +96,9 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non 'user_attributes': 'user_attributes', 'deleted_user_attributes': 'deleted_user_attributes', 'user_identities': 'user_identities', - 'api_key':'api_key' + 'api_key':'api_key', + 'mpid':'mpid', + 'mp_deviceid':'mp_deviceid' } self._api_key = api_key @@ -108,6 +112,8 @@ def __init__(self, events=None, source_request_id=None, environment=None, ip=Non self._user_attributes = user_attributes self._deleted_user_attributes = deleted_user_attributes self._user_identities = user_identities + self._mpid = mpid + self._mp_deviceid = mp_deviceid self._allowed_types = {type(AppEvent('empty')):'custom_event', type(FirstRunEvent()):'first_run', type(SessionStartEvent()):'session_start', @@ -400,6 +406,52 @@ def user_identities(self, user_identities): self._user_identities = user_identities + @property + def mpid(self): + """ + Gets the mpid of this Batch. + + + :return: The mpid of this Batch. + :rtype: str + """ + return self._mpid + + @mpid.setter + def mpid(self, mpid): + """ + Sets the mpid of this Batch. + + + :param mpid: The mpid of this Batch. + :type: str + """ + + self._mpid = mpid + + @property + def mp_deviceid(self): + """ + Gets the mp_deviceid of this Batch. + + + :return: The mp_deviceid of this Batch. + :rtype: str + """ + return self._mp_deviceid + + @mp_deviceid.setter + def mp_deviceid(self, mp_deviceid): + """ + Sets the mp_deviceid of this Batch. + + + :param mp_deviceid: The mp_deviceid of this Batch. + :type: str + """ + + self._mp_deviceid = mp_deviceid + def to_dict(self): """ Returns the model properties as a dict diff --git a/setup.py b/setup.py index bf167a4..1ba2fbf 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages NAME = "mparticle" -VERSION = "0.10.5" +VERSION = "0.10.6" diff --git a/test/test_batch.py b/test/test_batch.py index b727847..c89499e 100644 --- a/test/test_batch.py +++ b/test/test_batch.py @@ -48,6 +48,34 @@ def testBatch(self): """ model = mparticle.models.batch.Batch() + def testBatchMPID(self): + """ + Test Batch MPID + """ + model = mparticle.models.batch.Batch() + + identity_dict = model.to_dict() + for key in identity_dict: + self.assertEqual(None, identity_dict[key]) + + model.mpid = 600868121729048600 + identity_dict = model.to_dict() + self.assertEqual(600868121729048600, identity_dict["mpid"]) + + def testBatchDAS(self): + """ + Test Batch DAS + """ + model = mparticle.models.batch.Batch() + + identity_dict = model.to_dict() + for key in identity_dict: + self.assertEqual(None, identity_dict[key]) + + model.mp_deviceid = "59780f39-d7a0-4ebe-9950-280f937c29e2" + identity_dict = model.to_dict() + self.assertEqual("59780f39-d7a0-4ebe-9950-280f937c29e2", identity_dict["mp_deviceid"]) + if __name__ == '__main__': unittest.main()