Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and add http part support fo ToDo #32

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
477 changes: 192 additions & 285 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"dependencies": {
"@azure-tools/typespec-autorest": "^0.48.0",
"@azure-tools/typespec-azure-core": "^0.48.0",
"@azure-tools/typespec-azure-resource-manager": "^0.48.0",
"@azure-tools/typespec-client-generator-core": "^0.48.4",
"@typespec/compiler": "^0.62.0",
"@typespec/http": "^0.62.0",
"@azure-tools/typespec-autorest": "^0.49.0",
"@azure-tools/typespec-azure-core": "^0.49.0",
"@azure-tools/typespec-azure-resource-manager": "^0.49.0",
"@azure-tools/typespec-client-generator-core": "^0.49.0",
"@typespec/compiler": "^0.63.0",
"@typespec/http": "^0.63.0",
"@typespec/http-client-csharp": "^0.1.9-alpha.20241211.3",
"@typespec/http-server-csharp": "^0.58.0-alpha.5",
"@typespec/http-server-javascript": "file:./pkg/typespec-http-server-javascript-0.58.0-alpha.5.tgz",
"@typespec/http-server-csharp": "^0.58.0-alpha.6",
"@typespec/http-server-javascript": "^0.58.0-alpha.6",
"@typespec/http-client-java": "^0.1.3",
"@azure-tools/typespec-ts": "^0.35.0",
"@typespec/http-client-python": "^0.4.2",
"@typespec/json-schema": "^0.62.0",
"@typespec/openapi": "^0.62.0",
"@typespec/openapi3": "^0.62.0",
"@typespec/rest": "^0.62.0",
"@typespec/versioning": "^0.62.0"
"@typespec/json-schema": "^0.63.0",
"@typespec/openapi": "^0.63.0",
"@typespec/openapi3": "^0.63.0",
"@typespec/rest": "^0.63.0",
"@typespec/versioning": "^0.63.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace Todo
{
internal partial class MultiPartFormDataBinaryContent : BinaryContent
{
private readonly MultipartFormDataContent _multipartContent;
private static readonly Random _random = new Random();
private static readonly char[] _boundaryValues = "0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray();

public MultiPartFormDataBinaryContent()
{
_multipartContent = new MultipartFormDataContent(CreateBoundary());
}

public string ContentType
{
get
{
return _multipartContent.Headers.ContentType.ToString();
}
}

internal HttpContent HttpContent => _multipartContent;

private static string CreateBoundary()
{
Span<char> chars = new char[70];
byte[] random = new byte[70];
_random.NextBytes(random);
int mask = 255 >> 2;
int i = 0;
for (; i < 70; i++)
{
chars[i] = _boundaryValues[random[i] & mask];
}
return chars.ToString();
}

public void Add(string content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

Add(new StringContent(content), name, filename, contentType);
}

public void Add(int content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content.ToString("G", CultureInfo.InvariantCulture);
Add(new StringContent(value), name, filename, contentType);
}

public void Add(long content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content.ToString("G", CultureInfo.InvariantCulture);
Add(new StringContent(value), name, filename, contentType);
}

public void Add(float content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content.ToString("G", CultureInfo.InvariantCulture);
Add(new StringContent(value), name, filename, contentType);
}

public void Add(double content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content.ToString("G", CultureInfo.InvariantCulture);
Add(new StringContent(value), name, filename, contentType);
}

public void Add(decimal content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content.ToString("G", CultureInfo.InvariantCulture);
Add(new StringContent(value), name, filename, contentType);
}

public void Add(bool content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

string value = content ? "true" : "false";
Add(new StringContent(value), name, filename, contentType);
}

public void Add(Stream content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

Add(new StreamContent(content), name, filename, contentType);
}

public void Add(byte[] content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

Add(new ByteArrayContent(content), name, filename, contentType);
}

public void Add(BinaryData content, string name, string filename = default, string contentType = default)
{
Argument.AssertNotNull(content, nameof(content));
Argument.AssertNotNullOrEmpty(name, nameof(name));

Add(new ByteArrayContent(content.ToArray()), name, filename, contentType);
}

private void Add(HttpContent content, string name, string filename, string contentType)
{
if (contentType != null)
{
Argument.AssertNotNullOrEmpty(contentType, nameof(contentType));
AddContentTypeHeader(content, contentType);
}
if (filename != null)
{
Argument.AssertNotNullOrEmpty(filename, nameof(filename));
_multipartContent.Add(content, name, filename);
}
else
{
_multipartContent.Add(content, name);
}
}

public static void AddContentTypeHeader(HttpContent content, string contentType)
{
MediaTypeHeaderValue header = new MediaTypeHeaderValue(contentType);
content.Headers.ContentType = header;
}

public override bool TryComputeLength(out long length)
{
if (_multipartContent.Headers.ContentLength is long contentLength)
{
length = contentLength;
return true;
}
length = 0;
return false;
}

public override void WriteTo(Stream stream, CancellationToken cancellationToken = default)
{
#if NET6_0_OR_GREATER
_multipartContent.CopyTo(stream, default, cancellationToken);
#else
_multipartContent.CopyToAsync(stream).GetAwaiter().GetResult();
#endif
}

public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default)
{
#if NET6_0_OR_GREATER
await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);
#else
await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);
#endif
}

