Skip to content

Commit a4f0e0e

Browse files
committed
New interests form in publish api
1 parent 6716e35 commit a4f0e0e

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ Once you have created your PushNotifications instance you can publish a push not
3838

3939
.. code:: python
4040
41-
response = pn_client.publish({'interests': ['hello'], 'apns': {'aps': {'alert': 'Hello!'}}})
41+
response = pn_client.publish(interests=['hello'], publish_body={'apns': {'aps': {'alert': 'Hello!'}}})
4242
4343
print(response['publishId'])

pusher_push_notifications/__init__.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,49 @@ def endpoint(self):
2929
"""Property method to calculate the correct Pusher API host"""
3030
default_endpoint = '{}.pushnotifications.pusher.com'.format(
3131
self.instance_id,
32-
)
32+
).lower()
3333
return self._endpoint or default_endpoint
3434

35-
def publish(self, publish_body):
36-
requests.post(
35+
def publish(self, interests, publish_body):
36+
"""Publish the given publish_body to the specified interests.
37+
38+
Args:
39+
interests (list): List of interests that the publish body should
40+
be sent to.
41+
publish_body (dict): Dict containing the body of the push
42+
notification publish request.
43+
(see https://docs.pusher.com/push-notifications)
44+
45+
Returns:
46+
A dict containing the publish response from the Pusher Push
47+
Notifications service.
48+
(see https://docs.pusher.com/push-notifications)
49+
50+
Raises:
51+
TypeError: if interests is not a list
52+
TypeError: if publish_body is not a dict
53+
"""
54+
if not isinstance(interests, list):
55+
raise TypeError('interests must be a list')
56+
if not isinstance(publish_body, dict):
57+
raise TypeError('publish_body must be a dictionary')
58+
59+
publish_body['interests'] = interests
60+
61+
session = requests.Session()
62+
request = requests.Request(
63+
'POST',
3764
'https://{}/publish_api/v1/instances/{}/publishes'.format(
3865
self.endpoint,
3966
self.instance_id,
4067
),
4168
json=publish_body,
4269
headers={
70+
'host': self.endpoint,
4371
'authorization': 'Bearer {}'.format(self.secret_key),
4472
'x-pusher-library': 'pusher-push-notifications-python {}'.format(
4573
SDK_VERSION,
4674
)
4775
},
4876
)
77+
session.send(request.prepare())

tests/test_push_notifications.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_constructor_should_set_endpoint_default(self):
4343
)
4444
self.assertEqual(
4545
pn_client.endpoint,
46-
'INSTANCE_ID.pushnotifications.pusher.com',
46+
'instance_id.pushnotifications.pusher.com',
4747
)
4848

4949
def test_constructor_should_accept_endpoint_override(self):
@@ -67,14 +67,16 @@ def test_publish_should_make_correct_http_request(self):
6767
requests_mock.ANY,
6868
requests_mock.ANY,
6969
)
70-
pn_client.publish({
71-
'interests': ['donuts'],
72-
'apns': {
73-
'aps': {
74-
'alert': 'Hello World!',
70+
pn_client.publish(
71+
interests=['donuts'],
72+
publish_body={
73+
'apns': {
74+
'aps': {
75+
'alert': 'Hello World!',
76+
},
7577
},
7678
},
77-
})
79+
)
7880
req = http_mock.request_history[0]
7981

8082
method = req.method
@@ -111,3 +113,31 @@ def test_publish_should_make_correct_http_request(self):
111113
},
112114
},
113115
)
116+
117+
def test_publish_should_fail_if_interests_not_list(self):
118+
pn_client = PushNotifications(
119+
'INSTANCE_ID',
120+
'SECRET_KEY'
121+
)
122+
with self.assertRaises(TypeError):
123+
pn_client.publish(
124+
interests=False,
125+
publish_body={
126+
'apns': {
127+
'aps': {
128+
'alert': 'Hello World!',
129+
},
130+
},
131+
},
132+
)
133+
134+
def test_publish_should_fail_if_body_not_dict(self):
135+
pn_client = PushNotifications(
136+
'INSTANCE_ID',
137+
'SECRET_KEY'
138+
)
139+
with self.assertRaises(TypeError):
140+
pn_client.publish(
141+
interests=['donuts'],
142+
publish_body=False,
143+
)

0 commit comments

Comments
 (0)