Skip to content

Commit

Permalink
Merge pull request #136 from mfinkle/master
Browse files Browse the repository at this point in the history
Add support for HTTP DELETE in API requests
  • Loading branch information
komapa authored Jul 9, 2019
2 parents 4a5cd7c + cb21cd6 commit 551dfb3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pytumblr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,5 +559,7 @@ def send_api_request(self, method, url, params={}, valid_parameters=[], needs_ap
validate_params(valid_parameters, params)
if method == "get":
return self.request.get(url, params)
elif method == "delete":
return self.request.delete(url, params)
else:
return self.request.post(url, params, files)
20 changes: 20 additions & 0 deletions pytumblr/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ def post(self, url, params={}, files=[]):
except HTTPError as e:
return self.json_parse(e.response)

def delete(self, url, params):
"""
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
: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)
except TooManyRedirects as e:
resp = e.response

return self.json_parse(resp)

def json_parse(self, response):
"""
Wraps and abstracts response validation and JSON parsing
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pytumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ def test_create_video(self, mock_post):
response = self.client.create_video('codingjester.tumblr.com', embed="blahblahembed")
assert response == []

@mock.patch('requests.delete')
def test_api_delete(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)
assert response == []


if __name__ == "__main__":
unittest.main()

0 comments on commit 551dfb3

Please sign in to comment.