Skip to content

Commit

Permalink
Merge pull request #13 from kissmetrics/naming
Browse files Browse the repository at this point in the history
Use proper URI naming conventions and follow PEP8
  • Loading branch information
njm committed Nov 13, 2013
2 parents eeed6e6 + 9afcee2 commit 61ea233
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 95 deletions.
8 changes: 4 additions & 4 deletions KISSmetrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-

TRACKING_HOSTNAME = 'trk.kissmetrics.com'
TRACKING_PROTOCOL = 'http'
TRACKING_SCHEME = 'http'

RECORD_URI = 'e'
SET_URI = 's'
ALIAS_URI = 'a'
RECORD_PATH = 'e'
SET_PATH = 's'
ALIAS_PATH = 'a'

__author__ = 'Ernest W. Durbin III <ewdurbin@gmail.com>'
__license__ = 'MIT'
Expand Down
31 changes: 18 additions & 13 deletions KISSmetrics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@
class Client:

def __init__(self, key, trk_host=KISSmetrics.TRACKING_HOSTNAME,
trk_proto=KISSmetrics.TRACKING_PROTOCOL):
trk_scheme=KISSmetrics.TRACKING_SCHEME):
self.key = key
if trk_proto not in ['http', 'https']:
raise ValueError('trk_proto must be one of (http, https)')
if trk_scheme not in ['http', 'https']:
raise ValueError('trk_scheme must be one of (http, https)')
self.http = PoolManager()
self.trk_host = trk_host
self.trk_proto = trk_proto
self.trk_scheme = trk_scheme

def request(self, query, method="GET"):
url = '%s://%s/%s' % (self.trk_proto, self.trk_host, query)
return self.http.request(method, url)
def request(self, uri, method="GET"):
return self.http.request(method, uri)

def record(self, person, event, properties=None, timestamp=None,
uri=KISSmetrics.RECORD_URI):
path=KISSmetrics.RECORD_PATH):
this_request = request.record(self.key, person, event,
timestamp=timestamp,
properties=properties, uri=uri)
properties=properties,
scheme=self.trk_scheme,
host=self.trk_host, path=path)
return self.request(this_request)

def set(self, person, properties=None, timestamp=None,
uri=KISSmetrics.SET_URI):
path=KISSmetrics.SET_PATH):
this_request = request.set(self.key, person, timestamp=timestamp,
properties=properties, uri=uri)
properties=properties,
scheme=self.trk_scheme, host=self.trk_host,
path=path)
return self.request(this_request)

def alias(self, person, identity, uri=KISSmetrics.ALIAS_URI):
this_request = request.alias(self.key, person, identity, uri=uri)
def alias(self, person, identity, path=KISSmetrics.ALIAS_PATH):
this_request = request.alias(self.key, person, identity,
scheme=self.trk_scheme,
host=self.trk_host, path=path)
return self.request(this_request)
18 changes: 9 additions & 9 deletions KISSmetrics/client_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@ def __init__(self, key, host=None, http_timeout=None, logging=True):
if host:
(trk_host, trk_port) = host.split(':')
try:
trk_proto = port_map[trk_port]
trk_scheme = port_map[trk_port]
except KeyError:
raise ValueError('port in supplied host is not 80 or 443')
else:
trk_host = KISSmetrics.TRACKING_HOSTNAME
trk_proto = 'http'
self.client = Client(key=key, trk_host=trk_host, trk_proto=trk_proto)
trk_scheme = 'http'
self.client = Client(key=key, trk_host=trk_host, trk_scheme=trk_scheme)
self.identity = None

def identify(self, identity):
self.identity = identity

def record(self, action, props=None, uri=KISSmetrics.RECORD_URI,
def record(self, action, props=None, path=KISSmetrics.RECORD_PATH,
resp=False):
self.check_id_key()
timestamp = None
if not props:
props = {}
response = self.client.record(person=self.identity, event=action,
properties=props, timestamp=timestamp,
uri=uri)
path=path)
if resp:
return response

