Skip to content

Commit 042333e

Browse files
committed
Rename publish to publish_to_interests
1 parent bf68108 commit 042333e

File tree

2 files changed

+137
-31
lines changed

2 files changed

+137
-31
lines changed

pusher_push_notifications/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import re
77
import time
8+
import warnings
89

910
import jwt
1011
import requests
@@ -99,6 +100,44 @@ def endpoint(self):
99100
def publish(self, interests, publish_body):
100101
"""Publish the given publish_body to the specified interests.
101102
103+
Args:
104+
interests (list): List of interests that the publish body should
105+
be sent to.
106+
publish_body (dict): Dict containing the body of the push
107+
notification publish request.
108+
(see https://docs.pusher.com/push-notifications)
109+
110+
Returns:
111+
A dict containing the publish response from the Pusher Push
112+
Notifications service.
113+
(see https://docs.pusher.com/push-notifications)
114+
115+
Raises:
116+
PusherAuthError: if the secret_key is incorrect
117+
PusherMissingInstanceError: if the instance_id is incorrect
118+
PusherServerError: if the Push Notifications service returns
119+
an error
120+
PusherValidationError: if the publish_body is invalid
121+
TypeError: if interests is not a list
122+
TypeError: if publish_body is not a dict
123+
TypeError: if any interest is not a string
124+
ValueError: if len(interests) < 1
125+
ValueError: if len(interests) > 100
126+
ValueError: if any interest length is greater than the max
127+
ValueError: if any interest contains a forbidden character
128+
129+
.. deprecated::
130+
Use :func:`publish_to_interests` instead.
131+
"""
132+
warnings.warn(
133+
"publish method is deprecated. Please use publish_to_interests.",
134+
DeprecationWarning
135+
)
136+
return self.publish_to_interests(interests, publish_body)
137+
138+
def publish_to_interests(self, interests, publish_body):
139+
"""Publish the given publish_body to the specified interests.
140+
102141
Args:
103142
interests (list): List of interests that the publish body should
104143
be sent to.

tests/test_interests.py

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,74 @@
1616

1717

1818
class TestPushNotificationsInterests(unittest.TestCase):
19-
def test_publish_should_make_correct_http_request(self):
19+
def test_publish_to_interests_should_make_correct_http_request(self):
20+
pn_client = PushNotifications(
21+
'INSTANCE_ID',
22+
'SECRET_KEY'
23+
)
24+
with requests_mock.Mocker() as http_mock:
25+
http_mock.register_uri(
26+
requests_mock.ANY,
27+
requests_mock.ANY,
28+
status_code=200,
29+
json={
30+
'publishId': '1234',
31+
},
32+
)
33+
response = pn_client.publish_to_interests(
34+
interests=['donuts'],
35+
publish_body={
36+
'apns': {
37+
'aps': {
38+
'alert': 'Hello World!',
39+
},
40+
},
41+
},
42+
)
43+
req = http_mock.request_history[0]
44+
45+
method = req.method
46+
path = req.path
47+
headers = dict(req._request.headers.lower_items())
48+
body = req.json()
49+
50+
self.assertEqual(
51+
method,
52+
'POST',
53+
)
54+
self.assertEqual(
55+
path,
56+
'/publish_api/v1/instances/instance_id/publishes',
57+
)
58+
self.assertDictEqual(
59+
headers,
60+
{
61+
'content-type': 'application/json',
62+
'content-length': '69',
63+
'authorization': 'Bearer SECRET_KEY',
64+
'x-pusher-library': 'pusher-push-notifications-python 1.0.1',
65+
'host': 'instance_id.pushnotifications.pusher.com',
66+
},
67+
)
68+
self.assertDictEqual(
69+
body,
70+
{
71+
'interests': ['donuts'],
72+
'apns': {
73+
'aps': {
74+
'alert': 'Hello World!',
75+
},
76+
},
77+
},
78+
)
79+
self.assertDictEqual(
80+
response,
81+
{
82+
'publishId': '1234',
83+
},
84+
)
85+
86+
def test_deprecated_alias_still_works(self):
2087
pn_client = PushNotifications(
2188
'INSTANCE_ID',
2289
'SECRET_KEY'
@@ -83,13 +150,13 @@ def test_publish_should_make_correct_http_request(self):
83150
},
84151
)
85152

86-
def test_publish_should_fail_if_interests_not_list(self):
153+
def test_publish_to_interests_should_fail_if_interests_not_list(self):
87154
pn_client = PushNotifications(
88155
'INSTANCE_ID',
89156
'SECRET_KEY'
90157
)
91158
with self.assertRaises(TypeError) as e:
92-
pn_client.publish(
159+
pn_client.publish_to_interests(
93160
interests=False,
94161
publish_body={
95162
'apns': {
@@ -101,25 +168,25 @@ def test_publish_should_fail_if_interests_not_list(self):
101168
)
102169
self.assertIn('interests must be a list', str(e.exception))
103170

104-
def test_publish_should_fail_if_body_not_dict(self):
171+
def test_publish_to_interests_should_fail_if_body_not_dict(self):
105172
pn_client = PushNotifications(
106173
'INSTANCE_ID',
107174
'SECRET_KEY'
108175
)
109176
with self.assertRaises(TypeError) as e:
110-
pn_client.publish(
177+
pn_client.publish_to_interests(
111178
interests=['donuts'],
112179
publish_body=False,
113180
)
114181
self.assertIn('publish_body must be a dictionary', str(e.exception))
115182

116-
def test_publish_should_fail_if_no_interests_passed(self):
183+
def test_publish_to_interests_should_fail_if_no_interests_passed(self):
117184
pn_client = PushNotifications(
118185
'INSTANCE_ID',
119186
'SECRET_KEY'
120187
)
121188
with self.assertRaises(ValueError) as e:
122-
pn_client.publish(
189+
pn_client.publish_to_interests(
123190
interests=[],
124191
publish_body={
125192
'apns': {
@@ -131,7 +198,7 @@ def test_publish_should_fail_if_no_interests_passed(self):
131198
)
132199
self.assertIn('must target at least one interest', str(e.exception))
133200

134-
def test_publish_should_succeed_if_100_interests_passed(self):
201+
def test_publish_to_interests_should_succeed_if_100_interests_passed(self):
135202
pn_client = PushNotifications(
136203
'INSTANCE_ID',
137204
'SECRET_KEY'
@@ -145,7 +212,7 @@ def test_publish_should_succeed_if_100_interests_passed(self):
145212
'publishId': '1234',
146213
},
147214
)
148-
pn_client.publish(
215+
pn_client.publish_to_interests(
149216
interests=['interest-' + str(i) for i in range(0, 100)],
150217
publish_body={
151218
'apns': {
@@ -156,7 +223,7 @@ def test_publish_should_succeed_if_100_interests_passed(self):
156223
},
157224
)
158225

159-
def test_publish_should_fail_if_too_many_interests_passed(self):
226+
def test_publish_to_interests_should_fail_if_too_many_interests_passed(self):
160227
pn_client = PushNotifications(
161228
'INSTANCE_ID',
162229
'SECRET_KEY'
@@ -171,7 +238,7 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
171238
},
172239
)
173240
with self.assertRaises(ValueError) as e:
174-
pn_client.publish(
241+
pn_client.publish_to_interests(
175242
interests=['interest-' + str(i) for i in range(0, 101)],
176243
publish_body={
177244
'apns': {
@@ -183,13 +250,13 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
183250
)
184251
self.assertIn('Number of interests (101) exceeds maximum', str(e.exception))
185252

186-
def test_publish_should_fail_if_interest_not_a_string(self):
253+
def test_publish_to_interests_should_fail_if_interest_not_a_string(self):
187254
pn_client = PushNotifications(
188255
'INSTANCE_ID',
189256
'SECRET_KEY'
190257
)
191258
with self.assertRaises(TypeError) as e:
192-
pn_client.publish(
259+
pn_client.publish_to_interests(
193260
interests=[False],
194261
publish_body={
195262
'apns': {
@@ -201,13 +268,13 @@ def test_publish_should_fail_if_interest_not_a_string(self):
201268
)
202269
self.assertIn('Interest False is not a string', str(e.exception))
203270

204-
def test_publish_should_fail_if_interest_too_long(self):
271+
def test_publish_to_interests_should_fail_if_interest_too_long(self):
205272
pn_client = PushNotifications(
206273
'INSTANCE_ID',
207274
'SECRET_KEY'
208275
)
209276
with self.assertRaises(ValueError) as e:
210-
pn_client.publish(
277+
pn_client.publish_to_interests(
211278
interests=['A'*200],
212279
publish_body={
213280
'apns': {
@@ -219,13 +286,13 @@ def test_publish_should_fail_if_interest_too_long(self):
219286
)
220287
self.assertIn('longer than the maximum of 164 chars', str(e.exception))
221288

222-
def test_publish_should_fail_if_interest_contains_invalid_chars(self):
289+
def test_publish_to_interests_should_fail_if_interest_contains_invalid_chars(self):
223290
pn_client = PushNotifications(
224291
'INSTANCE_ID',
225292
'SECRET_KEY'
226293
)
227294
with self.assertRaises(ValueError) as e:
228-
pn_client.publish(
295+
pn_client.publish_to_interests(
229296
interests=['bad:interest'],
230297
publish_body={
231298
'apns': {
@@ -238,7 +305,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
238305
self.assertIn('"bad:interest" contains a forbidden character', str(e.exception))
239306

240307
with self.assertRaises(ValueError) as e:
241-
pn_client.publish(
308+
pn_client.publish_to_interests(
242309
interests=['bad|interest'],
243310
publish_body={
244311
'apns': {
@@ -251,7 +318,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
251318
self.assertIn('"bad|interest" contains a forbidden character', str(e.exception))
252319

253320
with self.assertRaises(ValueError) as e:
254-
pn_client.publish(
321+
pn_client.publish_to_interests(
255322
interests=['bad(interest)'],
256323
publish_body={
257324
'apns': {
@@ -263,7 +330,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
263330
)
264331
self.assertIn('"bad(interest)" contains a forbidden character', str(e.exception))
265332

266-
def test_publish_should_raise_on_http_4xx_error(self):
333+
def test_publish_to_interests_should_raise_on_http_4xx_error(self):
267334
pn_client = PushNotifications(
268335
'INSTANCE_ID',
269336
'SECRET_KEY'
@@ -276,7 +343,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
276343
json={'error': 'Invalid request', 'description': 'blah'},
277344
)
278345
with self.assertRaises(PusherValidationError) as e:
279-
pn_client.publish(
346+
pn_client.publish_to_interests(
280347
interests=['donuts'],
281348
publish_body={
282349
'apns': {
@@ -288,7 +355,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
288355
)
289356
self.assertIn('Invalid request: blah', str(e.exception))
290357

291-
def test_publish_should_raise_on_http_5xx_error(self):
358+
def test_publish_to_interests_should_raise_on_http_5xx_error(self):
292359
pn_client = PushNotifications(
293360
'INSTANCE_ID',
294361
'SECRET_KEY'
@@ -301,7 +368,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
301368
json={'error': 'Server error', 'description': 'blah'},
302369
)
303370
with self.assertRaises(PusherServerError) as e:
304-
pn_client.publish(
371+
pn_client.publish_to_interests(
305372
interests=['donuts'],
306373
publish_body={
307374
'apns': {
@@ -313,7 +380,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
313380
)
314381
self.assertIn('Server error: blah', str(e.exception))
315382

316-
def test_publish_should_raise_on_http_401_error(self):
383+
def test_publish_to_interests_should_raise_on_http_401_error(self):
317384
pn_client = PushNotifications(
318385
'INSTANCE_ID',
319386
'SECRET_KEY'
@@ -326,7 +393,7 @@ def test_publish_should_raise_on_http_401_error(self):
326393
json={'error': 'Auth error', 'description': 'blah'},
327394
)
328395
with self.assertRaises(PusherAuthError) as e:
329-
pn_client.publish(
396+
pn_client.publish_to_interests(
330397
interests=['donuts'],
331398
publish_body={
332399
'apns': {
@@ -338,7 +405,7 @@ def test_publish_should_raise_on_http_401_error(self):
338405
)
339406
self.assertIn('Auth error: blah', str(e.exception))
340407

341-
def test_publish_should_raise_on_http_404_error(self):
408+
def test_publish_to_interests_should_raise_on_http_404_error(self):
342409
pn_client = PushNotifications(
343410
'INSTANCE_ID',
344411
'SECRET_KEY'
@@ -351,7 +418,7 @@ def test_publish_should_raise_on_http_404_error(self):
351418
json={'error': 'Instance not found', 'description': 'blah'},
352419
)
353420
with self.assertRaises(PusherMissingInstanceError) as e:
354-
pn_client.publish(
421+
pn_client.publish_to_interests(
355422
interests=['donuts'],
356423
publish_body={
357424
'apns': {
@@ -364,7 +431,7 @@ def test_publish_should_raise_on_http_404_error(self):
364431
self.assertIn('Instance not found: blah', str(e.exception))
365432

366433

367-
def test_publish_should_error_correctly_if_error_not_json(self):
434+
def test_publish_to_interests_should_error_correctly_if_error_not_json(self):
368435
pn_client = PushNotifications(
369436
'INSTANCE_ID',
370437
'SECRET_KEY'
@@ -377,7 +444,7 @@ def test_publish_should_error_correctly_if_error_not_json(self):
377444
text='<notjson></notjson>',
378445
)
379446
with self.assertRaises(PusherServerError) as e:
380-
pn_client.publish(
447+
pn_client.publish_to_interests(
381448
interests=['donuts'],
382449
publish_body={
383450
'apns': {
@@ -389,7 +456,7 @@ def test_publish_should_error_correctly_if_error_not_json(self):
389456
)
390457
self.assertIn('Unknown error: no description', str(e.exception))
391458

392-
def test_publish_should_handle_not_json_success(self):
459+
def test_publish_to_interests_should_handle_not_json_success(self):
393460
pn_client = PushNotifications(
394461
'INSTANCE_ID',
395462
'SECRET_KEY'
@@ -402,7 +469,7 @@ def test_publish_should_handle_not_json_success(self):
402469
text='<notjson></notjson>',
403470
)
404471
with self.assertRaises(PusherBadResponseError) as e:
405-
pn_client.publish(
472+
pn_client.publish_to_interests(
406473
interests=['donuts'],
407474
publish_body={
408475
'apns': {

0 commit comments

Comments
 (0)