Skip to content

Commit

Permalink
Merge pull request #1388 from microsoftgraph/fix/code-samples
Browse files Browse the repository at this point in the history
Update code samples
  • Loading branch information
Ndiritu authored Nov 2, 2023
2 parents 82b5129 + 6098a05 commit 47dd5dd
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,27 @@ We call `iterate()` while passing a callback to be executed. If the callback ret
Iteration can be resumed by calling `iterate()` again.

```php
use Microsoft\Graph\Core\Tasks\PageIterator;
use Microsoft\Graph\Generated\Models\Message;
use DateTimeInterface;

$messages = $graphServiceClient->users()->byUserId(USER_ID)->messages()->get()->wait();

$pageIterator = new PageIterator($messages, $graphServiceClient->getRequestAdapter());

$callback = function (Message $message) {
echo "Message ID: {$message->getId()}";
return ($message->getId() !== 5);
}
$counter = 0;
$callback = function (Message $message) use (&$counter) {
echo "Subject: {$message->getSubject()}, Received at: {$message->getReceivedDateTime()->format(DateTimeInterface::RFC2822)}\n";
$counter ++;
return ($counter % 5 != 0);
};

// iteration will pause at message ID 5
$pageIterator->iterate($callback);
while ($pageIterator->hasNext()) {
// iteration pauses and resumes after every 5 messages
$pageIterator->iterate($callback);

// resumes iteration from next message (ID 6)
$pageIterator->iterate($callback);
echo "\nPaused iteration...Total messages: {$counter}\n\n";
}

```

Expand Down Expand Up @@ -299,6 +305,8 @@ The SDK provides a `LargeFileUpload` task that slices your file into bytes and p
To add a large attachment to an Outlook message:

```php
use Psr\Http\Client\NetworkExceptionInterface;


// create a file stream
$file = Utils::streamFor(fopen('fileName', 'r'));
Expand All @@ -312,16 +320,27 @@ $attachmentItem->setSize($file->getSize());
$uploadSessionRequestBody = new CreateUploadSessionPostRequestBody();
$uploadSessionRequestBody->setAttachmentItem($attachmentItem);

/** @var UploadSession $uploadSession */
$uploadSession = $graphServiceClient->users()->byUserId(USER_ID)->messages()->byMessageId('[id]')->attachments()->createUploadSession()->post($uploadSessionRequestBody)->wait();

// upload
$largeFileUpload = new LargeFileUploadTask($uploadSession, $graphServiceClient->getRequestAdapter(), $file);
try{
try {
$uploadSession = $largeFileUpload->upload()->wait();
} catch (\Psr\Http\Client\NetworkExceptionInterface $ex) {
} catch (NetworkExceptionInterface $ex) {
// resume upload in case of network errors
$uploadSession = $largeFileUpload->resume()->wait();
$retries = 0;
$maxRetries = 3;
while ($retries < $maxRetries) {
try {
$uploadSession = $largeFileUpload->resume()->wait();
if ($uploadSession) {
break;
}
} catch (NetworkExceptionInterface $ex) {
$retries ++;
}
}
throw $ex;
}

```
Expand Down Expand Up @@ -429,7 +448,6 @@ use Microsoft\Graph\BatchRequestBuilder;
use Microsoft\Graph\Core\Requests\BatchResponseItem;

$requestBuilder = new BatchRequestBuilder($graphServiceClient->getRequestAdapter());
/** @var BatchResponseContent $batchResponse */
$batchResponse = $requestBuilder->postAsync($batchRequestContent)->wait();

```
Expand Down

0 comments on commit 47dd5dd

Please sign in to comment.