Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pylint and minor fixes #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions customerio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""A CustomerIO API wrapper for Python."""
from __future__ import division
from datetime import datetime
import math
import time
import warnings

from requests import Session
from requests.status_codes import codes
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

Expand All @@ -20,8 +22,8 @@ class CustomerIOException(Exception):


class CustomerIO(object):

def __init__(self, site_id=None, api_key=None, host=None, port=None, url_prefix=None, json_encoder=None, retries=3, timeout=10, backoff_factor=0.02):
def __init__(self, site_id=None, api_key=None, host=None, port=None, url_prefix=None, json_encoder=None,
retries=3, timeout=10, backoff_factor=0.02):
self.site_id = site_id
self.api_key = api_key
self.host = host or 'track.customer.io'
Expand Down Expand Up @@ -59,43 +61,43 @@ def setup_connection(self):
self.http.auth = (self.site_id, self.api_key)

def get_customer_query_string(self, customer_id):
'''Generates a customer API path'''
"""Generates a customer API path"""
return '{base}/customers/{id}'.format(base=self.base_url, id=customer_id)

def get_event_query_string(self, customer_id):
'''Generates an event API path'''
"""Generates an event API path"""
return '{base}/customers/{id}/events'.format(base=self.base_url, id=customer_id)

def get_device_query_string(self, customer_id):
'''Generates a device API path'''
"""Generates a device API path"""
return '{base}/customers/{id}/devices'.format(base=self.base_url, id=customer_id)

def send_request(self, method, url, data):
'''Dispatches the request and returns a response'''
"""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)
message = """Failed to receive valid response 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:
if result_status != codes.ok:
raise CustomerIOException('%s: %s %s' % (result_status, url, data))
return response.text

def identify(self, id, **kwargs):
'''Identify a single customer by their unique id, and optionally add attributes'''
"""Identify a single customer by their unique id, and optionally add attributes"""
url = self.get_customer_query_string(id)
self.send_request('PUT', url, kwargs)

def track(self, customer_id, name, **data):
'''Track an event for a given customer_id'''
"""Track an event for a given customer_id"""
url = self.get_event_query_string(customer_id)
post_data = {
'name': name,
Expand All @@ -104,7 +106,7 @@ def track(self, customer_id, name, **data):
self.send_request('POST', url, post_data)

def pageview(self, customer_id, page, **data):
'''Track a pageview for a given customer_id'''
"""Track a pageview for a given customer_id"""
url = self.get_event_query_string(customer_id)
post_data = {
'type': "page",
Expand All @@ -114,7 +116,7 @@ def pageview(self, customer_id, page, **data):
self.send_request('POST', url, post_data)

def backfill(self, customer_id, name, timestamp, **data):
'''Backfill an event (track with timestamp) for a given customer_id'''
"""Backfill an event (track with timestamp) for a given customer_id"""
url = self.get_event_query_string(customer_id)

if isinstance(timestamp, datetime):
Expand All @@ -134,12 +136,12 @@ def backfill(self, customer_id, name, timestamp, **data):
self.send_request('POST', url, post_data)

def delete(self, customer_id):
'''Delete a customer profile'''
"""Delete a customer profile"""
url = self.get_customer_query_string(customer_id)
self.send_request('DELETE', url, {})

def add_device(self, customer_id, device_id, platform, **data):
'''Add a device to a customer profile'''
"""Add a device to a customer profile"""
if not customer_id:
raise CustomerIOException("customer_id cannot be blank in add_device")

Expand All @@ -158,7 +160,7 @@ def add_device(self, customer_id, device_id, platform, **data):
self.send_request('PUT', url, payload)

def delete_device(self, customer_id, device_id):
'''Delete a device from a customer profile'''
"""Delete a device from a customer profile"""
url = self.get_device_query_string(customer_id)
delete_url = '{base}/{token}'.format(base=url, token=device_id)
self.send_request('DELETE', delete_url, {})
Expand All @@ -176,7 +178,7 @@ def unsuppress(self, customer_id):
self.send_request('POST', '{base}/customers/{id}/unsuppress'.format(base=self.base_url, id=customer_id), {})

def add_to_segment(self, segment_id, customer_ids):
'''Add customers to a manual segment, customer_ids should be a list of strings'''
"""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")

Expand All @@ -191,7 +193,7 @@ def add_to_segment(self, segment_id, customer_ids):
self.send_request('POST', url, payload)

def remove_from_segment(self, segment_id, customer_ids):
'''Remove customers from a manual segment, customer_ids should be a list of strings'''
"""Remove customers from a manual segment, customer_ids should be a list of strings"""
if not segment_id:
raise CustomerIOException("segment_id cannot be blank in remove_from_segment")

Expand Down Expand Up @@ -228,4 +230,4 @@ def _stringify_list(self, customer_ids):
customer_string_ids.append(str(v))
else:
raise CustomerIOException('customer_ids cannot be {type}'.format(type=type(v)))
return customer_string_ids
return customer_string_ids