8000 Merge pull request #12 from december1981/master · progressify/wp-api-python@bfeb983 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfeb983

Browse files
author
derwentx
authored
Merge pull request d3v-null#12 from december1981/master
Allow a wordpress api request to specify certain status codes it want…
2 parents dd4374c + 1326d89 commit bfeb983

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

tests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,24 @@ def test_APIPostBadData(self):
438438
with self.assertRaises(UserWarning):
439439
self.wpapi.post('posts', data)
440440

441+
def test_APIPostBadDataHandleBadStatus(self):
442+
"""
443+
Test handling explicitly a bad status code for a request.
444+
"""
445+
nonce = "%f\u00ae" % random.random()
446+
447+
data = {
448+
'a': nonce
449+
}
450+
451+
response = self.wpapi.post('posts', data, handle_status_codes=[400])
452+
self.assertEqual(response.status_code, 400)
453+
454+
# If we don't specify a correct status code to handle we should
455+
# still expect an exception
456+
with self.assertRaises(UserWarning):
457+
self.wpapi.post('posts', data, handle_status_codes=[404])
458+
441459
def test_APIPostMedia(self):
442460
img_path = 'tests/data/test.jpg'
443461
with open(img_path, 'rb') as test_file:

wordpress/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ def __request(self, method, endpoint, data, **kwargs):
219219
# enforce utf-8 encoded binary
220220
data = StrUtils.to_binary(data)
221221

222+
handle_status_codes = kwargs.pop('handle_status_codes', [])
223+
222224
response = self.requester.request(
223225
method=method,
224226
url=endpoint_url,
@@ -227,7 +229,7 @@ def __request(self, method, endpoint, data, **kwargs):
227229
**kwargs
228230
)
229231

230-
if response.status_code not in [200, 201, 202]:
232+
if response.status_code not in [200, 201, 202] + handle_status_codes:
231233
self.request_post_mortem(response)
232234

233235
return response

0 commit comments

Comments
 (0)
0