TheNetworg/oauth2-azure

Access odata in API responses

jamesgraham opened this issue ยท 6 comments

Is there an easy way to not pass everything through wrapResponse?

I know this is useful in 99% of cases, but there are odata properties that I need, for example @odata.deltaLink.

https://developer.microsoft.com/en-us/graph/docs/Concepts/delta_query_events

I am using getObjects to handle the paging which works very well.

One way I can think of right now is to make request method public or add an optional parameter to the functions wrapResponse which would be true by default but would allow your to override it. Which is better for you?

I don't really mind but request being public would probably provide more flexibility for the future. I will need to write my own modified getObjects method but this is a small price to pay for that flexibility ๐Ÿ‘

I have pushed the change to master. Try switching to dev-master branch of the library in Composer and let me know if everything works fine, I will then publish it as 1.3.2 version. Additionally, if you would be so kind and shared how you handled it for getObjects, I would be most grateful.

This works very well.

It has solved a particular requirement of syncing calendar events wth the Graph API. Hopefully this snippet will help someone in the future.

$provider->urlAPI = 'https://graph.microsoft.com/v1.0/';

$accessToken = $this->getAccessToken();

if (empty($optParams['deltaToken'])) {
    $ref = 'me/calendarview/delta?startDateTime='.$optParams['startTime'].'&endDateTime='.$optParams['endTime'];
} else {
    $ref = 'me/calendarview/delta?$deltatoken='.$optParams['deltaToken'];
}

$headers = [
    'Prefer' => 'outlook.body-content-type="text"',
];

$events = [];
$deltaToken = null;
do {
    $response = $provider->request('get', $ref, $accessToken, ['headers' => $headers]);
    $values = $response;
    if (isset($response['value'])) {
        $values = $response['value'];
    }
    foreach ($values as $value) {
        $events[] = $value;
    }

    // Continue iterating through the pages.
    if (isset($response['odata.nextLink'])) {
        $ref = $response['odata.nextLink'];
    } elseif (isset($response['@odata.nextLink'])) {
        $ref = $response['@odata.nextLink'];
    } else {
        $ref = null;
    }

    // If we have reached the last page, store the delta for later.
    if (isset($response['@odata.deltaLink'])) {
        if (preg_match('/deltatoken=([^&]+)/i', $response['@odata.deltaLink'], $matches)) {
            $deltaToken = $matches[1];
        }
    }
} while ($ref);

I am going to fix #34 and also #35 and then release the library as v1.3.2 thanks a lot for the help!

Thanks @hajekj I'll keep an eye out for it