Skip to content

Commit

Permalink
Merge pull request #6 from cloudblue/feedback_20201012
Browse files Browse the repository at this point in the history
Feedback Oct. 12
  • Loading branch information
marcserrat authored Oct 13, 2020
2 parents 36dd1cd + ad6c8de commit 25ce9f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
7 changes: 4 additions & 3 deletions cnctcli/api/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import click
import requests


from cnctcli.api.utils import (
format_http_status,
get_headers,
handle_http_error,
parse_content_range,
)


Expand Down Expand Up @@ -53,12 +53,13 @@ def get_products(endpoint, api_key, query, limit, offset):
if query:
url = f'{url}?{query}'
res = requests.get(
f'{endpoint}/products',
url,
params={'limit': limit, 'offset': offset},
headers=get_headers(api_key),
)
if res.status_code == 200:
return res.json()
content_range = res.headers.get('Content-Range')
return res.json(), parse_content_range(content_range)

handle_http_error(res)

Expand Down
13 changes: 13 additions & 0 deletions cnctcli/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import platform
from collections import namedtuple
from http import HTTPStatus

import click

from cnctcli import get_version


ContentRange = namedtuple('ContentRange', ('first', 'last', 'count'))


def _get_user_agent():
version = get_version()
pimpl = platform.python_implementation()
Expand Down Expand Up @@ -41,3 +45,12 @@ def handle_http_error(res):
raise click.ClickException(f'{status}: {code} - {message}')

raise click.ClickException(f'{status}: unexpected error.')


def parse_content_range(value):
if not value:
return
_, info = value.split()
first_last, count = info.split('/')
first, last = first_last.split('-')
return ContentRange(int(first), int(last), int(count))
15 changes: 10 additions & 5 deletions cnctcli/commands/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def cmd_list_products(config, query, page_size, always_continue):
has_more = True

while has_more:
products = get_products(
products, pagination = get_products(
config.active.endpoint,
config.active.api_key,
query,
Expand All @@ -77,11 +77,16 @@ def cmd_list_products(config, query, page_size, always_continue):
click.echo(
f"{prod['id']} - {prod['name']}"
)
if not always_continue:
if not continue_or_quit():
return
if pagination:
has_more = pagination.last < pagination.count - 1
else:
has_more = len(products) == page_size

if has_more:
if not always_continue:
if not continue_or_quit():
return

has_more = len(products) == page_size
offset += page_size


Expand Down
3 changes: 2 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
click==7.1.2
openpyxl>=2.5.14
setuptools-scm==4.1.2
tqdm==4.48.2
tqdm==4.48.2
requests==2.21.0

0 comments on commit 25ce9f7

Please sign in to comment.