Skip to content

Commit 6716e35

Browse files
committed
wip
1 parent c380ac2 commit 6716e35

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

.dev_requirements.txt.swp

12 KB
Binary file not shown.

dev_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nose
22
pylint
3+
requests-mock

pusher_push_notifications/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""Pusher Push Notifications Python server SDK"""
22

3+
import requests
34
import six
45

6+
SDK_VERSION = '0.9.0'
7+
58

69
class PushNotifications(object):
710
"""Pusher Push Notifications API client
@@ -28,3 +31,18 @@ def endpoint(self):
2831
self.instance_id,
2932
)
3033
return self._endpoint or default_endpoint
34+
35+
def publish(self, publish_body):
36+
requests.post(
37+
'https://{}/publish_api/v1/instances/{}/publishes'.format(
38+
self.endpoint,
39+
self.instance_id,
40+
),
41+
json=publish_body,
42+
headers={
43+
'authorization': 'Bearer {}'.format(self.secret_key),
44+
'x-pusher-library': 'pusher-push-notifications-python {}'.format(
45+
SDK_VERSION,
46+
)
47+
},
48+
)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
six==1.10.0
2+
requests==2.18.4

tests/test_push_notifications.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import unittest
44

5+
import requests_mock
6+
57
from pusher_push_notifications import PushNotifications
68

79

@@ -54,3 +56,58 @@ def test_constructor_should_accept_endpoint_override(self):
5456
pn_client.endpoint,
5557
'example.com/push',
5658
)
59+
60+
def test_publish_should_make_correct_http_request(self):
61+
pn_client = PushNotifications(
62+
'INSTANCE_ID',
63+
'SECRET_KEY'
64+
)
65+
with requests_mock.Mocker() as http_mock:
66+
http_mock.register_uri(
67+
requests_mock.ANY,
68+
requests_mock.ANY,
69+
)
70+
pn_client.publish({
71+
'interests': ['donuts'],
72+
'apns': {
73+
'aps': {
74+
'alert': 'Hello World!',
75+
},
76+
},
77+
})
78+
req = http_mock.request_history[0]
79+
80+
method = req.method
81+
path = req.path
82+
headers = dict(req._request.headers.lower_items())
83+
body = req.json()
84+
85+
self.assertEqual(
86+
method,
87+
'POST',
88+
)
89+
self.assertEqual(
90+
path,
91+
'/publish_api/v1/instances/instance_id/publishes',
92+
)
93+
self.assertDictEqual(
94+
headers,
95+
{
96+
'content-type': 'application/json',
97+
'content-length': '69',
98+
'authorization': 'Bearer SECRET_KEY',
99+
'x-pusher-library': 'pusher-push-notifications-python 0.9.0',
100+
'host': 'instance_id.pushnotifications.pusher.com',
101+
},
102+
)
103+
self.assertDictEqual(
104+
body,
105+
{
106+
'interests': ['donuts'],
107+
'apns': {
108+
'aps': {
109+
'alert': 'Hello World!',
110+
},
111+
},
112+
},
113+
)

0 commit comments

Comments
 (0)