Skip to content

Commit

Permalink
run Black
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vdb committed Jan 16, 2019
1 parent be9411d commit 1481a22
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
25 changes: 7 additions & 18 deletions heapapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class HeapAPIClient(object):
"""
The client for the Heap Api.
"""

base_url = "https://heapanalytics.com/api"
headers = {'Content-Type': 'application/json'}
headers = {"Content-Type": "application/json"}

def __init__(self, app_id):
"""
Expand All @@ -18,7 +19,7 @@ def __init__(self, app_id):
:param app_id: Heap analytics app_id
:type app_id: str
"""
assert app_id, 'app_id must be valid!'
assert app_id, "app_id must be valid!"
self.app_id = str(app_id)

def track(self, identity, event, properties=None):
Expand All @@ -32,19 +33,13 @@ def track(self, identity, event, properties=None):
:param properties: optional, additional event properties
:type properties: dict
"""
data = {
"app_id": self.app_id,
"identity": identity,
"event": event
}
data = {"app_id": self.app_id, "identity": identity, "event": event}

if properties is not None:
data["properties"] = properties

response = requests.post(
self.base_url + '/track',
data=json.dumps(data),
headers=self.headers
self.base_url + "/track", data=json.dumps(data), headers=self.headers
)
response.raise_for_status()
return response
Expand All @@ -58,16 +53,10 @@ def add_user_properties(self, identity, properties):
:param properties: additional properties to associate with the user
:type properties: dict
"""
data = {
"app_id": self.app_id,
"identity": identity,
"properties": properties,
}
data = {"app_id": self.app_id, "identity": identity, "properties": properties}

response = requests.post(
self.base_url + '/add_user_properties',
data=json.dumps(data),
headers=self.headers
self.base_url + "/add_user_properties", data=json.dumps(data), headers=self.headers
)
response.raise_for_status()
return response
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.black]
line-length = 100
py36 = true
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
)/
'''
53 changes: 23 additions & 30 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,56 @@


class HeapAPIClientTestCase(unittest.TestCase):

def test_init_missing_app_id(self):
with self.assertRaises(AssertionError):
HeapAPIClient(None)

def test_init_ok(self):
client = HeapAPIClient(12)
self.assertEqual(client.app_id, '12')
self.assertEqual(client.app_id, "12")

@patch('requests.post')
@patch("requests.post")
def test_track(self, request_post):
client = HeapAPIClient(42)
resp = client.track('xxx', 'Purchase')
resp = client.track("xxx", "Purchase")

request_post.assert_called_with(
'https://heapanalytics.com/api/track',
data=json.dumps({
"app_id": "42",
"identity": "xxx",
"event": "Purchase"
}),
headers={'Content-Type': 'application/json'}
"https://heapanalytics.com/api/track",
data=json.dumps({"app_id": "42", "identity": "xxx", "event": "Purchase"}),
headers={"Content-Type": "application/json"},
)
self.assertEqual(resp, request_post.return_value)
resp.raise_for_status.assert_called_with()

@patch('requests.post')
@patch("requests.post")
def test_track_with_properties(self, request_post):
client = HeapAPIClient(42)
resp = client.track('xxx', 'Purchase', {'amount': 12, 'currency': 'USD'})
resp = client.track("xxx", "Purchase", {"amount": 12, "currency": "USD"})

request_post.assert_called_with(
'https://heapanalytics.com/api/track',
data=json.dumps({
"app_id": "42",
"identity": "xxx",
"event": "Purchase",
"properties": {"amount": 12, "currency": "USD"}
}),
headers={'Content-Type': 'application/json'}
"https://heapanalytics.com/api/track",
data=json.dumps(
{
"app_id": "42",
"identity": "xxx",
"event": "Purchase",
"properties": {"amount": 12, "currency": "USD"},
}
),
headers={"Content-Type": "application/json"},
)
self.assertEqual(resp, request_post.return_value)
resp.raise_for_status.assert_called_with()

@patch('requests.post')
@patch("requests.post")
def test_add_user_properties(self, request_post):
client = HeapAPIClient(42)
resp = client.add_user_properties('xxx', {'age': 22})
resp = client.add_user_properties("xxx", {"age": 22})

request_post.assert_called_with(
'https://heapanalytics.com/api/add_user_properties',
data=json.dumps({
"app_id": "42",
"identity": "xxx",
"properties": {"age": 22}
}),
headers={'Content-Type': 'application/json'}
"https://heapanalytics.com/api/add_user_properties",
data=json.dumps({"app_id": "42", "identity": "xxx", "properties": {"age": 22}}),
headers={"Content-Type": "application/json"},
)
self.assertEqual(resp, request_post.return_value)
resp.raise_for_status.assert_called_with()

0 comments on commit 1481a22

Please sign in to comment.