8000 move flatten_params, sorted_params, normalize_params out of auth into… · boostsup/wp-api-python@1b7e03d · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b7e03d

Browse files
author
derwentx
committed
move flatten_params, sorted_params, normalize_params out of auth into helpers
1 parent 9e10b57 commit 1b7e03d

File tree

3 files changed

+52
-56
lines changed

3 files changed

+52
-56
lines changed

tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def check_sorted(keys, expected):
171171
for key in keys:
172172
params[key] = ''
173173

174-
params = auth.OAuth.sorted_params(params)
174+
params = UrlUtils.sorted_params(params)
175175
ordered = [key for key, value in params]
176176
self.assertEqual(ordered, expected)
177177

@@ -584,7 +584,7 @@ def test_get_sign_key(self):
584584
)
585585

586586
def test_flatten_params(self):
587-
flattened_params = OAuth.flatten_params(self.twitter_params_raw)
587+
flattened_params = UrlUtils.flatten_params(self.twitter_params_raw)
588588
expected_flattened_params = self.twitter_param_string
589589
self.assertEqual(flattened_params, expected_flattened_params)
590590

wordpress/auth.py

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,6 @@ def api_version(self):
4646
def api_namespace(self):
4747
return self.requester.api
4848

49-
@classmethod
50-
def normalize_params(cls, params):
51-
""" Normalize parameters. works with RFC 5849 logic. params is a list of key, value pairs """
52-
if isinstance(params, dict):
53-
params = params.items()
54-
params = \
55-
[(cls.normalize_str(key), cls.normalize_str(UrlUtils.get_value_like_as_php(value))) \
56-
for key, value in params]
57-
58-
# print "NORMALIZED: %s\n" % str(params.keys())
59-
# resposne = urlencode(params)
60-
response = params
61-
# print "RESPONSE: %s\n" % str(resposne.split('&'))
62-
return response
63-
64-
@classmethod
65-
def sorted_params(cls, params):
66-
""" Sort parameters. works with RFC 5849 logic. params is a list of key, value pairs """
67-
68-
if isinstance(params, dict):
69-
params = params.items()
70-
71-
# return sorted(params)
72-
ordered = []
73-
base_keys = sorted(set(k.split('[')[0] for k, v in params))
74-
keys_seen = []
75-
for base in base_keys:
76-
for key, value in params:
77-
if key == base or key.startswith(base + '['):
78-
if key not in keys_seen:
79-
ordered.append((key, value))
80-
keys_seen.append(key)
81-
82-
return ordered
83-
84-
@classmethod
85-
def normalize_str(cls, string):
86-
return quote(string, '')
87-
88-
@classmethod
89-
def flatten_params(cls, params):
90-
if isinstance(params, dict):
91-
params = params.items()
92-
params = cls.normalize_params(params)
93-
params = cls.sorted_params(params)
94-
return "&".join(["%s=%s"%(key, value) for key, value in params])
95-
9649
def get_auth_url(self, endpoint_url, method):
9750
""" Returns the URL with added Auth params """
9851
return endpoint_url
@@ -117,7 +70,7 @@ def get_auth_url(self, endpoint_url, method):
11770
})
11871
endpoint_url = UrlUtils.substitute_query(
11972
endpoint_url,
120-
self.flatten_params(endpoint_params)
73+
UrlUtils.flatten_params(endpoint_params)
12174
)
12275
return endpoint_url
12376

@@ -167,7 +120,7 @@ def add_params_sign(self, method, url, params, sign_key=None):
167120
# for key, value in parse_qsl(urlparse_result.query):
168121
# params += [(key, value)]
169122

170-
params = self.sorted_params(params)
123+
params = UrlUtils.sorted_params(params)
171124

172125
params_without_signature = []
173126
for key, value in params:
@@ -177,7 +130,7 @@ def add_params_sign(self, method, url, params, sign_key=None):
177130
signature = self.generate_oauth_signature(method, params_without_signature, url, sign_key)
178131
params = params_without_signature + [("oauth_signature", signature)]
179132

180-
query_string = self.flatten_params(params)
133+
query_string = UrlUtils.flatten_params(params)
181134

182135
return UrlUtils.substitute_query(url, query_string)
183136

@@ -198,7 +151,7 @@ def get_auth_url(self, endpoint_url, method):
198151
@classmethod
199152
def get_signature_base_string(cls, method, params, url):
200153
base_request_uri = quote(UrlUtils.substitute_query(url), "")
201-
query_string = quote( cls.flatten_params(params), '~')
154+
query_string = quote( UrlUtils.flatten_params(params), '~')
202155
return "&".join([method, base_request_uri, query_string])
203156

204157
def generate_oauth_signature(self, method, params, url, key=None):

wordpress/helpers.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def remove_head(cls, string, head):
3636
string = string[len(head):]
3737
return string
3838

39-
4039
@classmethod
4140
def decapitate(cls, *args, **kwargs):
4241
return cls.remove_head(*args, **kwargs)
4342

43+
4444
class SeqUtils(object):
4545
@classmethod
4646
def filter_true(cls, seq):
@@ -144,8 +144,6 @@ def is_ssl(cls, url):
144144
def join_components(cls, components):
145145
return reduce(posixpath.join, SeqUtils.filter_true(components))
146146

147-
# TODO: move flatten_params, sorted_params, normalize_params out of auth into here
148-
149147
@staticmethod
150148
def get_value_like_as_php(val):
151149
""" Prepare value for quote """
@@ -184,3 +182,48 @@ def remove_port(cls, url):
184182
query=urlparse_result.query,
185183
fragment=urlparse_result.fragment
186184
))
185+
186+
@classmethod
187+
def normalize_str(cls, string):
188+
""" Normalize string for the purposes of url query parameters. """
189+
return quote(string, '')
190+
191+
@classmethod
192+
def normalize_params(cls, params):
193+
""" Normalize parameters. works with RFC 5849 logic. params is a list of key, value pairs """
194+
if isinstance(params, dict):
195+
params = params.items()
196+
params = \
197+
[(cls.normalize_str(key), cls.normalize_str(UrlUtils.get_value_like_as_php(value))) \
198+
for key, value in params]
199+
200+
response = params
201+
return response
202+
203+
@classmethod
204+
def sorted_params(cls, params):
205+
""" Sort parameters. works with RFC 5849 logic. params is a list of key, value pairs """
206+
207+
if isinstance(params, dict):
208+
params = params.items()
209+
210+
# return sorted(params)
211+
ordered = []
212+
base_keys = sorted(set(k.split('[')[0] for k, v in params))
213+
keys_seen = []
214+
for base in base_keys:
215+
for key, value in params:
216+
if key == base or key.startswith(base + '['):
217+
if key not in keys_seen:
218+
ordered.append((key, value))
219+
keys_seen.append(key)
220+
221+
return ordered
222+
223+
@classmethod
224+
def flatten_params(cls, params):
225+
if isinstance(params, dict):
226+
params = params.items()
227+
params = cls.normalize_params(params)
228+
params = cls.sorted_params(params)
229+
return "&".join(["%s=%s"%(key, value) for key, value in params])

0 commit comments

Comments
 (0)
0