def set(self, data, uri=KISSmetrics.SET_URI, resp=False):
def set(self, data, path=KISSmetrics.SET_PATH, resp=False):
self.check_id_key()
timestamp = None
response = self.client.set(person=self.identity, properties=data,
timestamp=timestamp, uri=uri)
timestamp=timestamp, path=path)
if resp:
return response

def alias(self, name, alias_to, uri=KISSmetrics.ALIAS_URI, resp=False):
def alias(self, name, alias_to, path=KISSmetrics.ALIAS_PATH, resp=False):
self.check_init()
response = self.client.alias(person=name, identity=alias_to, uri=uri)
response = self.client.alias(person=name, identity=alias_to, path=path)
if resp:
return response

Expand Down
26 changes: 13 additions & 13 deletions KISSmetrics/query_string.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-

KEY_PARAM = '_k'
PERSON_PARAM = '_p'
EVENT_NAME_PARAM = '_n'
TIME_PARAM = '_t'
TIME_FLAG_PARAM = '_d'
ALIAS_PARAM = '_n'
KEY_KEY = '_k'
PERSON_KEY = '_p'
EVENT_NAME_KEY = '_n'
TIME_KEY = '_t'
TIME_FLAG_KEY = '_d'
ALIAS_KEY = '_n'


try:
Expand All @@ -19,13 +19,13 @@ def create_query(key, person, event=None, timestamp=None,
if properties is None:
properties = {}

params = {KEY_PARAM: key, PERSON_PARAM: person}
query_dict = {KEY_KEY: key, PERSON_KEY: person}
if timestamp:
params[TIME_FLAG_PARAM] = 1
params[TIME_PARAM] = timestamp
query_dict[TIME_FLAG_KEY] = 1
query_dict[TIME_KEY] = timestamp
if event:
params[EVENT_NAME_PARAM] = event
query_dict[EVENT_NAME_KEY] = event
if identity:
params[ALIAS_PARAM] = identity
params.update(properties)
return urlencode(params)
query_dict[ALIAS_KEY] = identity
query_dict.update(properties)
return urlencode(query_dict)
24 changes: 18 additions & 6 deletions KISSmetrics/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@
from KISSmetrics.query_string import create_query


def _request(scheme, host, path, query):
return '%s://%s/%s?%s' % (scheme, host, path, query)


def record(key, person, event, timestamp=None, properties=None,
uri=KISSmetrics.RECORD_URI):
scheme=KISSmetrics.TRACKING_SCHEME,
host=KISSmetrics.TRACKING_HOSTNAME,
path=KISSmetrics.RECORD_PATH):
query = create_query(key, person, event=event, timestamp=timestamp,
properties=properties)
return '%s?%s' % (uri, query)
return _request(scheme, host, path, query)


def set(key, person, timestamp=None, properties=None, uri=KISSmetrics.SET_URI):
def set(key, person, timestamp=None, properties=None,
scheme=KISSmetrics.TRACKING_SCHEME,
host=KISSmetrics.TRACKING_HOSTNAME,
path=KISSmetrics.SET_PATH):
query = create_query(key, person, timestamp=timestamp,
properties=properties)
return '%s?%s' % (uri, query)
return _request(scheme, host, path, query)


def alias(key, person, identity, uri=KISSmetrics.ALIAS_URI):
def alias(key, person, identity,
scheme=KISSmetrics.TRACKING_SCHEME,
host=KISSmetrics.TRACKING_HOSTNAME,
path=KISSmetrics.ALIAS_PATH):
query = create_query(key, person, identity=identity)
return '%s?%s' % (uri, query)
return _request(scheme, host, path, query)
40 changes: 20 additions & 20 deletions test_integration_kissmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,49 @@ def setUp(self):
self.client = KISSmetrics.Client(key='foo', trk_host='httpbin.org')

def test_record_success(self):
response = self.client.record(person='bob', event='fizzed', uri='get')
response = self.client.record(person='bob', event='fizzed', path='get')
assert response.status == 200

def test_record_key(self):
response = self.client.record(person='bob', event='fizzed', uri='get')
response = self.client.record(person='bob', event='fizzed', path='get')
data = json.loads(response.data.decode())
assert data['args']['_k'] == 'foo'

