Skip to content

Commit

Permalink
mpfd apis proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerangel-msft committed Mar 7, 2025
1 parent eab347f commit 42e331f
Show file tree
Hide file tree
Showing 19 changed files with 956 additions and 14 deletions.
17 changes: 17 additions & 0 deletions sdk/core/System.ClientModel/api/System.ClientModel.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public partial interface IPersistableModel<out T>
string GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options);
System.BinaryData Write(System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial interface IStreamModel<out T> : System.ClientModel.Primitives.IPersistableModel<T>
{
void Write(System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial class JsonModelConverter : System.Text.Json.Serialization.JsonConverter<System.ClientModel.Primitives.IJsonModel<object>>
{
public JsonModelConverter() { }
Expand Down Expand Up @@ -247,8 +251,12 @@ public static partial class ModelReaderWriter
public static T? Read<T>(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IStreamModel<T> { }
}
public abstract partial class ModelReaderWriterContext
{
Expand All @@ -262,6 +270,15 @@ public ModelReaderWriterOptions(string format) { }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Json { get { throw null; } }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Xml { get { throw null; } }
}
public partial class MultiPartFile
{
public MultiPartFile(System.BinaryData contents, string? filename = null, string? contentType = null) { }
public MultiPartFile(System.IO.Stream contents, string? filename = null, string? contentType = null) { }
public System.BinaryData? Contents { get { throw null; } }
public string ContentType { get { throw null; } }
public System.IO.Stream? File { get { throw null; } }
public string? Filename { get { throw null; } }
}
public abstract partial class OperationResult
{
protected OperationResult(System.ClientModel.Primitives.PipelineResponse response) { }
Expand Down
17 changes: 17 additions & 0 deletions sdk/core/System.ClientModel/api/System.ClientModel.net8.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public partial interface IPersistableModel<out T>
string GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options);
System.BinaryData Write(System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial interface IStreamModel<out T> : System.ClientModel.Primitives.IPersistableModel<T>
{
void Write(System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial class JsonModelConverter : System.Text.Json.Serialization.JsonConverter<System.ClientModel.Primitives.IJsonModel<object>>
{
public JsonModelConverter() { }
Expand Down Expand Up @@ -247,8 +251,12 @@ public static partial class ModelReaderWriter
public static T? Read<T>(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IStreamModel<T> { }
}
public abstract partial class ModelReaderWriterContext
{
Expand All @@ -262,6 +270,15 @@ public ModelReaderWriterOptions(string format) { }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Json { get { throw null; } }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Xml { get { throw null; } }
}
public partial class MultiPartFile
{
public MultiPartFile(System.BinaryData contents, string? filename = null, string? contentType = null) { }
public MultiPartFile(System.IO.Stream contents, string? filename = null, string? contentType = null) { }
public System.BinaryData? Contents { get { throw null; } }
public string ContentType { get { throw null; } }
public System.IO.Stream? File { get { throw null; } }
public string? Filename { get { throw null; } }
}
public abstract partial class OperationResult
{
protected OperationResult(System.ClientModel.Primitives.PipelineResponse response) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public partial interface IPersistableModel<out T>
string GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options);
System.BinaryData Write(System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial interface IStreamModel<out T> : System.ClientModel.Primitives.IPersistableModel<T>
{
void Write(System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions options);
}
public partial class JsonModelConverter : System.Text.Json.Serialization.JsonConverter<System.ClientModel.Primitives.IJsonModel<object>>
{
public JsonModelConverter() { }
Expand Down Expand Up @@ -247,8 +251,12 @@ public static partial class ModelReaderWriter
public static T? Read<T>(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write(object model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write(object model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
public static System.BinaryData Write<T>(T model, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IPersistableModel<T> { throw null; }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterContext context, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { }
public static void Write<T>(T model, System.IO.Stream stream, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) where T : System.ClientModel.Primitives.IStreamModel<T> { }
}
public abstract partial class ModelReaderWriterContext
{
Expand All @@ -262,6 +270,15 @@ public ModelReaderWriterOptions(string format) { }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Json { get { throw null; } }
public static System.ClientModel.Primitives.ModelReaderWriterOptions Xml { get { throw null; } }
}
public partial class MultiPartFile
{
public MultiPartFile(System.BinaryData contents, string? filename = null, string? contentType = null) { }
public MultiPartFile(System.IO.Stream contents, string? filename = null, string? contentType = null) { }
public System.BinaryData? Contents { get { throw null; } }
public string ContentType { get { throw null; } }
public System.IO.Stream? File { get { throw null; } }
public string? Filename { get { throw null; } }
}
public abstract partial class OperationResult
{
protected OperationResult(System.ClientModel.Primitives.PipelineResponse response) { }
Expand Down
57 changes: 57 additions & 0 deletions sdk/core/System.ClientModel/src/Convenience/MultiPartFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.ClientModel.Internal;
using System.IO;

namespace System.ClientModel.Primitives;

/// <summary>
/// A file to be uploaded as part of a multipart request.
/// </summary>
public class MultiPartFile
{
/// <summary>
/// Creates a new instance of <see cref="MultiPartFile"/>.
/// </summary>
public MultiPartFile(Stream contents, string? filename = default, string? contentType = default)
{
Argument.AssertNotNull(contents, nameof(contents));

File = contents;
Filename = filename;
ContentType = contentType ?? "application/octet-stream";
}

/// <summary>
/// Creates a new instance of <see cref="MultiPartFile"/>.
/// </summary>
public MultiPartFile(BinaryData contents, string? filename = default, string? contentType = default)
{
Argument.AssertNotNull(contents, nameof(contents));

Contents = contents;
Filename = filename;
ContentType = contentType ?? "application/octet-stream";
}

/// <summary>
/// The file stream to be uploaded as part of a multipart request.
/// </summary>
public Stream? File { get; }

/// <summary>
/// The file contents to be uploaded as part of a multipart request.
/// </summary>
public BinaryData? Contents { get; }

/// <summary>
/// The name of the file to be uploaded as part of a multipart request.
/// </summary>
public string? Filename { get; }

/// <summary>
/// The content type of the file to be uploaded as part of a multipart request.
/// </summary>
public string ContentType { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections;
using System.IO;

namespace System.ClientModel.Primitives;

Expand Down Expand Up @@ -72,4 +73,5 @@ private static object GetFirstObject(IEnumerable enumerable)
}

internal abstract BinaryData Write(IEnumerable enumerable, ModelReaderWriterOptions options);
internal abstract void WriteTo(IEnumerable enumerable, Stream stream, ModelReaderWriterOptions options);
}
22 changes: 22 additions & 0 deletions sdk/core/System.ClientModel/src/ModelReaderWriter/IStreamModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IO;

namespace System.ClientModel.Primitives;

/// <summary>
/// Allows an object to control its own writing to a <see cref="Stream"/>.
/// The format is determined by the implementer.
/// </summary>
/// <typeparam name="T">The type the model can be converted into.</typeparam>
public interface IStreamModel<out T> : IPersistableModel<T>
{
/// <summary>
/// Writes the model into the provided <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to write the model into.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
void Write(Stream stream, ModelReaderWriterOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ClientModel.Internal;
using System.Collections;
using System.IO;
using System.Text.Json;

namespace System.ClientModel.Primitives;
Expand All @@ -18,6 +19,13 @@ internal override BinaryData Write(IEnumerable enumerable, ModelReaderWriterOpti
return sequenceWriter.ExtractReader().ToBinaryData();
}

internal override void WriteTo(IEnumerable enumerable, Stream stream, ModelReaderWriterOptions options)
{
using var writer = new Utf8JsonWriter(stream);
WriteEnumerable(enumerable, writer, options);
writer.Flush();
}

private static void WriteJson(object model, Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
if (model is IJsonModel<object> jsonModel)
Expand Down
Loading

0 comments on commit 42e331f

Please sign in to comment.