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

Added new APIv2 endpoints that allow advanced searches and included pagination functionality #47

Merged
merged 30 commits into from
Jan 31, 2022

Conversation

alannix-lw
Copy link
Contributor

No description provided.

@alannix-lw alannix-lw added help wanted Extra attention is needed draft enhancement New feature or request labels Dec 23, 2021
@alannix-lw alannix-lw changed the title feat: added initial function to process nextPage for pagination Adding pagination functionality Dec 29, 2021
@@ -29,49 +32,84 @@ def __init__(self, session):
def get(self,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the idea for paginated apis that get() will always return "all" and that if you actually want to work with pagination you would use a raw get_pages()? Meaning we're not actually exposing iterables at the api layer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is one of the things that I'm wrestling with on this update. My dream was to give any existing integrations all pages of data for the price of "on the house," but due to the lack of properly utilizing a generator, I realize that there may be some significant performance and resource consumption issues related to that. Would love some feedback on whether we should leave get alone and simply introduce new get_pages and items/get_items methods for pagination.

Copy link
Collaborator

@hazedav hazedav Dec 31, 2021

Choose a reason for hiding this comment

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

I don't fully understand the backwards compatibility requirements as it pertains to whether or not we're adding paging to things we've already established that get() would return everything...

One approach could be that get() would be a generator and all() would return everything. For things that aren't paginated we could just alias get->all.

Otherwise you could establish get() as the thing that returns everything and for paginated resource introduce a get_pages() which would also seem to align with the library.


logger.info(f"PATCH request to URI: {uri}")
logger.debug(f"PATCH request data:\n{data}")
def get_pages(self, uri, params=None, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there any pagination request settings (i.e. max items per page) here that we would want to promote, or should this simply be part of params?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Waiting for final documentation on this, but it looks like max items and payload size limits are "future features": https://docs.google.com/document/d/12z5Q7byMJQEoUFS-eLGspyPN-5eMC3jm/edit#heading=h.dfxjw4abp07x

Copy link
Contributor

Choose a reason for hiding this comment

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

well, we can also add in a max_items here and just have a simple counter.. that is not here, but in hte get_items function, and then exit the generator once you reach that max number.

Copy link
Contributor

@kiddinn kiddinn left a comment

Choose a reason for hiding this comment

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

few comments

laceworksdk/api/__init__.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/v2/cloud_activities.py Outdated Show resolved Hide resolved
return response
try:
next_page = response.json()['paging']['urls']['nextPage']
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a pretty broad exception handling here... can y8ou narrow that down?

also better to split this up a bit

at least to...

response_json = response.json()
next_page = response_json.get("paging", {}).get("urls", {}).get("nextPage")

That way you at least don't get KeyError there, etc. (also single vs double quote for consistency in the code base)

Copy link
Contributor

Choose a reason for hiding this comment

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

you marked this as resolved, yet I still see the same code, with a except Exception here

(that is to say, you did adress part of the comment, just not all of it, and not the broad exception handling aspect of it)

laceworksdk/http_session.py Outdated Show resolved Hide resolved
laceworksdk/http_session.py Outdated Show resolved Hide resolved

logger.info(f"PATCH request to URI: {uri}")
logger.debug(f"PATCH request data:\n{data}")
def get_pages(self, uri, params=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

well, we can also add in a max_items here and just have a simple counter.. that is not here, but in hte get_items function, and then exit the generator once you reach that max number.

@alannix-lw alannix-lw changed the title Adding pagination functionality Added new APIv2 endpoints that allow advanced searches and included pagination functionality Jan 12, 2022
@alannix-lw alannix-lw removed the draft label Jan 12, 2022
@alannix-lw alannix-lw marked this pull request as ready for review January 12, 2022 17:08
Copy link
Contributor

@kiddinn kiddinn left a comment

Choose a reason for hiding this comment

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

quite a large PR, sorry for taking so long

laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Show resolved Hide resolved
laceworksdk/api/base_endpoint.py Show resolved Hide resolved
laceworksdk/api/crud_endpoint.py Outdated Show resolved Hide resolved
laceworksdk/api/v2/agent_access_tokens.py Outdated Show resolved Hide resolved
laceworksdk/api/v2/agent_access_tokens.py Show resolved Hide resolved
laceworksdk/api/v2/agent_access_tokens.py Show resolved Hide resolved
laceworksdk/api/v2/alert_profiles.py Show resolved Hide resolved
return response
try:
next_page = response.json()['paging']['urls']['nextPage']
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

you marked this as resolved, yet I still see the same code, with a except Exception here

(that is to say, you did adress part of the comment, just not all of it, and not the broad exception handling aspect of it)

Copy link
Contributor

@kiddinn kiddinn left a comment

Choose a reason for hiding this comment

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

Two nits, otherwise LGTM

laceworksdk/__init__.py Outdated Show resolved Hide resolved
laceworksdk/http_session.py Outdated Show resolved Hide resolved
@alannix-lw alannix-lw removed the help wanted Extra attention is needed label Jan 31, 2022
@alannix-lw alannix-lw merged commit ee3f6f8 into master Jan 31, 2022
@alannix-lw alannix-lw deleted the alannix-lw/pagination branch January 31, 2022 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request major
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants