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

Documentation is lacking for handling of "@odata.nextLink" responses #690

Closed
WardenHub opened this issue Apr 25, 2024 · 2 comments
Closed
Labels
enhancement New feature or request

Comments

@WardenHub
Copy link

Is your feature request related to a problem? Please describe.
Documentation is lacking for handling of "@odata.nextLink" responses i.e. https://learn.microsoft.com/en-us/graph/api/signin-list?view=graph-rest-1.0&tabs=python#response-2

Describe the solution you'd like
Add an example how to retrieve the full data response

@WardenHub WardenHub added the enhancement New feature or request label Apr 25, 2024
@shemogumbe
Copy link
Collaborator

Hello, in a paginated response fromthe API, the odata nextlink can be used to get the data of the next page.
You can use the page iterator in microsoftgraph/msgraph-sdk-python-core#479 or optionally a simple snippet

calls = 0
    while calls <  5:
        response = requests.get(url, headers=headers)
        print(calls)
        data = response.json()
        if "@odata.nextLink" in data:
            url = data["@odata.nextLink"]
        elif "@odata.deltaLink" in data:
            print(data["@odata.deltaLink"])
            return data["@odata.deltaLink"]
        else:
            raise Exception("Unexpected response: no nextLink or deltaLink")
        calls += 1

The condition in the loop can be anything that suits your use case

@WardenHub
Copy link
Author

WardenHub commented May 8, 2024

Hey @shemogumbe thank you for the response,

Your example does not seem to be using the 'SignInCollectionResponse' object from the

from msgraph.generated.audit_logs.sign_ins.sign_ins_request_builder import SignInsRequestBuilder

.. isn't the whole point of the sdk that it handles things like odata.nextlink functionality?

Below the function I'm using

from msgraph import GraphServiceClient
from kiota_abstractions.api_error import APIError
from kiota_abstractions.base_request_configuration import RequestConfiguration
from msgraph.generated.audit_logs.sign_ins.sign_ins_request_builder import SignInsRequestBuilder

async def make_sign_ins_request(start_date, end_date,data = None):
    try:
        # Format dates to match the API requirement
        formatted_start_date = start_date.strftime("%Y-%m-%dT00:00:00Z")
        formatted_end_date = end_date.strftime("%Y-%m-%dT23:59:59Z")
        # Construct the filter
        query_params = SignInsRequestBuilder.SignInsRequestBuilderGetQueryParameters(
            filter=f"createdDateTime ge {formatted_start_date} and createdDateTime le {formatted_end_date}",
        )
        request_configuration = RequestConfiguration(
            query_parameters=query_params,
        )
        # Make the API request
        res = await graph_client.audit_logs.sign_ins.get(request_configuration=request_configuration)
        return res
    except APIError as e:
        if e.response_status_code == 429:
            print(f"{'-'*15}{e.response_status_code}{'-'*15}\nWaiting {config['request_delay']} seconds because of:\n\n{e}{'-'*30}")
            # Introduce a delay between requests
            await asyncio.sleep(config.get('request_delay', 0))
            # Retry the request and return its result
            res = await graph_client.audit_logs.sign_ins.get(request_configuration=request_configuration)
            return res
        else:
            print(f"{'-'*15}Error {e.response_status_code}{'-'*15}\n{e}\n{'-'*30}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants