Skip to content

Commit

Permalink
Merge pull request #87 from gadventures/uuid
Browse files Browse the repository at this point in the history
Add ability to include uuid as part of each request
  • Loading branch information
bartek committed Dec 14, 2017
2 parents b4b808d + 3306018 commit 52b00ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gapipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'number': os.environ.get('GAPI_CLIENT_CONNECTION_POOL_NUMBER', 10),
'maxsize': os.environ.get('GAPI_CLIENT_CONNECTION_POOL_MAXSIZE', 10),
},
'uuid': os.environ.get('GAPI_UUID', False),
}


Expand All @@ -48,6 +49,7 @@ def __init__(self, **config):
self.api_proxy = get_config(config, 'api_proxy')
self.api_language = get_config(config, 'api_language')
self.cache_backend = get_config(config, 'cache_backend')
self.uuid = get_config(config, 'uuid')

# begin with default connection pool options and overwrite any that the
# client has specified
Expand Down
5 changes: 5 additions & 0 deletions gapipy/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import sys
from uuid import uuid1

from . import __title__, __version__

Expand Down Expand Up @@ -27,6 +28,10 @@ def _request(self, uri, method, data=None, params=None, additional_headers=None)
assert method in ALLOWED_METHODS, "Only {} are allowed.".format(', '.join(ALLOWED_METHODS))
url = self._get_url(uri)
headers = self._get_headers(method, additional_headers)
if self.client.uuid:
if not params:
params = {}
params['uuid'] = str(uuid1())
response = self._make_call(method, url, headers, data, params)
return response

Expand Down
34 changes: 32 additions & 2 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ def __init__(self, **kwArgs):
self.__dict__.update(kwArgs)


@patch('gapipy.request.APIRequestor._request')
class APIRequestorTestCase(unittest.TestCase):

def setUp(self):
self.client = Client()
self.resources = Resources(_resource_name='resources', _uri=None)

@patch('gapipy.request.APIRequestor._request')
def test_get_resource_by_id(self, mock_request):
requestor = APIRequestor(self.client, self.resources)
requestor.get(1234)
mock_request.assert_called_once_with('/resources/1234', 'GET')

@patch('gapipy.request.APIRequestor._request')
def test_get_with_null_resource_id_and_uri_raises_error(self, mock_request):
requestor = APIRequestor(self.client, self.resources)
error_msg = 'Need to provide at least one of `resource_id` or `uri` as argument'
Expand All @@ -36,16 +37,19 @@ def test_get_with_null_resource_id_and_uri_raises_error(self, mock_request):
with self.assertRaisesRegex(ValueError, error_msg):
requestor.get()

@patch('gapipy.request.APIRequestor._request')
def test_get_with_falsy_resource_id_does_not_raise_error(self, mock_request):
requestor = APIRequestor(self.client, self.resources)
requestor.get(0)
mock_request.assert_called_once_with('/resources/0', 'GET')

@patch('gapipy.request.APIRequestor._request')
def test_list_resource(self, mock_request):
requestor = APIRequestor(self.client, self.resources)
requestor.list_raw()
mock_request.assert_called_once_with('/resources', 'GET', params=None)

@patch('gapipy.request.APIRequestor._request')
def test_list_resource_with_parent(self, mock_request):
parent = ('parent', '1234', None)
requestor = APIRequestor(self.client, self.resources, parent=parent)
Expand All @@ -54,7 +58,7 @@ def test_list_resource_with_parent(self, mock_request):
'/parent/1234/resources', 'GET', params=None)

@patch('gapipy.request.APIRequestor.list_raw')
def test_list_generator(self, mock_list, mock_request):
def test_list_generator(self, mock_list):
mock_list.side_effect = [FIRST_PAGE_LIST_DATA, SECOND_PAGE_LIST_DATA]
expected_calls = [
call(None),
Expand All @@ -66,3 +70,29 @@ def test_list_generator(self, mock_list, mock_request):

self.assertEqual(mock_list.mock_calls, expected_calls)
self.assertEqual(len(resources), 6)

@patch('gapipy.request.APIRequestor._request')
def test_uuid_not_set(self, mock_request):
self.client.uuid = False
requestor = APIRequestor(self.client, self.resources)
requestor.params = {'test': '1234'}
requestor.list_raw()
mock_request.assert_called_once_with('/resources', 'GET', params={'test': '1234'})

@patch('gapipy.request.APIRequestor._make_call')
def test_uuid_set(self, mock_make_call):
self.client.uuid = True
requestor = APIRequestor(self.client, self.resources)
requestor.list_raw()
params_arg = mock_make_call.call_args[0][-1]
self.assertTrue('uuid' in params_arg)

@patch('gapipy.request.APIRequestor._make_call')
def test_uuid_with_other_params(self, mock_make_call):
self.client.uuid = True
requestor = APIRequestor(self.client, self.resources)
requestor.params = {'test': '1234'}
requestor.list_raw()
params_arg = mock_make_call.call_args[0][-1]
self.assertEqual(params_arg['test'], '1234')
self.assertTrue('uuid' in params_arg)

0 comments on commit 52b00ce

Please sign in to comment.