From b2f1b3d9cb49800ca66fdb6847b62bdd9d31e0e8 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Thu, 2 Nov 2023 05:39:20 +0300 Subject: [PATCH 1/3] Update page iterator sample --- docs/Examples.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/Examples.md b/docs/Examples.md index 8a46e622986..87edcebb2fa 100644 --- a/docs/Examples.md +++ b/docs/Examples.md @@ -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"; +} ``` @@ -312,12 +318,11 @@ $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) { // resume upload in case of network errors @@ -429,7 +434,6 @@ use Microsoft\Graph\BatchRequestBuilder; use Microsoft\Graph\Core\Requests\BatchResponseItem; $requestBuilder = new BatchRequestBuilder($graphServiceClient->getRequestAdapter()); -/** @var BatchResponseContent $batchResponse */ $batchResponse = $requestBuilder->postAsync($batchRequestContent)->wait(); ``` From f8e7965c7b18ffbb068304fdf5523e5bfb825612 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Thu, 2 Nov 2023 06:16:11 +0300 Subject: [PATCH 2/3] Resume large file uploads multiple times with retry --- docs/Examples.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/Examples.md b/docs/Examples.md index 87edcebb2fa..a019f4927f6 100644 --- a/docs/Examples.md +++ b/docs/Examples.md @@ -305,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')); @@ -324,9 +326,23 @@ $uploadSession = $graphServiceClient->users()->byUserId(USER_ID)->messages()->by $largeFileUpload = new LargeFileUploadTask($uploadSession, $graphServiceClient->getRequestAdapter(), $file); 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 ++; + } + } + if ($retries >= $maxRetries && !$uploadSession) { + $cancelledUploadSession = $largeFileUpload->cancel(); + } } ``` From 6098a055b1054f64e87f3aa76e7e79beb77a1a48 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Thu, 2 Nov 2023 06:30:02 +0300 Subject: [PATCH 3/3] Throw network exception if retries fail due to network issues --- docs/Examples.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/Examples.md b/docs/Examples.md index a019f4927f6..b3239803b85 100644 --- a/docs/Examples.md +++ b/docs/Examples.md @@ -340,9 +340,7 @@ try { $retries ++; } } - if ($retries >= $maxRetries && !$uploadSession) { - $cancelledUploadSession = $largeFileUpload->cancel(); - } + throw $ex; } ```