public override void Dispose()
{
_multipartContent.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
namespace Todo.Models
{
/// <summary></summary>
internal partial class CreateRequest : IJsonModel<CreateRequest>
internal partial class CreateJsonRequest : IJsonModel<CreateJsonRequest>
{
internal CreateRequest()
internal CreateJsonRequest()
{
}

void IJsonModel<CreateRequest>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
void IJsonModel<CreateJsonRequest>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
JsonModelWriteCore(writer, options);
Expand All @@ -29,10 +29,10 @@ void IJsonModel<CreateRequest>.Write(Utf8JsonWriter writer, ModelReaderWriterOpt
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<CreateRequest>)this).GetFormatFromOptions(options) : options.Format;
string format = options.Format == "W" ? ((IPersistableModel<CreateJsonRequest>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(CreateRequest)} does not support writing '{format}' format.");
throw new FormatException($"The model {nameof(CreateJsonRequest)} does not support writing '{format}' format.");
}
writer.WritePropertyName("item"u8);
writer.WriteObjectValue(Item, options);
Expand Down Expand Up @@ -75,22 +75,22 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
}
}

CreateRequest IJsonModel<CreateRequest>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
CreateJsonRequest IJsonModel<CreateJsonRequest>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);

/// <param name="reader"> The JSON reader. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual CreateRequest JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
protected virtual CreateJsonRequest JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<CreateRequest>)this).GetFormatFromOptions(options) : options.Format;
string format = options.Format == "W" ? ((IPersistableModel<CreateJsonRequest>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(CreateRequest)} does not support reading '{format}' format.");
throw new FormatException($"The model {nameof(CreateJsonRequest)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeCreateRequest(document.RootElement, options);
return DeserializeCreateJsonRequest(document.RootElement, options);
}

internal static CreateRequest DeserializeCreateRequest(JsonElement element, ModelReaderWriterOptions options)
internal static CreateJsonRequest DeserializeCreateJsonRequest(JsonElement element, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
Expand Down Expand Up @@ -132,61 +132,61 @@ internal static CreateRequest DeserializeCreateRequest(JsonElement element, Mode
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
}
}
return new CreateRequest(item, attachments ?? new ChangeTrackingList<BinaryData>(), additionalBinaryDataProperties);
return new CreateJsonRequest(item, attachments ?? new ChangeTrackingList<BinaryData>(), additionalBinaryDataProperties);
}

BinaryData IPersistableModel<CreateRequest>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
BinaryData IPersistableModel<CreateJsonRequest>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

/// <param name="options"> The client options for reading and writing models. </param>
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<CreateRequest>)this).GetFormatFromOptions(options) : options.Format;
string format = options.Format == "W" ? ((IPersistableModel<CreateJsonRequest>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options);
default:
throw new FormatException($"The model {nameof(CreateRequest)} does not support writing '{options.Format}' format.");
throw new FormatException($"The model {nameof(CreateJsonRequest)} does not support writing '{options.Format}' format.");
}
}

CreateRequest IPersistableModel<CreateRequest>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
CreateJsonRequest IPersistableModel<CreateJsonRequest>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);

/// <param name="data"> The data to parse. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual CreateRequest PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
protected virtual CreateJsonRequest PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<CreateRequest>)this).GetFormatFromOptions(options) : options.Format;
string format = options.Format == "W" ? ((IPersistableModel<CreateJsonRequest>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
using (JsonDocument document = JsonDocument.Parse(data))
{
return DeserializeCreateRequest(document.RootElement, options);
return DeserializeCreateJsonRequest(document.RootElement, options);
}
default:
throw new FormatException($"The model {nameof(CreateRequest)} does not support reading '{options.Format}' format.");
throw new FormatException($"The model {nameof(CreateJsonRequest)} does not support reading '{options.Format}' format.");
}
}

string IPersistableModel<CreateRequest>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
string IPersistableModel<CreateJsonRequest>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";

/// <param name="createRequest"> The <see cref="CreateRequest"/> to serialize into <see cref="BinaryContent"/>. </param>
public static implicit operator BinaryContent(CreateRequest createRequest)
/// <param name="createJsonRequest"> The <see cref="CreateJsonRequest"/> to serialize into <see cref="BinaryContent"/>. </param>
public static implicit operator BinaryContent(CreateJsonRequest createJsonRequest)
{
if (createRequest == null)
if (createJsonRequest == null)
{
return null;
}
return BinaryContent.Create(createRequest, ModelSerializationExtensions.WireOptions);
return BinaryContent.Create(createJsonRequest, ModelSerializationExtensions.WireOptions);
}

/// <param name="result"> The <see cref="ClientResult"/> to deserialize the <see cref="CreateRequest"/> from. </param>
public static explicit operator CreateRequest(ClientResult result)
/// <param name="result"> The <see cref="ClientResult"/> to deserialize the <see cref="CreateJsonRequest"/> from. </param>
public static explicit operator CreateJsonRequest(ClientResult result)
{
using PipelineResponse response = result.GetRawResponse();
using JsonDocument document = JsonDocument.Parse(response.Content);
return DeserializeCreateRequest(document.RootElement, ModelSerializationExtensions.WireOptions);
return DeserializeCreateJsonRequest(document.RootElement, ModelSerializationExtensions.WireOptions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

namespace Todo.Models
{
/// <summary> The CreateRequest. </summary>
internal partial class CreateRequest
/// <summary> The CreateJsonRequest. </summary>
internal partial class CreateJsonRequest
{
/// <summary> Keeps track of any properties unknown to the library. </summary>
private protected readonly IDictionary<string, BinaryData> _additionalBinaryDataProperties;

internal CreateRequest(TodoItem item)
internal CreateJsonRequest(TodoItem item)
{
Item = item;
Attachments = new ChangeTrackingList<BinaryData>();
}

internal CreateRequest(TodoItem item, IList<BinaryData> attachments, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal CreateJsonRequest(TodoItem item, IList<BinaryData> attachments, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
Item = item;
Attachments = attachments;
Expand Down
Loading