Skip to content

Commit c380ac2

Browse files
committed
Add endpoint override logic
1 parent 762e02d commit c380ac2

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

pusher_push_notifications/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ class PushNotifications(object):
88
This client class can be used to publish notifications to the Pusher
99
Push Notifications service"""
1010

11-
def __init__(self, instance_id, secret_key):
11+
def __init__(self, instance_id, secret_key, endpoint=None):
1212
if not isinstance(instance_id, six.string_types):
1313
raise TypeError('instance_id must be a string')
1414
if not isinstance(secret_key, six.string_types):
1515
raise TypeError('secret_key must be a string')
16+
if (endpoint is not None
17+
and not isinstance(endpoint, six.string_types)):
18+
raise TypeError('endpoint must be a string')
19+
20+
self.instance_id = instance_id
21+
self.secret_key = secret_key
22+
self._endpoint = endpoint
23+
24+
@property
25+
def endpoint(self):
26+
"""Property method to calculate the correct Pusher API host"""
27+
default_endpoint = '{}.pushnotifications.pusher.com'.format(
28+
self.instance_id,
29+
)
30+
return self._endpoint or default_endpoint

tests/test_push_notifications.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ def test_constructor_should_fail_if_secret_key_not_string(self):
2525
instance_id='1234',
2626
secret_key=False,
2727
)
28+
29+
def test_constructor_should_fail_if_endpoint_not_string(self):
30+
with self.assertRaises(TypeError):
31+
PushNotifications(
32+
instance_id='1234',
33+
secret_key='1234',
34+
endpoint=False,
35+
)
36+
37+
def test_constructor_should_set_endpoint_default(self):
38+
pn_client = PushNotifications(
39+
instance_id='INSTANCE_ID',
40+
secret_key='1234',
41+
)
42+
self.assertEqual(
43+
pn_client.endpoint,
44+
'INSTANCE_ID.pushnotifications.pusher.com',
45+
)
46+
47+
def test_constructor_should_accept_endpoint_override(self):
48+
pn_client = PushNotifications(
49+
instance_id='INSTANCE_ID',
50+
secret_key='1234',
51+
endpoint='example.com/push',
52+
)
53+
self.assertEqual(
54+
pn_client.endpoint,
55+
'example.com/push',
56+
)

0 commit comments

Comments
 (0)