protected override DynamicResponse Build()

in src/Elastic.Transport/Responses/Dynamic/DynamicResponseBuilder.cs [19:95]


	protected override DynamicResponse Build(ApiCallDetails apiCallDetails, BoundConfiguration boundConfiguration, Stream responseStream, string contentType, long contentLength) =>
		BuildCoreAsync(false, apiCallDetails, boundConfiguration, responseStream, contentType, contentLength).EnsureCompleted();

	protected override Task<DynamicResponse> BuildAsync(ApiCallDetails apiCallDetails, BoundConfiguration boundConfiguration, Stream responseStream, string contentType, long contentLength, CancellationToken cancellationToken = default) =>
		BuildCoreAsync(true, apiCallDetails, boundConfiguration, responseStream, contentType, contentLength, cancellationToken).AsTask();

	private static async ValueTask<DynamicResponse> BuildCoreAsync(bool isAsync, ApiCallDetails apiCallDetails, BoundConfiguration boundConfiguration, Stream responseStream,
		string contentType, long contentLength, CancellationToken cancellationToken = default)
	{
		DynamicResponse response;

		//if not json store the result under "body"
		if (contentType == null || !contentType.StartsWith(BoundConfiguration.DefaultContentType))
		{
			DynamicDictionary dictionary;
			string stringValue;

			if (apiCallDetails.ResponseBodyInBytes is not null)
			{
				stringValue = Encoding.UTF8.GetString(apiCallDetails.ResponseBodyInBytes);

				dictionary = new DynamicDictionary
				{
					["body"] = new DynamicValue(stringValue)
				};

				return new DynamicResponse(dictionary);
			}

#if NET8_0_OR_GREATER
			if (contentLength > -1 && contentLength <= 1_048_576)
			{
				var buffer = ArrayPool<byte>.Shared.Rent((int)contentLength);
				responseStream.ReadExactly(buffer, 0, (int)contentLength);
				stringValue = Encoding.UTF8.GetString(buffer.AsSpan(0, (int)contentLength));
				ArrayPool<byte>.Shared.Return(buffer);

				dictionary = new DynamicDictionary
				{
					["body"] = new DynamicValue(stringValue)
				};

				return new DynamicResponse(dictionary);
			}
#endif

			var sr = new StreamReader(responseStream);

			if (isAsync)
			{
				stringValue = await sr.ReadToEndAsync
			(
#if NET8_0_OR_GREATER
				cancellationToken
#endif
			).ConfigureAwait(false);
			}
			else
			{
				stringValue = sr.ReadToEnd();
			}

			dictionary = new DynamicDictionary
			{
				["body"] = new DynamicValue(stringValue)
			};

			response = new DynamicResponse(dictionary);
		}
		else
		{
			var body = LowLevelRequestResponseSerializer.Instance.Deserialize<DynamicDictionary>(responseStream);
			response = new DynamicResponse(body);
		}

		return response;
	}