11# -*- coding: utf-8 -*-
22
3- from __future__ import (print_function , unicode_literals , absolute_import ,
4- division )
3+ from __future__ import (
4+ print_function ,
5+ unicode_literals ,
6+ absolute_import ,
7+ division )
8+
59from pusher .errors import *
610from pusher .signature import sign
711from pusher .version import VERSION
1216import six
1317import time
1418
19+
1520GET , POST , PUT , DELETE = "GET" , "POST" , "PUT" , "DELETE"
1621
22+
1723class RequestMethod (object ):
1824 def __init__ (self , client , f ):
1925 self .client = client
2026 self .f = f
2127
28+
2229 def __call__ (self , * args , ** kwargs ):
2330 return self .client .http .send_request (self .make_request (* args , ** kwargs ))
2431
32+
2533 def make_request (self , * args , ** kwargs ):
2634 return self .f (self .client , * args , ** kwargs )
2735
36+
2837def doc_string (doc ):
2938 def decorator (f ):
3039 f .__doc__ = doc
3140 return f
41+
3242 return decorator
3343
44+
3445def request_method (f ):
3546 @property
3647 @doc_string (f .__doc__ )
3748 def wrapped (self ):
3849 return RequestMethod (self , f )
50+
3951 return wrapped
4052
53+
4154def make_query_string (params ):
4255 return '&' .join (map ('=' .join , sorted (params .items (), key = lambda x : x [0 ])))
4356
57+
4458def process_response (status , body ):
4559 if status == 200 or status == 202 :
4660 return json .loads (body )
61+
4762 elif status == 400 :
4863 raise PusherBadRequest (body )
64+
4965 elif status == 401 :
5066 raise PusherBadAuth (body )
67+
5168 elif status == 403 :
5269 raise PusherForbidden (body )
70+
5371 else :
5472 raise PusherBadStatus ("%s: %s" % (status , body ))
5573
74+
5675class Request (object ):
5776 """Represents the request to be made to the Pusher API.
5877
@@ -67,56 +86,68 @@ class Request(object):
6786 def __init__ (self , client , method , path , params = None ):
6887 if params is None :
6988 params = {}
89+
7090 self .client = client
7191 self .method = method
7292 self .path = path
7393 self .params = copy .copy (params )
7494 if method == POST :
7595 self .body = six .text_type (json .dumps (params )).encode ('utf8' )
7696 self .query_params = {}
97+
7798 elif method == GET :
7899 self .body = bytes ()
79100 self .query_params = params
101+
80102 else :
81103 raise NotImplementedError ("Only GET and POST supported" )
104+
82105 self ._generate_auth ()
83106
107+
84108 def _generate_auth (self ):
85109 self .body_md5 = hashlib .md5 (self .body ).hexdigest ()
86110 self .query_params .update ({
87111 'auth_key' : self .client .key ,
88112 'body_md5' : six .text_type (self .body_md5 ),
89113 'auth_version' : '1.0' ,
90- 'auth_timestamp' : '%.0f' % time .time ()
91- })
114+ 'auth_timestamp' : '%.0f' % time .time ()})
92115
93116 auth_string = '\n ' .join ([
94117 self .method ,
95118 self .path ,
96- make_query_string (self .query_params )
97- ])
119+ make_query_string (self .query_params )])
120+
121+ self .query_params ['auth_signature' ] = sign (
122+ self .client .secret , auth_string )
98123
99- self .query_params ['auth_signature' ] = sign (self .client .secret , auth_string )
100124
101125 @property
102126 def query_string (self ):
103127 return make_query_string (self .query_params )
104128
129+
105130 @property
106131 def signed_path (self ):
107132 return "%s?%s" % (self .path , self .query_string )
108133
134+
109135 @property
110136 def url (self ):
111137 return "%s%s" % (self .base_url , self .signed_path )
112138
139+
113140 @property
114141 def base_url (self ):
115- return "%s://%s:%s" % (self .client .scheme , self .client .host , self .client .port )
142+ return (
143+ "%s://%s:%s" %
144+ (self .client .scheme , self .client .host , self .client .port ))
145+
116146
117147 @property
118148 def headers (self ):
119149 hdrs = {"X-Pusher-Library" : "pusher-http-python " + VERSION }
120150 if self .method == POST :
121151 hdrs ["Content-Type" ] = "application/json"
152+
122153 return hdrs
0 commit comments