|
21 | 21 |
|
22 | 22 | import os |
23 | 23 | from pathlib import Path |
24 | | -from typing import Dict, List, cast |
| 24 | +from typing import Dict, List, Tuple, cast |
25 | 25 |
|
26 | 26 | import click |
27 | 27 |
|
@@ -62,47 +62,52 @@ def search(click_context, local): |
62 | 62 |
|
63 | 63 | @search.command() |
64 | 64 | @click.option("--query", default="", help="Query string to search Connections by name.") |
| 65 | +@click.option("--page", type=int, default=1, help="Page number to display.") |
65 | 66 | @pass_ctx |
66 | | -def connections(ctx: Context, query): |
| 67 | +def connections(ctx: Context, query, page): |
67 | 68 | """Search for Connections.""" |
68 | 69 | item_type = "connection" |
69 | | - _output_search_results(item_type, search_items(ctx, item_type, query)) |
| 70 | + _output_search_results(item_type, *search_items(ctx, item_type, query, page), page) |
70 | 71 |
|
71 | 72 |
|
72 | 73 | @search.command() |
73 | 74 | @click.option("--query", default="", help="Query string to search Contracts by name.") |
| 75 | +@click.option("--page", type=int, default=1, help="Page number to display.") |
74 | 76 | @pass_ctx |
75 | | -def contracts(ctx: Context, query): |
| 77 | +def contracts(ctx: Context, query, page): |
76 | 78 | """Search for Contracts.""" |
77 | 79 | item_type = "contract" |
78 | | - _output_search_results(item_type, search_items(ctx, item_type, query)) |
| 80 | + _output_search_results(item_type, *search_items(ctx, item_type, query, page), page) |
79 | 81 |
|
80 | 82 |
|
81 | 83 | @search.command() |
82 | 84 | @click.option("--query", default="", help="Query string to search Protocols by name.") |
| 85 | +@click.option("--page", type=int, default=1, help="Page number to display.") |
83 | 86 | @pass_ctx |
84 | | -def protocols(ctx: Context, query): |
| 87 | +def protocols(ctx: Context, query, page): |
85 | 88 | """Search for Protocols.""" |
86 | 89 | item_type = "protocol" |
87 | | - _output_search_results(item_type, search_items(ctx, item_type, query)) |
| 90 | + _output_search_results(item_type, *search_items(ctx, item_type, query, page), page) |
88 | 91 |
|
89 | 92 |
|
90 | 93 | @search.command() |
91 | 94 | @click.option("--query", default="", help="Query string to search Skills by name.") |
| 95 | +@click.option("--page", type=int, default=1, help="Page number to display.") |
92 | 96 | @pass_ctx |
93 | | -def skills(ctx: Context, query): |
| 97 | +def skills(ctx: Context, query, page): |
94 | 98 | """Search for Skills.""" |
95 | 99 | item_type = "skill" |
96 | | - _output_search_results(item_type, search_items(ctx, item_type, query)) |
| 100 | + _output_search_results(item_type, *search_items(ctx, item_type, query, page), page) |
97 | 101 |
|
98 | 102 |
|
99 | 103 | @search.command() |
100 | 104 | @click.option("--query", default="", help="Query string to search Agents by name.") |
| 105 | +@click.option("--page", type=int, default=1, help="Page number to display.") |
101 | 106 | @pass_ctx |
102 | | -def agents(ctx: Context, query): |
| 107 | +def agents(ctx: Context, query, page): |
103 | 108 | """Search for Agents.""" |
104 | 109 | item_type = "agent" |
105 | | - _output_search_results(item_type, search_items(ctx, item_type, query)) |
| 110 | + _output_search_results(item_type, *search_items(ctx, item_type, query, page), page) |
106 | 111 |
|
107 | 112 |
|
108 | 113 | def setup_search_ctx(ctx: Context, local: bool) -> None: |
@@ -198,33 +203,55 @@ def _search_items_locally(ctx, item_type_plural): |
198 | 203 | return sorted(result, key=lambda k: k["name"]) |
199 | 204 |
|
200 | 205 |
|
201 | | -def search_items(ctx: Context, item_type: str, query: str) -> List: |
| 206 | +def search_items( |
| 207 | + ctx: Context, item_type: str, query: str, page: int |
| 208 | +) -> Tuple[List, int]: |
202 | 209 | """ |
203 | 210 | Search items by query and click.echo results. |
204 | 211 |
|
205 | 212 | :param ctx: Context object. |
206 | 213 | :param item_type: item type. |
207 | 214 | :param query: query string. |
208 | 215 |
|
209 | | - :return: None |
| 216 | + :return: (List of items, int items total count). |
210 | 217 | """ |
211 | 218 | click.echo('Searching for "{}"...'.format(query)) |
212 | 219 | item_type_plural = item_type + "s" |
213 | 220 | if ctx.config.get("is_local"): |
214 | | - return _search_items_locally(ctx, item_type_plural) |
215 | | - return request_api("GET", "/{}".format(item_type_plural), params={"search": query}) |
| 221 | + results = _search_items_locally(ctx, item_type_plural) |
| 222 | + count = len(results) |
| 223 | + else: |
| 224 | + resp = request_api( |
| 225 | + "GET", |
| 226 | + "/{}".format(item_type_plural), |
| 227 | + params={"search": query, "page": page}, |
| 228 | + ) |
| 229 | + results = resp["results"] |
| 230 | + count = resp["count"] |
| 231 | + return results, count |
216 | 232 |
|
217 | 233 |
|
218 | | -def _output_search_results(item_type: str, results: List[Dict]) -> None: |
| 234 | +def _output_search_results( |
| 235 | + item_type: str, results: List[Dict], count: int, page: int |
| 236 | +) -> None: |
219 | 237 | """ |
220 | 238 | Output search results. |
221 | 239 |
|
222 | | - :param results: list of found items |
| 240 | + :param item_type: str item type. |
| 241 | + :param results: list of found items. |
| 242 | + :param count: items total count. |
223 | 243 |
|
224 | 244 | """ |
225 | 245 | item_type_plural = item_type + "s" |
226 | | - if len(results) == 0: |
| 246 | + len_results = len(results) |
| 247 | + if len_results == 0: |
227 | 248 | click.echo("No {} found.".format(item_type_plural)) # pragma: no cover |
228 | 249 | else: |
229 | 250 | click.echo("{} found:\n".format(item_type_plural.title())) |
230 | 251 | click.echo(format_items(results)) |
| 252 | + if count > len_results: |
| 253 | + click.echo( |
| 254 | + "{} {} out of {}.\nPage {}".format( |
| 255 | + len_results, item_type_plural, count, page |
| 256 | + ) |
| 257 | + ) # pragma: no cover |
0 commit comments