Skip to content

Conversation

@muthurathinam
Copy link
Contributor

@muthurathinam muthurathinam commented Sep 17, 2018

Batching

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.

BatchRequestContent

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.

let folderDetails = {
    "name": "Testing Batch",
    "folder": {}
};
let createFolder = new Request("/me/drive/root/children", {
    method: "POST",
    headers: {
        "Content-type": "application/json"
    },
    body: JSON.stringify(folderDetails)
});

let createFolderStep: BatchRequestStep = {
    id: "1",
    request: createFolder
};
//Create instance by passing a batch request step
let batchContent = new MicrosoftGraph.BatchRequestContent([createFolderStep]);

let fileName = "test.pdf";
let oneDriveFileRequest = new Request(`/me/drive/root:/${fileName}:/content`, {
    method: "GET"
});
let oneDriveFileStep: BatchRequestStep = {
    id: "2",
    request: oneDriveFileRequest,
    dependsOn: ["1"]
};
//Adding a batch request step to the batch
let oneDriveRequestId = batchContent.addRequest(oneDriveFileStep);

//Extracting content from the instance
let content = await batchContent.getContent();

//Making call to $batch end point with the extracted content
let response = await client.api("/$batch").post(content);

There are more functionalities like removeRequest, addDependency, removeDependency.

BatchResponseContent

Component to simplify the processing of batch responses by providing functionalities like getResponses, getResponseById, getResponsesIterator

//Create instance with response from the batch request
let batchResponse = new MicrosoftGraph.BatchResponseContent(response);

//Getting response by id
console.log(batchResponse.getResponseById(oneDriveRequestId));

//Getting all the responses
console.log(batchResponse.getResponses());

//Getting iterator and looping through the responses iterator
let iterator = batchResponse.getResponsesIterator();
let data = iterator.next();
while (!data.done) {
    console.log(data.value[0] + ":" + data.value[1]);
    data = iterator.next();
}

@muthurathinam
Copy link
Contributor Author

@MIchaelMainer Have done the changes that you have requested. Please have a look.

MIchaelMainer
MIchaelMainer previously approved these changes Sep 18, 2018
* @param {BatchRequestStep} request - The request value
* @return The id of the added request
*/
addRequest(request: BatchRequestStep): string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to return the id of the added Request? Could it be a 'void' method..

Copy link
Contributor Author

@muthurathinam muthurathinam Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of void, returning id gives the sense that the request is been added. This is why we are returning id.

if (self.requests.has(request.id)) {
let error = new Error(`Adding request with duplicate id ${request.id}, Make the id of the requests unique`);
error.name = "Duplicate RequestId Error";
throw error;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what happens at the caller if we throw from here..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error is thrown to the caller and the caller of this method have to handle this.

requestData.body = await BatchRequestContent.getRequestBody(request);
}
/**
* Check any other property needs to be used from the Request object and add them

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Check any other ...

it helps in textual searching for TODOs later on.

}
if (!bodyParsed) {
try {
if (typeof Blob !== "undefined") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this Blob ? some global ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Blob is a global object present only in the browser environment. Since code is being used both by node and browsers, we have this check

body = await new Promise(resolve => {
reader.addEventListener("load", function () {
let dataURL = <string>reader.result,
regex = new RegExp("^\s*data:(.+?\/.+?(;.+?\=.+?)*)?(;base64)?,(.*)\s*$"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally a good idea to add a couple of examples in the comments for regular expression patterns.

pankajpalOlk
pankajpalOlk previously approved these changes Sep 19, 2018
darrelmiller
darrelmiller previously approved these changes Sep 20, 2018
darrelmiller
darrelmiller previously approved these changes Sep 21, 2018
Copy link

@deepak2016 deepak2016 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@muthurathinam muthurathinam merged commit d723d95 into dev Sep 21, 2018
@muthurathinam muthurathinam deleted the BatchContent branch September 21, 2018 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants