Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Dynamic pagination for _get_groups and _get_objects #217

Open
RasmusThing opened this issue Jun 16, 2023 · 2 comments
Open

Dynamic pagination for _get_groups and _get_objects #217

RasmusThing opened this issue Jun 16, 2023 · 2 comments

Comments

@RasmusThing
Copy link
Contributor

Remove the size and page variables for the _get_groups and _get_objects functions and add dynamic pagination in the library itself.

This will avoid users of the library to make duplicate code to paginate the endpoints themselves.

It's even written in the library as a TODO :)
# TODO add dynamic paging?

@RasmusThing
Copy link
Contributor Author

RasmusThing commented Jun 16, 2023

This should do the trick.
`def _get_groups(self, url, filter: str = None):
"""
Get generic group lists.

:param url: Base URL for requesting lists
:return: result dictionary
"""
result = {
    "success": False,
    "response": "",
    "error": "",
}

self.ise.headers.update(
    {"Accept": "application/json", "Content-Type": "application/json"}
)

f = furl(url)
# Max resources per page cannot be more then 100 resources.
f.args["size"] = 100
# TODO add filter valication
if filter:
    f.args["filter"] = filter

resp = self.ise.get(f.url, timeout=self.timeout)

if resp.status_code == 200:
    result["success"] = True
    result["total"] = resp.json()["SearchResult"]["total"]
    result["response"] = []
    for page in range(1, int((result["total"] / f.args["size"] + 1) + 1)):
        f.args["page"] = page
        resp = self.ise.get(f.url, timeout=self.timeout)
        if resp.status_code == 200:
            result["response"] += [
                (i["name"], i["id"], i["description"])
                for i in resp.json()["SearchResult"]["resources"]
            ]
    return result

else:
    return ERS._pass_ersresponse(result, resp)

def _get_objects(self, url, filter: str = None):
"""
Generic method for requesting objects lists.

:param url: Base URL for requesting lists
:param filter: argument side of a ERS filter string. Default: None
:return: result dictionary
"""
result = {
    "success": False,
    "response": "",
    "error": "",
}

self.ise.headers.update(
    {"Accept": "application/json", "Content-Type": "application/json"}
)

f = furl(url)
# Max resources per page cannot be more then 100 resources.
f.args["size"] = 100
# TODO add filter valication
if filter:
    f.args["filter"] = filter

resp = self.ise.get(f.url, timeout=self.timeout)

if resp.status_code == 200:
    json_res = resp.json()["SearchResult"]

    if int(json_res["total"]) >= 1:
        result["success"] = True
        result["total"] = json_res["total"]
        result["response"] = []
        for page in range(1, int((result["total"] / f.args["size"] + 1) + 1)):
            f.args["page"] = page
            resp = self.ise.get(f.url, timeout=self.timeout)
            if resp.status_code == 200:
                json_res = resp.json()["SearchResult"]
                result["response"] += [
                    (i["name"], i["id"]) for i in json_res["resources"]
                ]
        return result

    elif int(json_res["total"]) == 0:
        result["success"] = True
        result["response"] = []
        result["total"] = json_res["total"]
        return result
else:
    return ERS._pass_ersresponse(result, resp)`

@falkowich
Copy link
Owner

Hi,

Thanks for the code!
Is it possible that you can and/or have the time to do a PR?

If not, I'll create a PR myself with the changes.
Again, thanks for the help :)

--
Kind Regards Falk

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants