Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
stcheng authored Jan 23, 2024
2 parents 3e6dfa1 + 21557b2 commit 86561a6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
37 changes: 20 additions & 17 deletions facebook_business/adobjects/serverside/app_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AppData(object):
param_types = {
'application_tracking_enabled': 'bool',
'advertiser_tracking_enabled': 'bool',
'app_user_id': 'str',
'campaign_ids': 'str',
'consider_views': 'bool',
'extinfo': 'ExtendedDeviceInfo',
Expand All @@ -40,7 +39,6 @@ def __init__(
self,
application_tracking_enabled=None,
advertiser_tracking_enabled=None,
app_user_id=None,
campaign_ids=None,
consider_views=None,
extinfo=None,
Expand All @@ -57,7 +55,6 @@ def __init__(
"""AppData which contains app data and device information for events happening from an app"""
self._application_tracking_enabled = None
self._advertiser_tracking_enabled = None
self._app_user_id = None
self._campaign_ids = None
self._consider_views = None
self._extinfo = None
Expand All @@ -71,8 +68,6 @@ def __init__(

if application_tracking_enabled is not None:
self.application_tracking_enabled = application_tracking_enabled
if app_user_id is not None:
self.app_user_id = app_user_id
if campaign_ids is not None:
self.campaign_ids = campaign_ids
if consider_views is not None:
Expand Down Expand Up @@ -110,14 +105,6 @@ def advertiser_tracking_enabled(self):
def advertiser_tracking_enabled(self, advertiser_tracking_enabled):
self._advertiser_tracking_enabled = advertiser_tracking_enabled

@property
def app_user_id(self):
return self._app_user_id

@app_user_id.setter
def app_user_id(self, app_user_id):
self._app_user_id = app_user_id

@property
def campaign_ids(self):
return self._campaign_ids
Expand Down Expand Up @@ -199,9 +186,25 @@ def windows_attribution_id(self, windows_attribution_id):
self._windows_attribution_id = windows_attribution_id

def normalize(self):
extended_device_info_array = []
normalized_payload = {
'application_tracking_enabled': self.application_tracking_enabled,
'advertiser_tracking_enabled': self.advertiser_tracking_enabled,
'campaign_ids': self.campaign_ids,
'consider_views': self.consider_views,
'include_dwell_data': self.include_dwell_data,
'include_video_data': self.include_video_data,
'install_referrer': self.install_referrer,
'installer_package': self.installer_package,
'receipt_data': self.receipt_data,
'url_schemes': self.url_schemes,
'windows_attribution_id': self.windows_attribution_id
}
if self.extinfo is not None:
normalized_payload['extinfo'] = self.extinfo.normalize()

normalized_payload = {k: v for k, v in normalized_payload.items() if v is not None}
return normalized_payload

return extended_device_info_array

def to_dict(self):
"""Returns the model properties as a dict"""
Expand All @@ -224,7 +227,7 @@ def to_dict(self):
))
else:
result[attr] = value
if issubclass(Event, dict):
if issubclass(AppData, dict):
for key, value in self.items():
result[key] = value

Expand All @@ -240,7 +243,7 @@ def __repr__(self):

def __eq__(self, other):
"""Returns true if both objects are equal"""
if not isinstance(other, Event):
if not isinstance(other, AppData):
return False

return self.__dict__ == other.__dict__
Expand Down
33 changes: 31 additions & 2 deletions facebook_business/adobjects/serverside/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from facebook_business.adobjects.serverside.action_source import ActionSource
from facebook_business.adobjects.serverside.custom_data import CustomData
from facebook_business.adobjects.serverside.user_data import UserData
from facebook_business.adobjects.serverside.app_data import AppData


class Event(object):
Expand All @@ -35,6 +36,7 @@ class Event(object):
'event_id': 'str',
'user_data': 'UserData',
'custom_data': 'CustomData',
'app_data': 'AppData',
'data_processing_options': 'list[str]',
'data_processing_options_country': 'int',
'data_processing_options_state': 'int',
Expand All @@ -44,9 +46,9 @@ class Event(object):

def __init__(self, event_name = None, event_time = None, event_source_url = None,
opt_out = None, event_id = None, user_data = None, custom_data = None,
data_processing_options = None, data_processing_options_country = None,
app_data = None, data_processing_options = None, data_processing_options_country = None,
data_processing_options_state = None, action_source = None, advanced_measurement_table = None):
# type: (str, int, str, bool, str, UserData, CustomData, list[str], int, int, ActionSource, str) -> None
# type: (str, int, str, bool, str, UserData, CustomData, AppData, list[str], int, int, ActionSource, str) -> None

"""Conversions API Event"""
self._event_name = None
Expand All @@ -56,6 +58,7 @@ def __init__(self, event_name = None, event_time = None, event_source_url = None
self._event_id = None
self._user_data = None
self._custom_data = None
self._app_data = None
self._data_processing_options = None
self._data_processing_options_country = None
self._data_processing_options_state = None
Expand All @@ -73,6 +76,8 @@ def __init__(self, event_name = None, event_time = None, event_source_url = None
self.user_data = user_data
if custom_data is not None:
self.custom_data = custom_data
if app_data is not None:
self.app_data = app_data
if data_processing_options is not None:
self.data_processing_options = data_processing_options
if data_processing_options_country is not None:
Expand Down Expand Up @@ -265,6 +270,27 @@ def custom_data(self, custom_data):

self._custom_data = custom_data

@property
def app_data(self):
"""Gets the app_data of this Event.
:return: The app_data of this Event.
:rtype: AppData
"""
return self._app_data

@app_data.setter
def app_data(self, app_data):
"""Sets the app_data of this Event.
:param app_data: The app_data of this Event.
:type: AppData
"""
if not isinstance(app_data, AppData):
raise TypeError('Event.app_data must be of type AppData')

self._app_data = app_data

@property
def data_processing_options(self):
"""Gets the data_processing_options of this Event.
Expand Down Expand Up @@ -390,6 +416,9 @@ def normalize(self):
if self.custom_data is not None:
normalized_payload['custom_data'] = self.custom_data.normalize()

if self.app_data is not None:
normalized_payload['app_data'] = self.app_data.normalize()

if self.action_source is not None:
self.validate_action_source(self.action_source)
normalized_payload['action_source'] = self.action_source.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def to_dict(self):
))
else:
result[attr] = value
if issubclass(Event, dict):
if issubclass(ExtendedDeviceInfo, dict):
for key, value in self.items():
result[key] = value

Expand Down
9 changes: 4 additions & 5 deletions facebook_business/adobjects/serverside/tests/event_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ def test_constructor(self):
app_data = AppData(
application_tracking_enabled = True,
advertiser_tracking_enabled = True,
app_user_id = '999999',
campaign_ids = ['100001', '100002'],
campaign_ids = "100001,100002",
consider_views = True,
device_token = 'xyz',
extinfo = ext_device_info.normalize(),
extinfo = ext_device_info,
include_dwell_data = True,
include_video_data = True,
install_referrer = 'xyz',
Expand All @@ -88,6 +86,7 @@ def test_constructor(self):
data_processing_options_country=data_processing_options_country,
data_processing_options_state=data_processing_options_state,
action_source=action_source,
app_data=app_data,
)
expected_params = {
'event_name': event_name,
Expand All @@ -101,8 +100,8 @@ def test_constructor(self):
'data_processing_options_country': data_processing_options_country,
'data_processing_options_state': data_processing_options_state,
'action_source': action_source.value,
'app_data': app_data.normalize(),
}

self.assertEqual(event.normalize(), expected_params)

def test_action_source_validation(self):
Expand Down
4 changes: 3 additions & 1 deletion facebook_business/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,9 @@ def load_next_page(self):
if (
'paging' in response and
'cursors' in response['paging'] and
'after' in response['paging']['cursors']
'after' in response['paging']['cursors'] and
# 'after' will always exist even if no more pages are available
'next' in response['paging']
):
self.params['after'] = response['paging']['cursors']['after']
else:
Expand Down
8 changes: 6 additions & 2 deletions facebook_business/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ def is_type(self, value_type, value, allow_dict_as_obj=True):
if not isinstance(value, dict):
return False
sub_types = self.get_type_from_collection(value_type, 'map')
sub_type_key = sub_types[0]
sub_type_value = sub_types[1]
if len(sub_types) == 2:
sub_type_key = sub_types[0]
sub_type_value = sub_types[1]
else:
sub_type_key = 'string'
sub_type_value = sub_types[0]
return all(self.is_type(sub_type_key, k) and
self.is_type(sub_type_value, v) for k, v in value.items())

Expand Down

0 comments on commit 86561a6

Please sign in to comment.