Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for format flags not working on single endpoints #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def fixed_url_fix(s, charset='utf-8'):
@pytest.fixture(scope='session')
def endpoints():
httpretty.enable()
with open(os.path.join(TESTS_DIR, 'endpoints.json')) as resource:
with open(os.path.join(TESTS_DIR, 'endpoints.json'), encoding="utf-8") as resource:
test_data = json.load(resource)

endpoints = test_data['endpoints']
Expand Down
12 changes: 12 additions & 0 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from requests.exceptions import HTTPError

from tortilla.utils import Bunch, bunchify, run_from_ipython
from tortilla.formatters import hyphenate


def time_function(fn, *args, **kwargs):
Expand All @@ -17,6 +18,17 @@ def time_function(fn, *args, **kwargs):
return t2 - t1


def test_call_formatter(api):
endpoint = 'hyphenated_endpoint'
r = api(endpoint, hyphenate=True)
assert r._parent.config.formatter == hyphenate
assert r._part == hyphenate(endpoint)

r = api.hyphenated_endpoint(hyphenate=True)
assert r._parent.config.formatter == hyphenate
assert r._part == hyphenate(endpoint)


def test_json_response(api, endpoints):
assert api.user.get('jimmy') == endpoints['/user/jimmy']['body']
assert api.user.get('имя') == endpoints['/user/имя']['body']
Expand Down
16 changes: 13 additions & 3 deletions tortilla/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ def __call__(self, *parts, **options):
:param options: (optional) Arguments accepted by the
:class:`Wrap` initializer
"""

# check if a formatter is given for the url endpoint
if options.get('formatter') is None:
if options.get('hyphenate'):
options['formatter'] = formatters.hyphenate
elif options.get('mixedcase'):
options['formatter'] = formatters.mixedcase
elif options.get('camelcase'):
options['formatter'] = formatters.camelcase

self.config.update(**options)

if len(parts) == 0:
Expand All @@ -351,12 +361,12 @@ def __getattr__(self, part):
if part in self.__dict__:
return self.__dict__[part]

if self.config.formatter:
part = self.config.formatter(part)

return self._get_or_create_child_wrap(part)

def _get_or_create_child_wrap(self, name):
if self.config.formatter:
name = self.config.formatter(name)

if name not in self._children:
self._children[name] = self.__class__(
part=name,
Expand Down