Skip to content

Commit d98521c

Browse files
Adding usage docs for LargeFileUploadTask and Batching
1 parent df3cca0 commit d98521c

File tree

2 files changed

+359
-0
lines changed

2 files changed

+359
-0
lines changed

docs/content/Batching.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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+

docs/tasks/LargeFileUploadTask.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Large File Upload Task - Uploading large files to OneDrive.
2+
3+
This task simplifies the implementation of onedrive's [resumable upload](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession).
4+
5+
## Bootstrap code
6+
7+
```javascript
8+
const client = MicrosoftGraph.Client.init({
9+
debugLogging: true,
10+
authProvider: function (done) {
11+
done(null, <AUTH TOKEN>);
12+
}
13+
});
14+
```
15+
16+
## Uploading from browser
17+
18+
HTML to select the file for uploading.
19+
20+
```HTML
21+
<input id="fileUpload" type="file" onchange="fileUpload(this)" />
22+
```
23+
24+
Get files from the input element and start uploading.
25+
26+
```javascript
27+
function fileUpload(elem) {
28+
let file = elem.files[0];
29+
// Method is from creating session and start uploading
30+
// client is from the bootstrap code
31+
largeFileUpload(client, file, file.name)
32+
.then((response) => {
33+
console.log(response);
34+
})
35+
.catch((error) => {
36+
console.error(error);
37+
});
38+
```
39+
40+
**With `async`/`await` syntactic sugar in ECMAScript 2017**
41+
42+
```javascript
43+
async function fileUpload(elem) {
44+
let file = elem.files[0];
45+
try {
46+
// Method is from creating session and start uploading
47+
// client is from the bootstrap code
48+
let response = await largeFilUpload(client, file, file.name);
49+
} catch (error) {
50+
console.error(error);
51+
}
52+
}
53+
```
54+
55+
## Uploading from NodeJS
56+
57+
```javascript
58+
function uploadFile() {
59+
fs.readFile(<PATH_OF_THE_FILE>, {}, function (err, file) {
60+
if(err) {
61+
throw err;
62+
}
63+
let fileName = <NAME_OF_THE_FILE>;
64+
// Method is from creating session and start uploading
65+
// client is from the bootstrap code
66+
largeFileUpload(client, file, fileName)
67+
.then((response) => {
68+
console.log(response);
69+
})
70+
.catch((error) => {
71+
console.error(error);
72+
});
73+
});
74+
}
75+
```
76+
77+
## Creating session and start uploading
78+
79+
```javascript
80+
function largeFileUpload(client, file, fileName) {
81+
let options = {
82+
path: "/Documents",
83+
fileName: fileName,
84+
rangeSize: (1024 * 1024)
85+
};
86+
87+
MicrosoftGraph.OneDriveLargeFileUploadTask.create(client, file, options)
88+
.then((uploadTask) => {
89+
uploadTask.upload()
90+
.then((res) => {
91+
console.log(res);
92+
console.log("File Uploaded Successfully.!!");
93+
})
94+
.catch((err) => {
95+
console.log(err);
96+
});
97+
}).catch((err) => {
98+
console.log(err);
99+
});
100+
}
101+
```
102+
103+
**With `async`/`await` syntactic sugar in ECMAScript 2017**
104+
105+
```javascript
106+
async function uploadFile(client, file) {
107+
try {
108+
let options = {
109+
path: "/Documents",
110+
fileName: file.name,
111+
rangeSize: (1024 * 1024)
112+
};
113+
const uploadTask = await MicrosoftGraph.OneDriveLargeFileUploadTask.create(client, file, options);
114+
const response = await uploadTask.upload();
115+
console.log(response);
116+
console.log("File Uploaded Successfully.!!");
117+
} catch (err) {
118+
console.log(err);
119+
}
120+
}
121+
```
122+
123+
## We can just resume the broken upload
124+
125+
_Lets consider some break down happens in the middle of uploading, with the uploadTask object in hand you can resume easily._
126+
```javascript
127+
uploadTask.resume();
128+
```
129+
130+
## Even you can control the whole upload process
131+
132+
_You can create the upload task, and play with it by using **sliceFile** and **uploadSlice** methods_
133+
134+
```javascript
135+
let range = uploadTask.getNextRange();
136+
let slicedFile = uploadTask.sliceFile(range);
137+
uploadTask.uploadSlice(slicedFile, range, uploadTask.file.size);
138+
```
139+
140+

0 commit comments

Comments
 (0)