File tree Expand file tree Collapse file tree 5 files changed +77
-0
lines changed
pusher_push_notifications Expand file tree Collapse file tree 5 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1
1
nose
2
2
pylint
3
+ requests-mock
Original file line number Diff line number Diff line change 1
1
"""Pusher Push Notifications Python server SDK"""
2
2
3
+ import requests
3
4
import six
4
5
6
+ SDK_VERSION = '0.9.0'
7
+
5
8
6
9
class PushNotifications (object ):
7
10
"""Pusher Push Notifications API client
@@ -28,3 +31,18 @@ def endpoint(self):
28
31
self .instance_id ,
29
32
)
30
33
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
+ )
Original file line number Diff line number Diff line change 1
1
six == 1.10.0
2
+ requests == 2.18.4
Original file line number Diff line number Diff line change 2
2
3
3
import unittest
4
4
5
+ import requests_mock
6
+
5
7
from pusher_push_notifications import PushNotifications
6
8
7
9
@@ -54,3 +56,58 @@ def test_constructor_should_accept_endpoint_override(self):
54
56
pn_client .endpoint ,
55
57
'example.com/push' ,
56
58
)
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
+ )
You can’t perform that action at this time.
0 commit comments