Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Resolve pytype --strict-none-binding issue in the api client #3214

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions api_client/python/timesketch_api_client/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,19 @@ def _execute_query(self, file_name="", count=False):
"""Execute a search request and store the results.

Args:
file_name (str): optional file path to a filename that
file_name (str): Optional file path to a filename that
all the results will be saved to. If not provided
the results will be stored in the search object.
count (bool): optional boolean that determines whether
count (bool): Optional boolean that determines whether
we want to execute the query or only count the
number of events that the query would produce.
number of events that the query would produce. If
set to True, the results will be stored in the
search object, and the number of events will be
returned.

Returns:
A dict with the search results or the total number of events
(if count=True) or None if saved to file.
"""
query_filter = self.query_filter
if not isinstance(query_filter, dict):
Expand Down Expand Up @@ -531,14 +538,14 @@ def _execute_query(self, file_name="", count=False):
if file_name:
with open(file_name, "wb") as fw:
fw.write(response.content)
return
return None

response_json = error.get_response_json(response, logger)

if count:
meta = response_json.get("meta", {})
self._total_elastic_size = meta.get("total_count", 0)
return
return meta.get("total_count", 0)

scroll_id = response_json.get("meta", {}).get("scroll_id", "")
form_data["scroll_id"] = scroll_id
Expand Down Expand Up @@ -579,6 +586,7 @@ def _execute_query(self, file_name="", count=False):
)

self._raw_response = response_json
return response_json

def add_chip(self, chip):
"""Add a chip to the ..."""
Expand Down Expand Up @@ -647,7 +655,7 @@ def expected_size(self):
if self._total_elastic_size:
return self._total_elastic_size

self._execute_query(count=True)
_ = self._execute_query(count=True)
return self._total_elastic_size

def from_manual( # pylint: disable=arguments-differ
Expand Down Expand Up @@ -1074,8 +1082,10 @@ def scrolling_enable(self):

def to_dict(self):
"""Returns a dict with the respone of the query."""
if not self._raw_response:
if self._raw_response is None:
self._execute_query()
if not self._raw_response:
raise ValueError("No results to return.")
Comment on lines +1087 to +1088
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _raw_response contains an empty list (no results) will this still evaluate to True? Why not re-use the is None clause instead? Same comment for L 1113


return self._raw_response

Expand All @@ -1098,8 +1108,10 @@ def to_file(self, file_name):

def to_pandas(self):
"""Returns a pandas DataFrame with the response of the query."""
if not self._raw_response:
self._execute_query()
if self._raw_response is None:
self._raw_response = self._execute_query()
if not self._raw_response:
raise ValueError("No results to return.")

return_list = []
timelines = {t.id: t.name for t in self._sketch.list_timelines()}
Expand Down
3 changes: 3 additions & 0 deletions api_client/python/timesketch_api_client/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ def to_string(self):
string_list.append(block.text)
elif block.TYPE == "view":
search_obj = block.view
if search_obj is None:
logging.warning("Block has no view. Skipping")
continue
data_frame = search_obj.to_pandas()
string_list.append(data_frame.to_string(index=False))
elif block.TYPE == "aggregation":
Expand Down
Loading