Skip to content

Commit

Permalink
Allow serialization writer methods to return Futures
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Jun 27, 2024
1 parent 22ad456 commit 94cb95d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,26 @@ extension RequestInformationExtensions on RequestInformation {

/// Sets the content of the request to the provided collection of parsable
/// objects.
void setContentFromParsableCollection<T extends Parsable>(
Future<void> setContentFromParsableCollection<T extends Parsable>(
RequestAdapter requestAdapter,
String contentType,
Iterable<T> 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<T extends Parsable>(
Future<void> setContentFromParsable<T extends Parsable>(
RequestAdapter requestAdapter,
String contentType,
T item,
) {
) async {
final writer = _getSerializationWriter(requestAdapter, contentType, item);

var writtenContentType = contentType;
Expand All @@ -74,7 +75,7 @@ extension RequestInformationExtensions on RequestInformation {

headers.putIfAbsent(contentTypeHeader, () => {writtenContentType});

content = writer.getSerializedContent();
content = await writer.getSerializedContent();
}

SerializationWriter _getSerializationWriter<T>(
Expand All @@ -92,40 +93,41 @@ extension RequestInformationExtensions on RequestInformation {

/// Sets the content of the request to the provided collection of scalar
/// values.
void setContentFromScalarCollection<T>(
Future<void> setContentFromScalarCollection<T>(
RequestAdapter requestAdapter,
String contentType,
Iterable<T> 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<T>(
Future<void> setContentFromScalar<T>(
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}',
Expand All @@ -134,7 +136,7 @@ extension RequestInformationExtensions on RequestInformation {

headers.putIfAbsent(contentTypeHeader, () => {contentType});

content = writer.getSerializedContent();
content = await writer.getSerializedContent();
}

void configure<T>(void Function(RequestConfiguration)? configurator) {
Expand Down
28 changes: 16 additions & 12 deletions packages/kiota_abstractions/lib/src/multipart_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<void> _addNewLine(SerializationWriter writer) async =>
await writer.writeStringValue(null, '');
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,71 @@ 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<void> 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<void> 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<void> 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<void> 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<void> writeDateTimeValue(String? key, DateTime? value);

/// Writes the specified collection of primitive values to the buffer with an
/// optional given [key].
void writeCollectionOfPrimitiveValues<T>(String? key, Iterable<T>? values);
FutureOr<void> writeCollectionOfPrimitiveValues<T>(
String? key,
Iterable<T>? values,
);

/// Writes the specified collection of [T] object values to the buffer with an
/// optional given [key].
void writeCollectionOfObjectValues<T extends Parsable>(
FutureOr<void> writeCollectionOfObjectValues<T extends Parsable>(
String? key,
Iterable<T>? values,
);

/// Writes the specified collection of enum [T] values to the buffer with an
/// optional given [key].
void writeCollectionOfEnumValues<T extends Enum>(
FutureOr<void> writeCollectionOfEnumValues<T extends Enum>(
String? key,
Iterable<T>? 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<void> 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<T extends Parsable>(
FutureOr<void> writeObjectValue<T extends Parsable>(
String? key,
T? value, [
Iterable<Parsable>? additionalValuesToMerge,
]);

/// Writes the specified enum value to the buffer with an optional given
/// [key].
void writeEnumValue<T extends Enum>(String? key, T? value);
FutureOr<void> writeEnumValue<T extends Enum>(String? key, T? value);

/// Writes a null value for the specified [key].
void writeNullValue(String? key);
FutureOr<void> writeNullValue(String? key);

/// Writes the specified additional data to the buffer.
void writeAdditionalData(Map<String, dynamic> value);
FutureOr<void> writeAdditionalData(Map<String, dynamic> value);

/// Gets the value of the serialized content.
Uint8List getSerializedContent();
FutureOr<Uint8List> getSerializedContent();

/// Callback called before the serialization process starts.
ParsableHook? onBeforeObjectSerialization;
Expand Down

0 comments on commit 94cb95d

Please sign in to comment.