Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_device(self, customer_id, device_id, platform, **data):
'''Add a device to a customer profile'''
if not customer_id:
raise CustomerIOException("customer_id cannot be blank in add_device")
if not device_id:
raise CustomerIOException("device_id cannot be blank in add_device")
if not platform:
raise CustomerIOException("platform cannot be blank in add_device")
data.update({
'id': device_id,
'platform': platform,
})
payload = {'device': data }
url = self.get_device_query_string(customer_id)
self.send_request('PUT', url, payload)
def add_to_segment(self, segment_id, customer_ids):
'''Add customers to a manual segment, customer_ids should be a list of strings'''
if not segment_id:
raise CustomerIOException("segment_id cannot be blank in add_to_segment")
if not customer_ids:
raise CustomerIOException("customer_ids cannot be blank in add_to_segment")
if not isinstance(customer_ids, list):
raise CustomerIOException("customer_ids must be a list in add_to_segment")
url = '{base}/segments/{id}/add_customers'.format(base=self.base_url, id=segment_id)
payload = {'ids': self._stringify_list(customer_ids)}
self.send_request('POST', url, payload)
def add_device(self, customer_id, device_id, platform, **data):
'''Add a device to a customer profile'''
if not customer_id:
raise CustomerIOException("customer_id cannot be blank in add_device")
if not device_id:
raise CustomerIOException("device_id cannot be blank in add_device")
if not platform:
raise CustomerIOException("platform cannot be blank in add_device")
data.update({
'id': device_id,
'platform': platform,
})
payload = {'device': data }
url = self.get_device_query_string(customer_id)
self.send_request('PUT', url, payload)
def _stringify_list(self, customer_ids):
customer_string_ids = []
for v in customer_ids:
if isinstance(v, str):
customer_string_ids.append(v)
elif isinstance(v, int):
customer_string_ids.append(str(v))
else:
raise CustomerIOException('customer_ids cannot be {type}'.format(type=type(v)))
return customer_string_ids
def add_device(self, customer_id, device_id, platform, **data):
'''Add a device to a customer profile'''
if not customer_id:
raise CustomerIOException("customer_id cannot be blank in add_device")
if not device_id:
raise CustomerIOException("device_id cannot be blank in add_device")
if not platform:
raise CustomerIOException("platform cannot be blank in add_device")
data.update({
'id': device_id,
'platform': platform,
})
payload = {'device': data }
url = self.get_device_query_string(customer_id)
self.send_request('PUT', url, payload)
def send_request(self, method, url, data):
'''Dispatches the request and returns a response'''
try:
response = self.http.request(method, url=url, json=self._sanitize(data), timeout=self.timeout)
except Exception as e:
# Raise exception alerting user that the system might be
# experiencing an outage and refer them to system status page.
message = '''Failed to receive valid reponse after {count} retries.
Check system status at http://status.customer.io.
Last caught exception -- {klass}: {message}
'''.format(klass=type(e), message=e, count=self.retries)
raise CustomerIOException(message)
result_status = response.status_code
if result_status != 200:
raise CustomerIOException('%s: %s %s' % (result_status, url, data))
return response.text
def add_to_segment(self, segment_id, customer_ids):
'''Add customers to a manual segment, customer_ids should be a list of strings'''
if not segment_id:
raise CustomerIOException("segment_id cannot be blank in add_to_segment")
if not customer_ids:
raise CustomerIOException("customer_ids cannot be blank in add_to_segment")
if not isinstance(customer_ids, list):
raise CustomerIOException("customer_ids must be a list in add_to_segment")
url = '{base}/segments/{id}/add_customers'.format(base=self.base_url, id=segment_id)
payload = {'ids': self._stringify_list(customer_ids)}
self.send_request('POST', url, payload)
def backfill(self, customer_id, name, timestamp, **data):
'''Backfill an event (track with timestamp) for a given customer_id'''
url = self.get_event_query_string(customer_id)
if isinstance(timestamp, datetime):
timestamp = self._datetime_to_timestamp(timestamp)
elif not isinstance(timestamp, int):
try:
timestamp = int(timestamp)
except Exception as e:
raise CustomerIOException("{t} is not a valid timestamp ({err})".format(t=timestamp, err=e))
post_data = {
'name': name,
'data': data,
'timestamp': timestamp
}
self.send_request('POST', url, post_data)
'''Dispatches the request and returns a response'''
try:
response = self.http.request(method, url=url, json=self._sanitize(data), timeout=self.timeout)
except Exception as e:
# Raise exception alerting user that the system might be
# experiencing an outage and refer them to system status page.
message = '''Failed to receive valid reponse after {count} retries.
Check system status at http://status.customer.io.
Last caught exception -- {klass}: {message}
'''.format(klass=type(e), message=e, count=self.retries)
raise CustomerIOException(message)
result_status = response.status_code
if result_status != 200:
raise CustomerIOException('%s: %s %s' % (result_status, url, data))
return response.text