Skip to content

Commit

Permalink
Merge pull request #29 from soda480/0.3.4
Browse files Browse the repository at this point in the history
Add Head method
  • Loading branch information
soda480 authored Apr 27, 2021
2 parents 309722b + d8d483c commit 40d24e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ The examples below show how RESTclient can be used to consume the GitHub REST AP
>>> client.delete('/repos/soda480/test-repo1')
```

`HEAD` request
```python
>>> response = client.head('/user/repos', raw_response=True)
>>> response.headers
```

#### Retries
Add support for retry using the `retrying` library: https://pypi.org/project/retrying/

Expand Down
9 changes: 8 additions & 1 deletion src/main/python/rest3client/restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import base64
import requests
from collections.abc import Iterable
from functools import wraps

from rest3client.ssladapter import SSLAdapter
from retrying import retry
Expand Down Expand Up @@ -178,6 +179,7 @@ def get_response(self, response, **kwargs):
def request_handler(function):
""" decorator to process arguments and response for request method
"""
@wraps(function)
def _request_handler(self, endpoint, **kwargs):
""" decorator method to prepare and handle requests and responses
"""
Expand All @@ -188,7 +190,6 @@ def _request_handler(self, endpoint, **kwargs):
return
response = function(self, endpoint, **arguments)
return self.get_response(response, **kwargs)

return _request_handler

@request_handler
Expand Down Expand Up @@ -221,6 +222,12 @@ def patch(self, endpoint, **kwargs):
"""
return self.session.request('patch', kwargs.pop('address'), **kwargs)

@request_handler
def head(self, endpoint, **kwargs):
""" helper method to submit head requests
"""
return self.session.request('head', kwargs.pop('address'), **kwargs)

def get_retry_methods(self):
""" return list of retry_ methods found in self
"""
Expand Down
13 changes: 13 additions & 0 deletions src/unittest/python/test_RESTclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ def test__patch_Should_CallRequestsPatch_When_Called(self, *patches):
verify=client.cabundle)
self.assertTrue(requests_patch_call in client.session.request.mock_calls)

@patch('rest3client.restclient.os.access')
@patch('rest3client.restclient.requests.Session')
def test__head_Should_CallRequestsHead_When_Called(self, *patches):
client = RESTclient('api.name.com')
client.head('/rest/endpoint')
requests_head_call = call(
'head',
'https://api.name.com/rest/endpoint',
headers={
'Content-Type': 'application/json'},
verify=client.cabundle)
self.assertTrue(requests_head_call in client.session.request.mock_calls)

@patch('rest3client.restclient.os.access')
@patch('rest3client.restclient.requests.Session')
def test__delete_Should_CallRequestsDelete_When_Called(self, *patches):
Expand Down

0 comments on commit 40d24e4

Please sign in to comment.