Skip to content

Commit

Permalink
fix: improve pagination for asm search
Browse files Browse the repository at this point in the history
  • Loading branch information
grace-murphy committed Sep 6, 2024
1 parent 41be59c commit b4c1a66
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
24 changes: 23 additions & 1 deletion censys/asm/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def search(
cursor: Optional[str] = None,
sort: Optional[List[str]] = None,
fields: Optional[List[str]] = None,
pages: Optional[int] = None,
) -> dict:
"""Search inventory data.
Expand All @@ -28,6 +29,7 @@ def search(
cursor (str, optional): Cursor to start search from.
sort (List[str], optional): List of fields to sort by.
fields (List[str], optional): List of fields to return.
pages (int, optional): Number of pages of results to return (when set to -1 returns all pages available).
Returns:
dict: Inventory search results.
Expand All @@ -43,6 +45,9 @@ def search(
if page_size is None:
page_size = 50

if pages is None:
pages = 1

args = {
"workspaces": workspaces,
"pageSize": page_size,
Expand All @@ -57,7 +62,24 @@ def search(
if fields:
args["fields"] = fields

return self._get(self.base_path, args=args)
page = 0
next_cursor = None
hits = []
resp = self._get(self.base_path, args=args)
next_cursor = resp.get("nextCursor")
hits.extend(resp.get("hits", []))
while next_cursor and (page == 0 or pages == -1 or page < pages):
args["cursor"] = next_cursor
resp = self._get(self.base_path, args=args)
if "nextCursor" in resp:
next_cursor = resp.get("nextCursor")
else:
next_cursor = None
hits.extend(resp.get("hits", []))
page += 1

resp["hits"] = hits
return resp

def aggregate(
self,
Expand Down
27 changes: 25 additions & 2 deletions censys/cli/commands/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ def cli_execute_saved_query_by_name(args: argparse.Namespace):
query = results[0]["query"]

try:
res = s.search(None, query, args.page_size, None, args.sort, args.fields)
res = s.search(
None, query, args.page_size, None, args.sort, args.fields, args.pages
)
console.print_json(json.dumps(res))
except CensysAsmException:
console.print("Failed to execute saved query.")
Expand All @@ -579,7 +581,9 @@ def cli_execute_saved_query_by_id(args: argparse.Namespace):
console.print("No saved query found with that ID.")
sys.exit(1)
try:
res = s.search(None, query, args.page_size, None, args.sort, args.fields)
res = s.search(
None, query, args.page_size, None, args.sort, args.fields, args.pages
)
console.print_json(json.dumps(res))
except CensysAsmException:
console.print("Failed to execute saved query.")
Expand All @@ -602,6 +606,7 @@ def cli_search(args: argparse.Namespace):
args.cursor,
args.sort,
args.fields,
args.pages,
)
console.print_json(json.dumps(res))
except CensysAsmException:
Expand Down Expand Up @@ -885,6 +890,12 @@ def add_verbose(parser):
type=List[str],
default=[],
)
execute_saved_query_by_name_parser.add_argument(
"--pages",
help="Number of pages to return. Defaults to 1.",
type=int,
default=1,
)
add_verbose(execute_saved_query_by_name_parser)
execute_saved_query_by_name_parser.set_defaults(
func=cli_execute_saved_query_by_name
Expand Down Expand Up @@ -920,6 +931,12 @@ def add_verbose(parser):
type=List[str],
default=[],
)
execute_saved_query_by_id_parser.add_argument(
"--pages",
help="Number of pages to return. Defaults to 1.",
type=int,
default=1,
)
add_verbose(execute_saved_query_by_id_parser)
execute_saved_query_by_id_parser.set_defaults(func=cli_execute_saved_query_by_id)

Expand Down Expand Up @@ -965,5 +982,11 @@ def add_verbose(parser):
type=str,
required=False,
)
search_parser.add_argument(
"--pages",
help="Number of pages to return. Defaults to 1.",
type=int,
default=1,
)
add_verbose(search_parser)
search_parser.set_defaults(func=cli_search)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "censys"
version = "2.2.13"
version = "2.2.14"
description = "An easy-to-use and lightweight API wrapper for Censys APIs (censys.io)."
authors = ["Censys, Inc. <support@censys.io>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/asm/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

TEST_INVENTORY_SEARCH_JSON = {
"totalHits": 0,
"nextCursor": "string",
"nextCursor": "",
"previousCursor": "string",
"queryDurationMillis": 0,
"hits": [{}],
Expand Down

0 comments on commit b4c1a66

Please sign in to comment.