Skip to content

Commit

Permalink
Merge pull request #133 from Kinto/126-specify-retry-cli-args
Browse files Browse the repository at this point in the history
Add --retry and --retry-after to CLI utils helpers (fixes #126)
  • Loading branch information
Natim committed May 10, 2017
2 parents 71006cc + 08c981d commit 6f06e31
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ This document describes changes between each past release.
7.3.0 (unreleased)
==================

**New Feature**
**New Features**

- Keep tracks of Backoff headers and raise an ``BackoffException`` if
we are not waiting enough between two calls. (#53)
- Add ``--retry`` and ``--retry-after`` to CLI utils helpers (fixes #126)

**Bug fixes**

Expand Down
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ If no specific bucket name is provided, the "default" bucket is used.
# To create a bucket.
client.create_bucket('payments')
# To get an existing bucket
# To get an existing bucket
bucket = client.get_bucket('payments')
# Or retrieve all readable buckets.
Expand All @@ -163,7 +163,7 @@ If no specific bucket name is provided, the "default" bucket is used.
Groups
------

A group associates a name to a list of principals. It is useful in order to handle permissions.
A group associates a name to a list of principals. It is useful in order to handle permissions.

.. code-block:: python
Expand Down Expand Up @@ -436,11 +436,16 @@ The script now accepts basic options:
Bucket name.
-c COLLECTION, --collection COLLECTION
Collection name.
--retry RETRY Number of retries when a request fails
--retry-after RETRY_AFTER
Delay in seconds between retries when requests fail
(default: provided by server)
-v, --verbose Show all messages.
-q, --quiet Show only critical errors.
-D, --debug Show all messages, including debug messages.



Run tests
=========

Expand Down
15 changes: 14 additions & 1 deletion kinto_http/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def create_client_from_args(args):
return Client(server_url=args.server,
auth=args.auth,
bucket=getattr(args, 'bucket', None),
collection=getattr(args, 'collection', None))
collection=getattr(args, 'collection', None),
retry=args.retry,
retry_after=args.retry_after)


class AuthAction(argparse.Action):
Expand All @@ -35,6 +37,8 @@ def __call__(self, parser, namespace, values, option_string=None):
def add_parser_options(parser=None,
default_server=None,
default_auth=None,
default_retry=0,
default_retry_after=None,
default_bucket=None,
default_collection=None,
include_bucket=True,
Expand Down Expand Up @@ -62,6 +66,15 @@ def add_parser_options(parser=None,
help='Collection name.',
type=str, default=default_collection)

parser.add_argument('--retry',
help='Number of retries when a request fails',
type=int, default=default_retry)

parser.add_argument('--retry-after',
help='Delay in seconds between retries when requests fail. '
'(default: provided by server)',
type=int, default=default_retry_after)

# Defaults
parser.add_argument('-v', '--verbose', action='store_const',
const=logging.INFO, dest='verbosity',
Expand Down
42 changes: 36 additions & 6 deletions kinto_http/tests/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
['-a', '--auth'],
['-b', '--bucket'],
['-c', '--collection'],
['--retry'],
['--retry-after'],
['-v', '--verbose'],
['-q', '--quiet'],
['-D', '--debug'],
Expand All @@ -26,7 +28,7 @@ def assert_option_strings(self, parser, *option_strings_list):
def test_add_parser_options_create_a_parser_if_needed(self):
parser = cli_utils.add_parser_options()
self.assert_option_strings(parser, *ALL_PARAMETERS)
assert len(parser._actions) == 8
assert len(parser._actions) == 10

def test_add_parser_options_adds_arguments_on_existing_parser(self):
parser = argparse.ArgumentParser(prog="importer")
Expand All @@ -36,7 +38,7 @@ def test_add_parser_options_adds_arguments_on_existing_parser(self):
parser = cli_utils.add_parser_options(parser)
self.assert_option_strings(parser, ['-t', '--type'],
*ALL_PARAMETERS)
assert len(parser._actions) == 9
assert len(parser._actions) == 11

def test_add_parser_options_can_ignore_bucket_and_collection(self):
parser = cli_utils.add_parser_options(
Expand All @@ -45,12 +47,14 @@ def test_add_parser_options_can_ignore_bucket_and_collection(self):
['-h', '--help'],
['-s', '--server'],
['-a', '--auth'],
['--retry'],
['--retry-after'],
['-v', '--verbose'],
['-q', '--quiet'],
['-D', '--debug'],
]
self.assert_option_strings(parser, *parameters)
assert len(parser._actions) == 6
assert len(parser._actions) == 8

def test_can_change_default_values(self):
parser = cli_utils.add_parser_options(
Expand All @@ -67,6 +71,8 @@ def test_can_change_default_values(self):
'auth': 'user:password',
'bucket': 'blocklists',
'collection': 'certificates',
'retry': 0,
'retry_after': None,
'verbosity': None
}

Expand Down Expand Up @@ -96,7 +102,7 @@ def test_get_auth_is_called_by_argparse(self):
class ClientFromArgsTest(unittest.TestCase):

@mock.patch('kinto_http.cli_utils.Client')
def test_create_client_from_args_build_a_client(self, mocked_client):
def test_create_client_from_default_args_build_a_client(self, mocked_client):
parser = cli_utils.add_parser_options(
default_server="https://firefox.settings.services.mozilla.com/",
default_bucket="blocklists",
Expand All @@ -111,7 +117,29 @@ def test_create_client_from_args_build_a_client(self, mocked_client):
server_url='https://firefox.settings.services.mozilla.com/',
auth=('user', 'password'),
bucket='blocklists',
collection='certificates')
collection='certificates',
retry=0,
retry_after=None)

@mock.patch('kinto_http.cli_utils.Client')
def test_create_client_from_args_build_a_client(self, mocked_client):
parser = cli_utils.add_parser_options(
default_server="https://firefox.settings.services.mozilla.com/",
)

args = parser.parse_args(['--auth', 'user:password',
'--bucket', 'blocklists',
'--collection', 'certificates',
'--retry', '3'])

cli_utils.create_client_from_args(args)
mocked_client.assert_called_with(
server_url='https://firefox.settings.services.mozilla.com/',
auth=('user', 'password'),
bucket='blocklists',
collection='certificates',
retry=3,
retry_after=None)

@mock.patch('kinto_http.cli_utils.Client')
def test_create_client_from_args_default_bucket_and_collection_to_none(
Expand All @@ -129,4 +157,6 @@ def test_create_client_from_args_default_bucket_and_collection_to_none(
server_url='https://firefox.settings.services.mozilla.com/',
auth=('user', 'password'),
bucket=None,
collection=None)
collection=None,
retry=0,
retry_after=None)

0 comments on commit 6f06e31

Please sign in to comment.