Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add batch request content #354

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
151 changes: 151 additions & 0 deletions src/content/BatchItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/

/**
* @module BatchItem
*/

/**
* @interface
* Signature represents key value pair object
*/
import {
Parsable,
ParseNode,
SerializationWriter,
createUntypedNodeFromDiscriminatorValue,
} from "@microsoft/kiota-abstractions";

/**
* @interface
* Signature represents payload structure for batch request and response
*/
export interface BatchItem {
readonly id: string;
method: string;
url: string;
headers?: Record<string, string> | null;
body?: Record<string, any> | null;
Copy link
Member

Choose a reason for hiding this comment

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

What if the body is a stream?

Copy link
Member

Choose a reason for hiding this comment

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

@rkodev please avoid resolving conversation until a consensus has been reached. It makes progress much harder to track reviewers' side.

dependsOn?: string[];
}

/**
* @interface
* Signature represents unwrapped payload structure for batch response
*/
export interface BatchResponse {
id: string;
headers?: Record<string, string> | null;
body?: ArrayBuffer | null;
Copy link
Member

Choose a reason for hiding this comment

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

once they get a batch response (item?), if the payload is structured/a parsable, how can they get from the body to the deseiralized payload?

Copy link
Contributor Author

@rkodev rkodev Feb 21, 2025

Choose a reason for hiding this comment

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

I believe this is a will be resolved by implementing #354 (comment) ?

status?: number;
}

/**
* @interface
* Signature representing Batch request body
*/
export interface BatchRequestCollection {
requests: BatchItem[];
}

/**
* @interface
* Signature representing Batch response body
*/
export interface BatchResponseCollection {
responses: BatchResponse[];
}

/**
* Serializes the batch request body
* @param writer
* @param batchRequestBody
*/
export const serializeBatchRequestBody = (
writer: SerializationWriter,
batchRequestBody: Partial<BatchRequestCollection> | undefined | null = {},
): void => {
if (batchRequestBody) {
writer.writeCollectionOfObjectValues("requests", batchRequestBody.requests, serializeBatchItem);
}
};

/**
* Serializes the batch item
* @param writer
* @param batchRequestData
*/
export const serializeBatchItem = (
writer: SerializationWriter,
batchRequestData: Partial<BatchItem> | undefined | null = {},
): void => {
if (batchRequestData) {
writer.writeStringValue("id", batchRequestData.id);
writer.writeStringValue("method", batchRequestData.method);
writer.writeStringValue("url", batchRequestData.url);
writer.writeObjectValue("headers", batchRequestData.headers);
writer.writeObjectValue("body", batchRequestData.body);
writer.writeCollectionOfPrimitiveValues("dependsOn", batchRequestData.dependsOn);
}
};

/**
* BatchResponseCollection ParsableFactory
* @param _parseNode
*/
export const createBatchResponseContentFromDiscriminatorValue = (
_parseNode: ParseNode | undefined,
): ((instance?: Parsable) => Record<string, (node: ParseNode) => void>) => {
return deserializeIntoBatchResponseContent;
};

/**
* Deserializes the batch response body
* @param batchResponseBody
*/
export const deserializeIntoBatchResponseContent = (
batchResponseBody: Partial<BatchResponseCollection> | undefined = {},
): Record<string, (node: ParseNode) => void> => {
return {
responses: n => {
batchResponseBody.responses = n.getCollectionOfObjectValues(createBatchResponseFromDiscriminatorValue);
},
};
};

/**
* BatchItem ParsableFactory
* @param _parseNode
*/
export const createBatchResponseFromDiscriminatorValue = (
_parseNode: ParseNode | undefined,
): ((instance?: Parsable) => Record<string, (node: ParseNode) => void>) => {
return deserializeIntoBatchResponse;
};

/**
* Deserializes the batch item
* @param batchResponse
*/
export const deserializeIntoBatchResponse = (
batchResponse: Partial<BatchResponse> | undefined = {},
): Record<string, (node: ParseNode) => void> => {
return {
id: n => {
batchResponse.id = n.getStringValue();
},
headers: n => {
batchResponse.headers = n.getObjectValue<Record<string, string>>(createUntypedNodeFromDiscriminatorValue);
},
body: n => {
batchResponse.body = n.getByteArrayValue();
},
status: n => {
batchResponse.status = n.getNumberValue();
},
};
};
Loading
Loading