|
| 1 | +from typing import TypeVar, Type, Dict, Optional, Union |
| 2 | +import logging |
| 3 | + |
| 4 | +from kiota_abstractions.request_adapter import RequestAdapter |
| 5 | +from kiota_abstractions.request_information import RequestInformation |
| 6 | +from kiota_abstractions.method import Method |
| 7 | +from kiota_abstractions.serialization import Parsable |
| 8 | +from kiota_abstractions.headers_collection import HeadersCollection |
| 9 | +from kiota_abstractions.api_error import APIError |
| 10 | + |
| 11 | +from .batch_request_content import BatchRequestContent |
| 12 | +from .batch_request_content_collection import BatchRequestContentCollection |
| 13 | +from .batch_response_content import BatchResponseContent |
| 14 | +from .batch_response_content_collection import BatchResponseContentCollection |
| 15 | + |
| 16 | +T = TypeVar('T', bound='Parsable') |
| 17 | + |
| 18 | +APPLICATION_JSON = "application/json" |
| 19 | + |
| 20 | + |
| 21 | +class BatchRequestBuilder: |
| 22 | + """ |
| 23 | + Provides operations to call the batch method. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + request_adapter: RequestAdapter, |
| 29 | + error_map: Optional[Dict[str, Type[Parsable]]] = None |
| 30 | + ): |
| 31 | + if request_adapter is None: |
| 32 | + raise ValueError("request_adapter cannot be Null.") |
| 33 | + self._request_adapter = request_adapter |
| 34 | + self.url_template = f"{self._request_adapter.base_url}/$batch" |
| 35 | + self.error_map = error_map or {} |
| 36 | + |
| 37 | + async def post( |
| 38 | + self, |
| 39 | + batch_request_content: Union[BatchRequestContent, BatchRequestContentCollection], |
| 40 | + error_map: Optional[Dict[str, Type[Parsable]]] = None, |
| 41 | + ) -> Union[T, BatchResponseContentCollection]: |
| 42 | + """ |
| 43 | + Sends a batch request and returns the batch response content. |
| 44 | + |
| 45 | + Args: |
| 46 | + batch_request_content (Union[BatchRequestContent, |
| 47 | + BatchRequestContentCollection]): The batch request content. |
| 48 | + response_type: Optional[Type[T]] : The type to deserialize the response into. |
| 49 | + Optional[Dict[str, Type[Parsable]]] = None: |
| 50 | + Error mappings for response handling. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + Union[T, BatchResponseContentCollection]: The batch response content |
| 54 | + or the specified response type. |
| 55 | +
|
| 56 | + """ |
| 57 | + if batch_request_content is None: |
| 58 | + raise ValueError("batch_request_content cannot be Null.") |
| 59 | + response_type = BatchResponseContent |
| 60 | + |
| 61 | + if isinstance(batch_request_content, BatchRequestContent): |
| 62 | + request_info = await self.to_post_request_information(batch_request_content) |
| 63 | + bytes_content = request_info.content |
| 64 | + json_content = bytes_content.decode("utf-8") |
| 65 | + updated_str = '{"requests":' + json_content + '}' |
| 66 | + updated_bytes = updated_str.encode("utf-8") |
| 67 | + request_info.content = updated_bytes |
| 68 | + error_map = error_map or self.error_map |
| 69 | + response = None |
| 70 | + try: |
| 71 | + response = await self._request_adapter.send_async( |
| 72 | + request_info, response_type, error_map |
| 73 | + ) |
| 74 | + |
| 75 | + except APIError as e: |
| 76 | + logging.error("API Error: %s", e) |
| 77 | + raise e |
| 78 | + if response is None: |
| 79 | + raise ValueError("Failed to get a valid response from the API.") |
| 80 | + return response |
| 81 | + if isinstance(batch_request_content, BatchRequestContentCollection): |
| 82 | + batch_responses = await self._post_batch_collection(batch_request_content, error_map) |
| 83 | + return batch_responses |
| 84 | + |
| 85 | + raise ValueError("Invalid type for batch_request_content.") |
| 86 | + |
| 87 | + async def _post_batch_collection( |
| 88 | + self, |
| 89 | + batch_request_content_collection: BatchRequestContentCollection, |
| 90 | + error_map: Optional[Dict[str, Type[Parsable]]] = None, |
| 91 | + ) -> BatchResponseContentCollection: |
| 92 | + """ |
| 93 | + Sends a collection of batch requests and returns a collection of batch response contents. |
| 94 | + |
| 95 | + Args: |
| 96 | + batch_request_content_collection (BatchRequestContentCollection): The |
| 97 | + collection of batch request contents. |
| 98 | + Optional[Dict[str, Type[Parsable]]] = None: |
| 99 | + Error mappings for response handling. |
| 100 | +
|
| 101 | + Returns: |
| 102 | + BatchResponseContentCollection: The collection of batch response contents. |
| 103 | + """ |
| 104 | + if batch_request_content_collection is None: |
| 105 | + raise ValueError("batch_request_content_collection cannot be Null.") |
| 106 | + |
| 107 | + batch_responses = BatchResponseContentCollection() |
| 108 | + |
| 109 | + for batch_request_content in batch_request_content_collection.batches: |
| 110 | + request_info = await self.to_post_request_information(batch_request_content) |
| 111 | + response = await self._request_adapter.send_async( |
| 112 | + request_info, BatchResponseContent, error_map or self.error_map |
| 113 | + ) |
| 114 | + batch_responses.add_response(response) |
| 115 | + |
| 116 | + return batch_responses |
| 117 | + |
| 118 | + async def to_post_request_information( |
| 119 | + self, batch_request_content: BatchRequestContent |
| 120 | + ) -> RequestInformation: |
| 121 | + """ |
| 122 | + Creates request information for a batch POST request. |
| 123 | + |
| 124 | + Args: |
| 125 | + batch_request_content (BatchRequestContent): The batch request content. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + RequestInformation: The request information. |
| 129 | + """ |
| 130 | + |
| 131 | + if batch_request_content is None: |
| 132 | + raise ValueError("batch_request_content cannot be Null.") |
| 133 | + batch_request_items = list(batch_request_content.requests.values()) |
| 134 | + |
| 135 | + request_info = RequestInformation() |
| 136 | + request_info.http_method = Method.POST |
| 137 | + request_info.url_template = self.url_template |
| 138 | + request_info.headers = HeadersCollection() |
| 139 | + request_info.headers.try_add("Content-Type", APPLICATION_JSON) |
| 140 | + request_info.set_content_from_parsable( |
| 141 | + self._request_adapter, APPLICATION_JSON, batch_request_items |
| 142 | + ) |
| 143 | + |
| 144 | + return request_info |
0 commit comments