An Schultz
An Schultz

Reputation: 13

Calls to Office365 API to synchronize events, throttling

I am trying to synchronize a few events from Outlook to my local DB and I call the API as below:

$url = 'https://outlook.office365.com/api/v2.0/users/' . $this->user . '/CalendarView/'
    . '?startDateTime=' . $start_datetime
    . '&endDateTime=' . $end_datetime

This gives me all the events from Outlook between two specific dates.

Then I go and save all this events using the code below. The problem with it is that it returns only 10 events at a time.

$http = new \Http_Curl();
        $http->set_headers( $this->get_headers() );
        $response = $http->get( $url );

        $data = array();

        $continue = true;
        while ( $continue ) {
            if ( isset($response->value) ) {
                $arr = array();

                foreach ( $response->value as $event ) {
                    $arr[] = $event;
                }

                $data = array_merge( $data, $arr );
            }

            $property = '@odata.nextLink';
            if ( isset( $response->$property ) ) {
                $url = $response->$property;
                $response = $http->get( $url );
            } else {
                $continue = false;
            }
        }

        unset( $http );

        return $data;

I tried then to call the API like below, setting the top parameter to 10, but I end up with many empty events.

$url = 'https://outlook.office365.com/api/v2.0/users/' . $this->user . '/CalendarView/'
    . '?startDateTime=' . $start_datetime
    . '&endDateTime=' . $end_datetime
     .'&top=100'

I am trying to avoid making more than 60 calls per minute. Is there any way to first get the number of events between two dates and then retrieve all of them, so the top parameter should actually be the total number of events.

Upvotes: 0

Views: 80

Answers (1)

Yogesh
Yogesh

Reputation: 2228

The correct query parameter is $top and not top. Notice $ in there.

http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html#_Toc453752362

5.1.5 System Query Options $top and $skip The $top system query option requests the number of items in the queried collection to be included in the result. The $skip query option requests the number of items in the queried collection that are to be skipped and not included in the result. A client can request a particular page of items by combining $top and $skip. The semantics of $top and $skip are covered in the [OData-Protocol] document. The [OData-ABNF] top and skip syntax rules define the formal grammar of the $top and $skip query options respectively.

Upvotes: 1

Related Questions