From 94cb95d41b3a29b9de49446e362f152b6afdcf46 Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Wed, 26 Jun 2024 20:30:38 +0200 Subject: [PATCH] Allow serialization writer methods to return Futures --- .../request_information_extensions.dart | 46 ++++++++++--------- .../lib/src/multipart_body.dart | 28 ++++++----- .../serialization/serialization_writer.dart | 31 +++++++------ 3 files changed, 57 insertions(+), 48 deletions(-) diff --git a/packages/kiota_abstractions/lib/src/extensions/request_information_extensions.dart b/packages/kiota_abstractions/lib/src/extensions/request_information_extensions.dart index 88d84f2..9145549 100644 --- a/packages/kiota_abstractions/lib/src/extensions/request_information_extensions.dart +++ b/packages/kiota_abstractions/lib/src/extensions/request_information_extensions.dart @@ -43,25 +43,26 @@ extension RequestInformationExtensions on RequestInformation { /// Sets the content of the request to the provided collection of parsable /// objects. - void setContentFromParsableCollection( + Future setContentFromParsableCollection( RequestAdapter requestAdapter, String contentType, Iterable items, - ) { - final writer = _getSerializationWriter(requestAdapter, contentType, items) - ..writeCollectionOfObjectValues(null, items); + ) async { + final writer = _getSerializationWriter(requestAdapter, contentType, items); + + await writer.writeCollectionOfObjectValues(null, items); headers.putIfAbsent(contentTypeHeader, () => {contentType}); - content = writer.getSerializedContent(); + content = await writer.getSerializedContent(); } /// Sets the content of the request to the provided parsable object. - void setContentFromParsable( + Future setContentFromParsable( RequestAdapter requestAdapter, String contentType, T item, - ) { + ) async { final writer = _getSerializationWriter(requestAdapter, contentType, item); var writtenContentType = contentType; @@ -74,7 +75,7 @@ extension RequestInformationExtensions on RequestInformation { headers.putIfAbsent(contentTypeHeader, () => {writtenContentType}); - content = writer.getSerializedContent(); + content = await writer.getSerializedContent(); } SerializationWriter _getSerializationWriter( @@ -92,40 +93,41 @@ extension RequestInformationExtensions on RequestInformation { /// Sets the content of the request to the provided collection of scalar /// values. - void setContentFromScalarCollection( + Future setContentFromScalarCollection( RequestAdapter requestAdapter, String contentType, Iterable items, - ) { - final writer = _getSerializationWriter(requestAdapter, contentType, items) - ..writeCollectionOfPrimitiveValues(null, items); + ) async { + final writer = _getSerializationWriter(requestAdapter, contentType, items); + + await writer.writeCollectionOfPrimitiveValues(null, items); headers.putIfAbsent(contentTypeHeader, () => {contentType}); - content = writer.getSerializedContent(); + content = await writer.getSerializedContent(); } /// Sets the content of the request to the provided scalar value. - void setContentFromScalar( + Future setContentFromScalar( RequestAdapter requestAdapter, String contentType, T item, - ) { + ) async { final writer = _getSerializationWriter(requestAdapter, contentType, item); switch (item) { case final String s: - writer.writeStringValue(null, s); + await writer.writeStringValue(null, s); case final bool b: - writer.writeBoolValue(null, value: b); + await writer.writeBoolValue(null, value: b); case final int i: - writer.writeIntValue(null, i); + await writer.writeIntValue(null, i); case final double d: - writer.writeDoubleValue(null, d); + await writer.writeDoubleValue(null, d); case final DateTime t: - writer.writeDateTimeValue(null, t); + await writer.writeDateTimeValue(null, t); case null: - writer.writeNullValue(null); + await writer.writeNullValue(null); default: throw UnsupportedError( 'Unsupported scalar value type: ${item.runtimeType}', @@ -134,7 +136,7 @@ extension RequestInformationExtensions on RequestInformation { headers.putIfAbsent(contentTypeHeader, () => {contentType}); - content = writer.getSerializedContent(); + content = await writer.getSerializedContent(); } void configure(void Function(RequestConfiguration)? configurator) { diff --git a/packages/kiota_abstractions/lib/src/multipart_body.dart b/packages/kiota_abstractions/lib/src/multipart_body.dart index fa71bab..379fc70 100644 --- a/packages/kiota_abstractions/lib/src/multipart_body.dart +++ b/packages/kiota_abstractions/lib/src/multipart_body.dart @@ -89,21 +89,25 @@ class MultipartBody implements Parsable { if (first) { first = false; } else { - _addNewLine(writer); + await _addNewLine(writer); } - writer - ..writeStringValue(null, '--$boundary') - ..writeStringValue('Content-Type', partContentType) - ..writeStringValue('Content-Disposition', 'form-data; name="$partKey"'); + await writer.writeStringValue(null, '--$boundary'); + await writer.writeStringValue('Content-Type', partContentType); + await writer.writeStringValue( + 'Content-Disposition', + 'form-data; name="$partKey"', + ); - _addNewLine(writer); + await _addNewLine(writer); if (partValue is Parsable) { - final partWriter = writerFactory.getSerializationWriter(partContentType) - ..writeObjectValue(null, partValue); + final partWriter = + writerFactory.getSerializationWriter(partContentType); + + await writer.writeObjectValue(null, partValue); - final partContent = partWriter.getSerializedContent(); + final partContent = await partWriter.getSerializedContent(); writer.writeByteArrayValue(null, partContent); } else if (partValue is String) { @@ -123,11 +127,11 @@ class MultipartBody implements Parsable { } } - _addNewLine(writer); + await _addNewLine(writer); writer.writeStringValue(null, '--$boundary--'); } - void _addNewLine(SerializationWriter writer) => - writer.writeStringValue(null, ''); + Future _addNewLine(SerializationWriter writer) async => + await writer.writeStringValue(null, ''); } diff --git a/packages/kiota_abstractions/lib/src/serialization/serialization_writer.dart b/packages/kiota_abstractions/lib/src/serialization/serialization_writer.dart index b389640..b55191a 100644 --- a/packages/kiota_abstractions/lib/src/serialization/serialization_writer.dart +++ b/packages/kiota_abstractions/lib/src/serialization/serialization_writer.dart @@ -4,51 +4,54 @@ part of '../../kiota_abstractions.dart'; abstract class SerializationWriter { /// Writes the specified string value to the buffer with an optional given /// [key]. - void writeStringValue(String? key, String? value); + FutureOr writeStringValue(String? key, String? value); /// Writes the specified int value to the buffer with an optional given /// [key]. - void writeBoolValue(String? key, {bool? value}); + FutureOr writeBoolValue(String? key, {bool? value}); /// Writes the specified int value to the buffer with an optional given /// [key]. - void writeIntValue(String? key, int? value); + FutureOr writeIntValue(String? key, int? value); /// Writes the specified double value to the buffer with an optional given /// [key]. - void writeDoubleValue(String? key, double? value); + FutureOr writeDoubleValue(String? key, double? value); /// Writes the specified [DateTime] value to the buffer with an optional given /// [key]. - void writeDateTimeValue(String? key, DateTime? value); + FutureOr writeDateTimeValue(String? key, DateTime? value); /// Writes the specified collection of primitive values to the buffer with an /// optional given [key]. - void writeCollectionOfPrimitiveValues(String? key, Iterable? values); + FutureOr writeCollectionOfPrimitiveValues( + String? key, + Iterable? values, + ); /// Writes the specified collection of [T] object values to the buffer with an /// optional given [key]. - void writeCollectionOfObjectValues( + FutureOr writeCollectionOfObjectValues( String? key, Iterable? values, ); /// Writes the specified collection of enum [T] values to the buffer with an /// optional given [key]. - void writeCollectionOfEnumValues( + FutureOr writeCollectionOfEnumValues( String? key, Iterable? values, ); /// Writes the specified byte list as a base64 string to the buffer with an /// optional given [key]. - void writeByteArrayValue(String? key, Uint8List? value); + FutureOr writeByteArrayValue(String? key, Uint8List? value); /// Writes the specified model object to the buffer with an optional given /// [key]. /// The [additionalValuesToMerge] parameter is used to merge additional /// values to the main object when serializing an intersection wrapper. - void writeObjectValue( + FutureOr writeObjectValue( String? key, T? value, [ Iterable? additionalValuesToMerge, @@ -56,16 +59,16 @@ abstract class SerializationWriter { /// Writes the specified enum value to the buffer with an optional given /// [key]. - void writeEnumValue(String? key, T? value); + FutureOr writeEnumValue(String? key, T? value); /// Writes a null value for the specified [key]. - void writeNullValue(String? key); + FutureOr writeNullValue(String? key); /// Writes the specified additional data to the buffer. - void writeAdditionalData(Map value); + FutureOr writeAdditionalData(Map value); /// Gets the value of the serialized content. - Uint8List getSerializedContent(); + FutureOr getSerializedContent(); /// Callback called before the serialization process starts. ParsableHook? onBeforeObjectSerialization;