5
5
import json
6
6
import re
7
7
import time
8
+ import urllib
8
9
import warnings
9
10
10
11
import jwt
@@ -49,8 +50,7 @@ class PusherBadResponseError(PusherError, Exception):
49
50
"""
50
51
51
52
52
- def handle_http_error (response_body , status_code ):
53
- """Handle different http error codes from the Push Notifications service"""
53
+ def _handle_http_error (response_body , status_code ):
54
54
error_string = '{}: {}' .format (
55
55
response_body .get ('error' , 'Unknown error' ),
56
56
response_body .get ('description' , 'no description' ),
@@ -65,6 +65,17 @@ def handle_http_error(response_body, status_code):
65
65
raise PusherServerError (error_string )
66
66
67
67
68
+ def _make_url (scheme , host , path ):
69
+ return urllib .parse .urlunparse ([
70
+ scheme ,
71
+ host ,
72
+ path ,
73
+ None ,
74
+ None ,
75
+ None ,
76
+ ])
77
+
78
+
68
79
class PushNotifications (object ):
69
80
"""Pusher Push Notifications API client
70
81
This client class can be used to publish notifications to the Pusher
@@ -97,6 +108,46 @@ def endpoint(self):
97
108
).lower ()
98
109
return self ._endpoint or default_endpoint
99
110
111
+ def _make_request (self , method , path , path_params , body = None ):
112
+ path_params = {
113
+ name : urllib .parse .quote (value )
114
+ for name , value in path_params .items ()
115
+ }
116
+ path = path .format (** path_params )
117
+ url = _make_url (scheme = 'https' , host = self .endpoint , path = path )
118
+
119
+ session = requests .Session ()
120
+ request = requests .Request (
121
+ method ,
122
+ url ,
123
+ json = body ,
124
+ headers = {
125
+ 'host' : self .endpoint ,
126
+ 'authorization' : 'Bearer {}' .format (self .secret_key ),
127
+ 'x-pusher-library' : 'pusher-push-notifications-python {}' .format (
128
+ SDK_VERSION ,
129
+ )
130
+ },
131
+ )
132
+
133
+ response = session .send (request .prepare ())
134
+
135
+ if response .status_code != 200 :
136
+ try :
137
+ error_body = response .json ()
138
+ except ValueError :
139
+ error_body = {}
140
+ _handle_http_error (error_body , response .status_code )
141
+
142
+ try :
143
+ response_body = response .json ()
144
+ except ValueError :
145
+ raise PusherBadResponseError (
146
+ 'The server returned a malformed response' ,
147
+ )
148
+
149
+ return response_body
150
+
100
151
def publish (self , interests , publish_body ):
101
152
"""Publish the given publish_body to the specified interests.
102
153
@@ -201,39 +252,16 @@ def publish_to_interests(self, interests, publish_body):
201
252
202
253
publish_body = copy .deepcopy (publish_body )
203
254
publish_body ['interests' ] = interests
204
- session = requests .Session ()
205
- request = requests .Request (
206
- 'POST' ,
207
- 'https://{}/publish_api/v1/instances/{}/publishes' .format (
208
- self .endpoint ,
209
- self .instance_id ,
210
- ),
211
- json = publish_body ,
212
- headers = {
213
- 'host' : self .endpoint ,
214
- 'authorization' : 'Bearer {}' .format (self .secret_key ),
215
- 'x-pusher-library' : 'pusher-push-notifications-python {}' .format (
216
- SDK_VERSION ,
217
- )
255
+
256
+ response_body = self ._make_request (
257
+ method = 'POST' ,
258
+ path = '/publish_api/v1/instances/{instance_id}/publishes/interests' ,
259
+ path_params = {
260
+ 'instance_id' : self .instance_id ,
218
261
},
262
+ body = publish_body ,
219
263
)
220
264
221
- response = session .send (request .prepare ())
222
-
223
- if response .status_code != 200 :
224
- try :
225
- response_body = response .json ()
226
- except ValueError :
227
- response_body = {}
228
- handle_http_error (response_body , response .status_code )
229
-
230
- try :
231
- response_body = response .json ()
232
- except ValueError :
233
- raise PusherBadResponseError (
234
- 'The server returned a malformed response' ,
235
- )
236
-
237
265
return response_body
238
266
239
267
def publish_to_users (self , user_ids , publish_body ):
@@ -293,39 +321,16 @@ def publish_to_users(self, user_ids, publish_body):
293
321
294
322
publish_body = copy .deepcopy (publish_body )
295
323
publish_body ['users' ] = user_ids
296
- session = requests .Session ()
297
- request = requests .Request (
298
- 'POST' ,
299
- 'https://{}/publish_api/v1/instances/{}/publishes/users' .format (
300
- self .endpoint ,
301
- self .instance_id ,
302
- ),
303
- json = publish_body ,
304
- headers = {
305
- 'host' : self .endpoint ,
306
- 'authorization' : 'Bearer {}' .format (self .secret_key ),
307
- 'x-pusher-library' : 'pusher-push-notifications-python {}' .format (
308
- SDK_VERSION ,
309
- )
324
+
325
+ response_body = self ._make_request (
326
+ method = 'POST' ,
327
+ path = '/publish_api/v1/instances/{instance_id}/publishes/users' ,
328
+ path_params = {
329
+ 'instance_id' : self .instance_id ,
310
330
},
331
+ body = publish_body ,
311
332
)
312
333
313
- response = session .send (request .prepare ())
314
-
315
- if response .status_code != 200 :
316
- try :
317
- response_body = response .json ()
318
- except ValueError :
319
- response_body = {}
320
- handle_http_error (response_body , response .status_code )
321
-
322
- try :
323
- response_body = response .json ()
324
- except ValueError :
325
- raise PusherBadResponseError (
326
- 'The server returned a malformed response' ,
327
- )
328
-
329
334
return response_body
330
335
331
336
def authenticate_user (self , user_id ):
@@ -385,20 +390,11 @@ def delete_user(self, user_id):
385
390
if len (user_id ) > USER_ID_MAX_LENGTH :
386
391
raise ValueError ('user_id longer than the maximum of 164 chars' )
387
392
388
- session = requests .Session ()
389
- request = requests .Request (
390
- 'DELETE' ,
391
- 'https://{}/user_api/v1/instances/{}/users/{}' .format (
392
- self .endpoint ,
393
- self .instance_id ,
394
- user_id ,
395
- ),
396
- headers = {
397
- 'host' : self .endpoint ,
398
- 'authorization' : 'Bearer {}' .format (self .secret_key ),
399
- 'x-pusher-library' : 'pusher-push-notifications-python {}' .format (
400
- SDK_VERSION ,
401
- )
393
+ self ._make_request (
394
+ method = 'DELETE' ,
395
+ path = '/user_api/v1/instances/{instance_id}/users/{user_id}' ,
396
+ path_params = {
397
+ 'instance_id' : self .instance_id ,
398
+ 'user_id' : user_id ,
402
399
},
403
400
)
404
- session .send (request .prepare ())
0 commit comments