This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbatchRequest.ts
More file actions
60 lines (46 loc) · 1.66 KB
/
batchRequest.ts
File metadata and controls
60 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {BatchRequestOperation} from "./structuredTypes";
/**
* The BATCH API allows a user to execute multiple OneNoteApi actions in a single HTTP request.
* For example, sending two PATCHES in the same HTTP request
* To use, construct a new BatchRequest and then pass in an object that adheres to the BatchRequestOperation interface into
* BatchRequest::addOperation(...). Once the request is built, send it using OneNoteApi::sendBatchRequest(...)
*/
export class BatchRequest {
private operations: BatchRequestOperation[];
private boundaryName: string;
private requestBody: string;
constructor() {
this.operations = [];
this.boundaryName = "batch_" + Math.floor(Math.random() * 1000);
}
public addOperation(op: BatchRequestOperation): void {
this.operations.push(op);
}
public getOperation(index: number): BatchRequestOperation {
return this.operations[index];
}
public getNumOperations(): number {
return this.operations.length;
}
public getRequestBody(): string {
let data = "";
this.operations.forEach((operation) => {
let req = "";
req += "--" + this.boundaryName + "\r\n";
req += "Content-Type: application/http" + "\r\n";
req += "Content-Transfer-Encoding: binary" + "\r\n";
req += "\r\n";
req += operation.httpMethod + " " + operation.uri + " " + "HTTP/1.1" + "\r\n";
req += "Content-Type: " + operation.contentType + "\r\n";
req += "\r\n";
req += (operation.content ? operation.content : "") + "\r\n";
req += "\r\n";
data += req;
});
data += "--" + this.boundaryName + "--\r\n";
return data;
}
public getContentType(): string {
return 'multipart/mixed; boundary="' + this.boundaryName + '"';
}
}