okta / okta-sdk-python

Apache License 2.0
234 stars 143 forks source link

Pagination in get_logs not respecting until query parameter #392

Closed cleavenworth closed 6 months ago

cleavenworth commented 6 months ago

I'm having a similar issue to #231, however from what I can tell I am following the requirements correctly to return a bounded response from the Logs endpoint.

Here is a snippet of my code where I am calling get_logs, same as the referenced issue the code will execute on a loop until there are no more results from the log. I am specifying both since and until in the log query. The results returned are correct, but resp always returns has_next as True, even after reaching the end of available logs.

async def main(app_id: str, days: int):
    today = datetime.now()
    window = today - timedelta(days=int(days))
    today = today.strftime("%Y-%m-%dT%H:%M:%SZ")
    window = window.strftime("%Y-%m-%dT%H:%M:%SZ")
    query = f'target.id eq "{app_id}" and target.type eq "AppInstance" and (eventType eq "user.authentication.sso" or eventType eq "user.authentication.verify")'
    log_lines = []
    while True:
        log_results, resp, err = await OktaClient.get_logs(
            query_params={
                "since": window,
                "until": today,
                "filter": query,
                "limit": "1000",
            }
        )
        for log in log_results:
            log_lines.append(log)
        if resp.has_next():
            log_results, err = await resp.next()
        else:
            break
cleavenworth commented 6 months ago

User error. I put the log results within the while loop without assigning the initial response beforehand.

sgregorioTC commented 4 months ago

@cleavenworth, this was super helpful to me building a log call. Can you share what you mean by your user error comment, I'm fairly inexperience with coding and would love to see what it looks like after you discovered the issue to help understand it.