|
| 1 | +import { ErrorMappings, HttpMethod, RequestAdapter, RequestInformation } from "@microsoft/kiota-abstractions"; |
| 2 | +import { |
| 3 | + BatchResponseBody, |
| 4 | + createBatchResponseContentFromDiscriminatorValue, |
| 5 | + serializeBatchRequestBody, |
| 6 | +} from "./BatchRequestStep.js"; |
| 7 | +import { BatchResponseContent } from "./BatchResponseContent.js"; |
| 8 | +import { BatchRequestContent } from "./BatchRequestContent.js"; |
| 9 | +import { BatchRequestContentCollection } from "./BatchRequestContentCollection.js"; |
| 10 | +import { BatchResponseContentCollection } from "./BatchResponseContentCollection.js"; |
| 11 | + |
| 12 | +export class BatchRequestBuilder { |
| 13 | + /** |
| 14 | + * @private |
| 15 | + * @static |
| 16 | + * Executes the requests in the batch request content |
| 17 | + */ |
| 18 | + private readonly requestAdapter: RequestAdapter; |
| 19 | + |
| 20 | + /** |
| 21 | + * @private |
| 22 | + * @static |
| 23 | + * Error mappings to be used while deserializing the response |
| 24 | + */ |
| 25 | + private readonly errorMappings: ErrorMappings; |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates an instance of BatchRequestContent. |
| 29 | + * @param {RequestAdapter} requestAdapter - The request adapter to be used for executing the requests. |
| 30 | + * @param {ErrorMappings} errorMappings - The error mappings to be used while deserializing the response. |
| 31 | + * @throws {Error} If the request adapter is undefined. |
| 32 | + * @throws {Error} If the error mappings are undefined. |
| 33 | + */ |
| 34 | + constructor(requestAdapter: RequestAdapter, errorMappings: ErrorMappings) { |
| 35 | + if (!requestAdapter) { |
| 36 | + const error = new Error("Request adapter is undefined, Please provide a valid request adapter"); |
| 37 | + error.name = "Invalid Request Adapter Error"; |
| 38 | + throw error; |
| 39 | + } |
| 40 | + this.requestAdapter = requestAdapter; |
| 41 | + if (!errorMappings) { |
| 42 | + const error = new Error("Error mappings are undefined, Please provide a valid error mappings"); |
| 43 | + error.name = "Invalid Error Mappings Error"; |
| 44 | + throw error; |
| 45 | + } |
| 46 | + this.errorMappings = errorMappings; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @public |
| 51 | + * @async |
| 52 | + * Executes the batch request |
| 53 | + */ |
| 54 | + public async postBatchResponseContentAsync( |
| 55 | + batchRequestContent: BatchRequestContent, |
| 56 | + ): Promise<BatchResponseContent | undefined> { |
| 57 | + const requestInformation = new RequestInformation(); |
| 58 | + requestInformation.httpMethod = HttpMethod.POST; |
| 59 | + requestInformation.urlTemplate = "{+baseurl}/$batch"; |
| 60 | + |
| 61 | + const content = batchRequestContent.getContent(); |
| 62 | + requestInformation.setContentFromParsable( |
| 63 | + this.requestAdapter, |
| 64 | + "application/json", |
| 65 | + content, |
| 66 | + serializeBatchRequestBody, |
| 67 | + ); |
| 68 | + |
| 69 | + requestInformation.headers.add("Content-Type", "application/json"); |
| 70 | + |
| 71 | + const result = await this.requestAdapter.send<BatchResponseBody>( |
| 72 | + requestInformation, |
| 73 | + createBatchResponseContentFromDiscriminatorValue, |
| 74 | + this.errorMappings, |
| 75 | + ); |
| 76 | + |
| 77 | + if (result === undefined) { |
| 78 | + return undefined; |
| 79 | + } else { |
| 80 | + return new BatchResponseContent(result); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Executes the batch requests asynchronously. |
| 86 | + * |
| 87 | + * @returns {Promise<BatchResponseContent | undefined>} A promise that resolves to the batch response content or undefined. |
| 88 | + * @throws {Error} If the batch limit is exceeded. |
| 89 | + */ |
| 90 | + public async postBatchRequestContentCollectionAsync( |
| 91 | + collection: BatchRequestContentCollection, |
| 92 | + ): Promise<BatchResponseContentCollection | undefined> { |
| 93 | + // chuck the batch requests into smaller batches |
| 94 | + const batches = collection.getBatchResponseContents(); |
| 95 | + |
| 96 | + // loop over batches and create batch request body |
| 97 | + const batchResponseBody: BatchResponseContent[] = []; |
| 98 | + for (const requestContent of batches) { |
| 99 | + const response = await requestContent.postAsync(); |
| 100 | + if (response) { |
| 101 | + batchResponseBody.push(response); |
| 102 | + } |
| 103 | + } |
| 104 | + return new BatchResponseContentCollection(batchResponseBody); |
| 105 | + } |
| 106 | +} |
0 commit comments