def test_record_person(self):
response = self.client.record(person='bob', event='fizzed', uri='get')
response = self.client.record(person='bob', event='fizzed', path='get')
data = json.loads(response.data.decode())
assert data['args']['_p'] == 'bob'

def test_record_event(self):
response = self.client.record(person='bob', event='fizzed', uri='get')
response = self.client.record(person='bob', event='fizzed', path='get')
data = json.loads(response.data.decode())
assert data['args']['_n'] == 'fizzed'

def test_set_success(self):
response = self.client.set(person='bob', properties={'cool': 1}, uri='get')
response = self.client.set(person='bob', properties={'cool': 1}, path='get')
assert response.status == 200

def test_set_key(self):
response = self.client.set(person='bob', properties={'cool': 1}, uri='get')
response = self.client.set(person='bob', properties={'cool': 1}, path='get')
data = json.loads(response.data.decode())
assert data['args']['_k'] == 'foo'

def test_set_person(self):
response = self.client.set(person='bob', properties={'cool': 1}, uri='get')
response = self.client.set(person='bob', properties={'cool': 1}, path='get')
data = json.loads(response.data.decode())
assert data['args']['_p'] == 'bob'

def test_set_property(self):
response = self.client.set(person='bob', properties={'cool': 1}, uri='get')
response = self.client.set(person='bob', properties={'cool': 1}, path='get')
data = json.loads(response.data.decode())
assert data['args']['cool'] == '1'

def test_alias_success(self):
response = self.client.alias(person='bob', identity='shadybob', uri='get')
response = self.client.alias(person='bob', identity='shadybob', path='get')
assert response.status == 200

def test_alias_person(self):
response = self.client.alias(person='bob', identity='shadybob', uri='get')
response = self.client.alias(person='bob', identity='shadybob', path='get')
data = json.loads(response.data.decode())
assert data['args']['_n'] == 'shadybob'

Expand All @@ -67,57 +67,57 @@ def setUp(self):

def test_record_success(self):
self.client.identify('bob')
response = self.client.record('fizzed', uri='get', resp=True)
response = self.client.record('fizzed', path='get', resp=True)
assert response.status == 200

def test_record_key(self):
self.client.identify('bob')
response = self.client.record('fizzed', uri='get', resp=True)
response = self.client.record('fizzed', path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_k'] == 'foo'

def test_record_person(self):
self.client.identify('bob')
response = self.client.record('fizzed', uri='get', resp=True)
response = self.client.record('fizzed', path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_p'] == 'bob'

def test_record_event(self):
self.client.identify('bob')
response = self.client.record('fizzed', uri='get', resp=True)
response = self.client.record('fizzed', path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_n'] == 'fizzed'

def test_set_success(self):
self.client.identify('bob')
response = self.client.set({'cool': 1}, uri='get', resp=True)
response = self.client.set({'cool': 1}, path='get', resp=True)
assert response.status == 200

def test_set_key(self):
self.client.identify('bob')
response = self.client.set({'cool': 1}, uri='get', resp=True)
response = self.client.set({'cool': 1}, path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_k'] == 'foo'

def test_set_person(self):
self.client.identify('bob')
response = self.client.set({'cool': 1}, uri='get', resp=True)
response = self.client.set({'cool': 1}, path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_p'] == 'bob'

def test_set_property(self):
self.client.identify('bob')
response = self.client.set({'cool': 1}, uri='get', resp=True)
response = self.client.set({'cool': 1}, path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['cool'] == '1'

def test_alias_success(self):
self.client.identify('bob')
response = self.client.alias('bob', 'shadybob', uri='get', resp=True)
response = self.client.alias('bob', 'shadybob', path='get', resp=True)
assert response.status == 200

def test_alias_person(self):
self.client.identify('bob')
response = self.client.alias('bob', 'shadybob', uri='get', resp=True)
response = self.client.alias('bob', 'shadybob', path='get', resp=True)
data = json.loads(response.data.decode())
assert data['args']['_n'] == 'shadybob'
Loading

0 comments on commit 61ea233

Please sign in to comment.