|
| 1 | +# [Batching](https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching) |
| 2 | +Batching is a way of combining multiple requests to resources in same/different workloads in a single HTTP request. This can be achieved by making a post call with those requests as a JSON payload to $batch endpoint. |
| 3 | + |
| 4 | +### BatchRequestContent |
| 5 | +A component which eases the way of creating batch request payload. This class handles all the batch specific payload construction and stuffs, we just need to worry about individual requests. |
| 6 | + |
| 7 | +### BatchResponseContent |
| 8 | +A Component to simplify the processing of batch responses by providing functionalities like getResponses, getResponseById, getResponsesIterator |
| 9 | + |
| 10 | +**Update the profile photo and download the uploaded photo with batching - Making serial requests** |
| 11 | +```typescript |
| 12 | +// elem here is the input HTML element of type file |
| 13 | +const serialBatching = async function(elem) { |
| 14 | + try { |
| 15 | + let file = elem.files[0]; |
| 16 | + let uploadProfilePhotoRequest = new Request("/me/photo/$value", { |
| 17 | + method: "PUT", |
| 18 | + headers: { |
| 19 | + "Content-type": file.type |
| 20 | + }, |
| 21 | + body: file |
| 22 | + }); |
| 23 | + |
| 24 | + let uploadProfilePhotoStep: BatchRequestStep = { |
| 25 | + id: "1", |
| 26 | + request: uploadProfilePhotoRequest |
| 27 | + }; |
| 28 | + |
| 29 | + let downloadProfilePhotoRequest = new Request("/me/photo/$value", { |
| 30 | + method: "GET" |
| 31 | + }); |
| 32 | + |
| 33 | + let downloadId = "2"; |
| 34 | + let downloadProfilePhotoStep: BatchRequestStep = { |
| 35 | + id: downloadId, |
| 36 | + request: downloadProfilePhotoRequest, |
| 37 | + // Adding dependsOn makes this request wait until the dependency finishes |
| 38 | + // This download request waits until the upload request completes |
| 39 | + dependsOn: ["1"] |
| 40 | + }; |
| 41 | + |
| 42 | + //Create instance by passing a batch request step |
| 43 | + let batchRequestContent = new MicrosoftGraph.BatchRequestContent([uploadProfilePhotoStep, downloadProfilePhotoStep]); |
| 44 | + |
| 45 | + //Extracting content from the instance |
| 46 | + let content = await batchRequestContent.getContent(); |
| 47 | + |
| 48 | + //Making call to $batch end point with the extracted content |
| 49 | + let response = await client.api("/$batch").post(content); |
| 50 | + |
| 51 | + //Create instance with response from the batch request |
| 52 | + let batchResponseContent = new MicrosoftGraph.BatchResponseContent(response); |
| 53 | + |
| 54 | + //Getting response by id |
| 55 | + console.log(batchResponse.getResponseById(downloadId)); |
| 56 | + |
| 57 | + //Getting all the responses |
| 58 | + console.log(batchResponseContent.getResponses()); |
| 59 | + } catch (error) { |
| 60 | + console.error(error); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +**GET of POST contents from and to different workloads - Making parallel requests** |
| 67 | + |
| 68 | +```typescript |
| 69 | +const parallelBatching = async function() { |
| 70 | + try { |
| 71 | + let fileName = "test.pdf"; |
| 72 | + let oneDriveFileRequest = new Request(`/me/drive/root:/${fileName}:/content`, { |
| 73 | + method: "GET" |
| 74 | + }); |
| 75 | + |
| 76 | + let oneDriveFileStep: BatchRequestStep = { |
| 77 | + id: "1", |
| 78 | + request: oneDriveFileRequest |
| 79 | + }; |
| 80 | + |
| 81 | + let folderDetails = { |
| 82 | + "name": "Testing Batch", |
| 83 | + "folder": {} |
| 84 | + }; |
| 85 | + let createFolder = new Request("/me/drive/root/children", { |
| 86 | + method: "POST", |
| 87 | + headers: { |
| 88 | + "Content-type": "application/json" |
| 89 | + }, |
| 90 | + body: JSON.stringify(folderDetails) |
| 91 | + }); |
| 92 | + |
| 93 | + let createFolderStep: BatchRequestStep = { |
| 94 | + id: "2", |
| 95 | + request: createFolder, |
| 96 | + dependsOn: ["1"] |
| 97 | + }; |
| 98 | + |
| 99 | + let mailsRequest = new Request("/me/messages", { |
| 100 | + method: "GET" |
| 101 | + }); |
| 102 | + |
| 103 | + let mailsRequestStep: BatchRequestStep = { |
| 104 | + id: "3", |
| 105 | + request: mailsRequest, |
| 106 | + dependsOn: ["2"] |
| 107 | + }; |
| 108 | + |
| 109 | + //Create instance by passing a batch request step |
| 110 | + let batchRequestContent = new MicrosoftGraph.BatchRequestContent(); |
| 111 | + |
| 112 | + // Dynamically adding requests to the batch content |
| 113 | + let fileDownloadId = batchRequestContent.addRequest(oneDriveFileStep); |
| 114 | + |
| 115 | + let createFolderId = batchRequestContent.addRequest(createFolderStep); |
| 116 | + |
| 117 | + let mailsId = batchRequestContent.addRequest(mailsRequestStep); |
| 118 | + |
| 119 | + // Dynamically removing unnecessary dependencies |
| 120 | + // NOTE: Passing second param empty removes all the dependencies for that request |
| 121 | + batchRequestContent.removeDependency("3", "2"); |
| 122 | + |
| 123 | + // Dynamically removing unnecessary request. Removing a request automatically removes the dependencies in relevant dependents |
| 124 | + // Here download file dependency is removed from the onedrive create folder request |
| 125 | + batchRequestContent.removeRequest(fileDownloadId); |
| 126 | + |
| 127 | + // Now no requests depends on anything so the request will be made parallel in the service end |
| 128 | + // Extracting content from the instance |
| 129 | + let content = await batchRequestContent.getContent(); |
| 130 | + |
| 131 | + //Making call to $batch end point with the extracted content |
| 132 | + let response = await client.api("/$batch").post(content); |
| 133 | + |
| 134 | + //Create instance with response from the batch request |
| 135 | + let batchResponse = new MicrosoftGraph.BatchResponseContent(response); |
| 136 | + |
| 137 | + //Getting iterator and looping through the responses iterator |
| 138 | + let iterator = batchResponse.getResponsesIterator(); |
| 139 | + let data = iterator.next(); |
| 140 | + while (!data.done) { |
| 141 | + console.log(data.value[0] + ":" + data.value[1]); |
| 142 | + data = iterator.next(); |
| 143 | + } |
| 144 | + } catch (error) { |
| 145 | + console.error(error); |
| 146 | + } |
| 147 | +}; |
| 148 | +``` |
| 149 | + |
| 150 | +**Create a folder in OneDrive and upload multiple files - Making batch request with all other request depend on one request** |
| 151 | + |
| 152 | +```typescript |
| 153 | +// elem here is the input HTML element of type file |
| 154 | +const sameBatching = async function (elem) { |
| 155 | + try { |
| 156 | + let file1 = elem.files[0]; |
| 157 | + let file2 = elem.files[1]; |
| 158 | + |
| 159 | + let folderDetails = { |
| 160 | + "name": "MyFiles", |
| 161 | + "folder": {} |
| 162 | + }; |
| 163 | + let createFolder = new Request("/me/drive/root/children", { |
| 164 | + method: "POST", |
| 165 | + headers: { |
| 166 | + "Content-type": "application/json" |
| 167 | + }, |
| 168 | + body: JSON.stringify(folderDetails) |
| 169 | + }); |
| 170 | + |
| 171 | + let createFolderStep: BatchRequestStep = { |
| 172 | + id: "1", |
| 173 | + request: createFolder |
| 174 | + }; |
| 175 | + |
| 176 | + let uploadFileRequest1 = new Request(`/me/drive/root:/MyFiles/${file1.name}:/content`, { |
| 177 | + method: "PUT", |
| 178 | + headers: { |
| 179 | + "Content-type": file1.type |
| 180 | + }, |
| 181 | + body: file1 |
| 182 | + }); |
| 183 | + |
| 184 | + let uploadFileStep1: BatchRequestStep = { |
| 185 | + id: "2", |
| 186 | + request: uploadFileRequest1, |
| 187 | + dependsOn: ["1"] |
| 188 | + }; |
| 189 | + |
| 190 | + let uploadFileRequest2 = new Request(`/me/drive/root:/MyFiles/${file2.name}:/content`, { |
| 191 | + method: "PUT", |
| 192 | + headers: { |
| 193 | + "Content-type": file2.type |
| 194 | + }, |
| 195 | + body: file2 |
| 196 | + }); |
| 197 | + |
| 198 | + let uploadFileStep2: BatchRequestStep = { |
| 199 | + id: "3", |
| 200 | + request: uploadFileRequest2, |
| 201 | + dependsOn: ["1"] |
| 202 | + }; |
| 203 | + |
| 204 | + let batchRequestContent = new MicrosoftGraph.BatchRequestContent([createFolderStep, uploadFileStep1, uploadFileStep2]); |
| 205 | + |
| 206 | + let content = await batchRequestContent.getContent(); |
| 207 | + |
| 208 | + let response = await client.api("/$batch").post(content); |
| 209 | + let batchResponseContent = new MicrosoftGraph.BatchResponseContent(response); |
| 210 | + console.log(batchResponseContent.getResponses()); |
| 211 | + } catch (error) { |
| 212 | + console.error(error); |
| 213 | + } |
| 214 | +}; |
| 215 | +``` |
| 216 | + |
| 217 | +### Constraints |
| 218 | +Refer these [#1](https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching), [#2](https://developer.microsoft.com/en-us/graph/docs/concepts/known_issues#json-batching) documents for current constraints in the batching |
| 219 | + |
0 commit comments