Skip to content

Commit 3303601

Browse files
committed
add unicode-ready safe_encode
1 parent a974d09 commit 3303601

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

sunlight/service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@
1717
from urllib.parse import urlencode
1818
from urllib.request import urlopen
1919
from urllib.error import HTTPError
20+
_str_type = str
2021
else:
2122
from urllib import urlencode
2223
from urllib2 import urlopen
2324
from urllib2 import HTTPError
25+
_str_type = basestring
26+
27+
28+
def safe_encode(kwargs):
29+
kwargs = kwargs.copy()
30+
for k, v in kwargs.iteritems():
31+
if isinstance(v, _str_type):
32+
kwargs[k] = v.encode('utf8')
33+
return urlencode(kwargs)
2434

2535

2636
class Service:

sunlight/services/capitolwords.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _get_url( self, obj, apikey, **kwargs ):
9696
service_url,
9797
obj,
9898
apikey,
99-
sunlight.service.urlencode(kwargs)
99+
sunlight.service.safe_encode(kwargs)
100100
)
101101
return ret
102102

sunlight/services/congress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def _get_url(self, obj, apikey, **kwargs):
131131
service_url,
132132
obj,
133133
apikey,
134-
sunlight.service.urlencode(kwargs)
134+
sunlight.service.safe_encode(kwargs)
135135
)
136136

137137
def _decode_response(self, response):

sunlight/services/influenceexplorer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _get_url( self, obj, apikey, **kwargs ):
8383
service_url,
8484
obj,
8585
apikey,
86-
sunlight.service.urlencode(kwargs)
86+
sunlight.service.safe_encode(kwargs)
8787
)
8888

8989
def _decode_response( self, response ):

sunlight/services/openstates.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ def district_boundary(self, boundary_id, **kwargs):
178178
# API impl methods
179179

180180
def _get_url(self, objs, apikey, **kwargs):
181-
nargs = {}
182-
for arg in kwargs:
183-
nargs[arg] = kwargs[arg].encode('utf-8')
184-
kwargs = nargs
185-
186181
# Gate for any None's in the query. This is usually a problem.
187182
if None in objs:
188183
raise BadRequestException("`None' passed to the URL encoder (%s)" %
@@ -196,7 +191,7 @@ def _get_url(self, objs, apikey, **kwargs):
196191
service_url,
197192
object_path,
198193
apikey,
199-
sunlight.service.urlencode(kwargs)
194+
sunlight.service.safe_encode(kwargs)
200195
)
201196

202197
def _decode_response(self, response):

0 commit comments

Comments
 (0)