Skip to content

Commit 25ce9f7

Browse files
authored
Merge pull request #6 from cloudblue/feedback_20201012
Feedback Oct. 12
2 parents 36dd1cd + ad6c8de commit 25ce9f7

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

cnctcli/api/products.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import click
77
import requests
88

9-
109
from cnctcli.api.utils import (
1110
format_http_status,
1211
get_headers,
1312
handle_http_error,
13+
parse_content_range,
1414
)
1515

1616

@@ -53,12 +53,13 @@ def get_products(endpoint, api_key, query, limit, offset):
5353
if query:
5454
url = f'{url}?{query}'
5555
res = requests.get(
56-
f'{endpoint}/products',
56+
url,
5757
params={'limit': limit, 'offset': offset},
5858
headers=get_headers(api_key),
5959
)
6060
if res.status_code == 200:
61-
return res.json()
61+
content_range = res.headers.get('Content-Range')
62+
return res.json(), parse_content_range(content_range)
6263

6364
handle_http_error(res)
6465

cnctcli/api/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import platform
2+
from collections import namedtuple
23
from http import HTTPStatus
34

45
import click
56

67
from cnctcli import get_version
78

89

10+
ContentRange = namedtuple('ContentRange', ('first', 'last', 'count'))
11+
12+
913
def _get_user_agent():
1014
version = get_version()
1115
pimpl = platform.python_implementation()
@@ -41,3 +45,12 @@ def handle_http_error(res):
4145
raise click.ClickException(f'{status}: {code} - {message}')
4246

4347
raise click.ClickException(f'{status}: unexpected error.')
48+
49+
50+
def parse_content_range(value):
51+
if not value:
52+
return
53+
_, info = value.split()
54+
first_last, count = info.split('/')
55+
first, last = first_last.split('-')
56+
return ContentRange(int(first), int(last), int(count))

cnctcli/commands/product.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def cmd_list_products(config, query, page_size, always_continue):
6363
has_more = True
6464

6565
while has_more:
66-
products = get_products(
66+
products, pagination = get_products(
6767
config.active.endpoint,
6868
config.active.api_key,
6969
query,
@@ -77,11 +77,16 @@ def cmd_list_products(config, query, page_size, always_continue):
7777
click.echo(
7878
f"{prod['id']} - {prod['name']}"
7979
)
80-
if not always_continue:
81-
if not continue_or_quit():
82-
return
80+
if pagination:
81+
has_more = pagination.last < pagination.count - 1
82+
else:
83+
has_more = len(products) == page_size
84+
85+
if has_more:
86+
if not always_continue:
87+
if not continue_or_quit():
88+
return
8389

84-
has_more = len(products) == page_size
8590
offset += page_size
8691

8792

requirements/dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
click==7.1.2
22
openpyxl>=2.5.14
33
setuptools-scm==4.1.2
4-
tqdm==4.48.2
4+
tqdm==4.48.2
5+
requests==2.21.0

0 commit comments

Comments
 (0)