diff --git a/pytumblr/__init__.py b/pytumblr/__init__.py index d503a79..cdff0ea 100644 --- a/pytumblr/__init__.py +++ b/pytumblr/__init__.py @@ -548,7 +548,7 @@ def _send_post(self, blogname, params): return self.send_api_request("post", url, params, valid_options) - def send_api_request(self, method, url, params={}, valid_parameters=[], needs_api_key=False): + def send_api_request(self, method, url, params={}, valid_parameters=[], needs_api_key=False, use_body=False): """ Sends the url with parameters to the requested url, validating them to make sure that they are what we expect to have passed to us @@ -557,6 +557,7 @@ def send_api_request(self, method, url, params={}, valid_parameters=[], needs_ap :param params: a dict, the parameters used for the API request :param valid_parameters: a list, the list of valid parameters :param needs_api_key: a boolean, whether or not your request needs an api key injected + :param use_body: a boolean, put params in request body rather than the URL :returns: a dict parsed from the JSON response """ @@ -577,6 +578,6 @@ def send_api_request(self, method, url, params={}, valid_parameters=[], needs_ap if method == "get": return self.request.get(url, params) elif method == "delete": - return self.request.delete(url, params) + return self.request.delete(url, params, use_body) else: return self.request.post(url, params, files) diff --git a/pytumblr/request.py b/pytumblr/request.py index fe2cf26..6998e75 100644 --- a/pytumblr/request.py +++ b/pytumblr/request.py @@ -76,23 +76,33 @@ def post(self, url, params={}, files=[]): except HTTPError as e: return self.json_parse(e.response) - def delete(self, url, params): + def delete(self, url, params, use_body=False): """ Issues a DELETE request against the API, properly formatting the params :param url: a string, the url you are requesting :param params: a dict, the key-value of all the paramaters needed in the request + :param use_body: a boolean, put params in request body rather than the URL + :returns: a dict parsed of the JSON response """ url = self.host + url - if params: - url = url + "?" + urllib.parse.urlencode(params) try: - resp = requests.delete(url, allow_redirects=False, headers=self.headers, auth=self.oauth) + if use_body: + data = urllib.parse.urlencode(params) + if not PY3: + data = str(data) + resp = requests.delete(url, data=data, headers=self.headers, auth=self.oauth) + else: + if params: + url = url + "?" + urllib.parse.urlencode(params) + resp = requests.delete(url, allow_redirects=False, headers=self.headers, auth=self.oauth) except TooManyRedirects as e: resp = e.response + except HTTPError as e: + resp = e.response return self.json_parse(resp) diff --git a/tests/test_pytumblr.py b/tests/test_pytumblr.py index aa38556..b316169 100644 --- a/tests/test_pytumblr.py +++ b/tests/test_pytumblr.py @@ -328,6 +328,14 @@ def test_api_delete(self, mock_delete): response = self.client.send_api_request('delete', api_url, {'param1': 'foo', 'param2': 'bar'}, ['param1', 'param2'], False) assert response == [] + @mock.patch('requests.delete') + def test_api_delete_body(self, mock_delete): + mock_delete.side_effect = wrap_response('{"meta": {"status": 200, "msg": "OK"}, "response": []}') + + api_url = '/v2/some/api' + response = self.client.send_api_request('delete', api_url, {'param1': 'foo', 'param2': 'bar'}, ['param1', 'param2'], False, use_body=True) + assert response == [] + if __name__ == "__main__": unittest.main()