Skip to content

Commit

Permalink
Passing more kwargs to _get_iterator_list and passing only kwargs to …
Browse files Browse the repository at this point in the history
…all self._api method calls (closes vintasoftware#130)
  • Loading branch information
fjsj committed Nov 8, 2016
1 parent 275090f commit 72975db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
38 changes: 19 additions & 19 deletions tapioca/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,65 @@ def __init__(self, serializer_class=None, *args, **kwargs):
else:
self.serializer = self.get_serializer()

def _get_to_native_method(self, method_name, value):
def _get_to_native_method(self, method_name, value, **kwargs):
if not self.serializer:
raise NotImplementedError("This client does not have a serializer")

def to_native_wrapper(**kwargs):
return self._value_to_native(method_name, value, **kwargs)
return self._value_to_native(method_name=method_name, value=value, **kwargs)

return to_native_wrapper

def _value_to_native(self, method_name, value, **kwargs):
return self.serializer.deserialize(method_name, value, **kwargs)

def get_serializer(self):
def get_serializer(self, **kwargs):
if self.serializer_class:
return self.serializer_class()

def get_api_root(self, api_params):
def get_api_root(self, api_params, **kwargs):
return self.api_root

def fill_resource_template_url(self, template, params):
def fill_resource_template_url(self, template, params, **kwargs):
return template.format(**params)

def get_request_kwargs(self, api_params, *args, **kwargs):
serialized = self.serialize_data(kwargs.get('data'))

kwargs.update({
'data': self.format_data_to_request(serialized),
'data': self.format_data_to_request(data=serialized),
})
return kwargs

def process_response(self, response):

def process_response(self, response, **kwargs):
if 500 <= response.status_code < 600:
raise ResponseProcessException(ServerError, None)

data = self.response_to_native(response)
data = self.response_to_native(response=response)

if 400 <= response.status_code < 500:
raise ResponseProcessException(ClientError, data)

return data

def serialize_data(self, data):
def serialize_data(self, data, **kwargs):
if self.serializer:
return self.serializer.serialize(data)

return data

def format_data_to_request(self, data):
def format_data_to_request(self, data, **kwargs):
raise NotImplementedError()

def response_to_native(self, response):
def response_to_native(self, response, **kwargs):
raise NotImplementedError()

def get_iterator_list(self, response_data):
def get_iterator_list(self, api_params, iterator_request_kwargs,
response_data, response, **kwargs):
raise NotImplementedError()

def get_iterator_next_request_kwargs(self, iterator_request_kwargs,
response_data, response):
def get_iterator_next_request_kwargs(self, api_params, iterator_request_kwargs,
response_data, response, **kwargs):
raise NotImplementedError()

def is_authentication_expired(self, exception, *args, **kwargs):
Expand All @@ -91,10 +91,10 @@ def refresh_authentication(self, api_params, *args, **kwargs):

class FormAdapterMixin(object):

def format_data_to_request(self, data):
def format_data_to_request(self, data, **kwargs):
return data

def response_to_native(self, response):
def response_to_native(self, response, **kwargs):
return {'text': response.text}


Expand All @@ -109,10 +109,10 @@ def get_request_kwargs(self, api_params, *args, **kwargs):
arguments['headers']['Content-Type'] = 'application/json'
return arguments

def format_data_to_request(self, data):
def format_data_to_request(self, data, **kwargs):
if data:
return json.dumps(data)

def response_to_native(self, response):
def response_to_native(self, response, **kwargs):
if response.content.strip():
return response.json()
22 changes: 14 additions & 8 deletions tapioca/tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __call__(self, *args, **kwargs):
url_params = self._api_params.get('default_url_params', {})
url_params.update(kwargs)
if self._resource and url_params:
data = self._api.fill_resource_template_url(self._data, url_params)
data = self._api.fill_resource_template_url(template=self._data, params=url_params)

return self._wrap_in_tapioca_executor(data, resource=self._resource,
response=self._response)
Expand All @@ -103,7 +103,7 @@ def _get_client_from_name(self, name):
resource_mapping = self._api.resource_mapping
if name in resource_mapping:
resource = resource_mapping[name]
api_root = self._api.get_api_root(self._api_params)
api_root = self._api.get_api_root(api_params=self._api_params)

url = api_root.rstrip('/') + '/' + resource['resource'].lstrip('/')
return self._wrap_in_tapioca(url, resource=resource)
Expand Down Expand Up @@ -186,7 +186,7 @@ def __iter__(self):

def __getattr__(self, name):
if name.startswith('to_'): # deserializing
return self._api._get_to_native_method(name, self._data)
return self._api._get_to_native_method(method_name=name, value=self._data)
return self._wrap_in_tapioca_executor(getattr(self._data, name))

def __call__(self, *args, **kwargs):
Expand Down Expand Up @@ -216,16 +216,17 @@ def _make_request(self, request_method, refresh_token=None, *args, **kwargs):
response = requests.request(request_method, **request_kwargs)

try:
data = self._api.process_response(response)
data = self._api.process_response(response=response)
except ResponseProcessException as e:
client = self._wrap_in_tapioca(e.data, response=response,
request_kwargs=request_kwargs)
tapioca_exception = e.tapioca_exception(client=client)

should_refresh_token = (refresh_token is not False and
self._refresh_token_default)
if should_refresh_token and self._api.is_authentication_expired(tapioca_exception):
self._api.refresh_authentication(self._api_params)
if should_refresh_token and self._api.is_authentication_expired(
exception=tapioca_exception):
self._api.refresh_authentication(api_params=self._api_params)
return self._make_request(request_method, *args, **kwargs)
else:
raise tapioca_exception
Expand All @@ -249,11 +250,16 @@ def delete(self, *args, **kwargs):
return self._make_request('DELETE', *args, **kwargs)

def _get_iterator_list(self):
return self._api.get_iterator_list(self._data)
return self._api.get_iterator_list(
api_params=self._api_params, resource=self._resource,
iterator_request_kwargs=self._request_kwargs,
response_data=self._data, response=self._response)

def _get_iterator_next_request_kwargs(self):
return self._api.get_iterator_next_request_kwargs(
self._request_kwargs, self._data, self._response)
api_params=self._api_params, resource=self._resource,
iterator_request_kwargs=self._request_kwargs,
response_data=self._data, response=self._response)

def _reached_max_limits(self, page_count, item_count, max_pages,
max_items):
Expand Down
4 changes: 2 additions & 2 deletions tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class TesterClientAdapter(JSONAdapterMixin, TapiocaAdapter):
api_root = 'https://api.test.com'
resource_mapping = RESOURCE_MAPPING

def get_iterator_list(self, response_data):
def get_iterator_list(self, response_data, **kwargs):
return response_data['data']

def get_iterator_next_request_kwargs(self, iterator_request_kwargs,
response_data, response):
response_data, response, **kwargs):
paging = response_data.get('paging')
if not paging:
return
Expand Down

0 comments on commit 72975db

Please sign in to comment.