diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpRequestApi.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpRequestApi.cs index 5428986201..b747d5d8bf 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpRequestApi.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpRequestApi.cs @@ -16,10 +16,9 @@ protected HttpRequestApi(CSharpType type, ValueExpression original) : base(type, { } + public abstract Type UriBuilderType { get; } public abstract MethodBodyStatement SetMethod(string httpMethod); - public abstract MethodBodyStatement SetUri(ValueExpression uri); - public abstract MethodBodyStatement SetHeaders(IReadOnlyList arguments); public abstract ValueExpression Content(); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientUriBuilderDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientUriBuilderDefinition.cs index b9ea195239..29d1cab430 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientUriBuilderDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientUriBuilderDefinition.cs @@ -98,21 +98,16 @@ protected override MethodProvider[] BuildMethods() { var methods = new List(); - if (GetBaseType() is null) - { - methods.Add(BuildResetMethod()); - methods.AddRange(BuildAppendPathMethods()); - methods.AddRange(BuildAppendPathDelimitedMethods()); - methods.AddRange(BuildAppendQueryMethods()); - methods.AddRange(BuildAppendQueryDelimitedMethods()); - methods.Add(BuildToUriMethod()); - } + methods.Add(BuildResetMethod()); + methods.AddRange(BuildAppendPathMethods()); + methods.AddRange(BuildAppendPathDelimitedMethods()); + methods.AddRange(BuildAppendQueryMethods()); + methods.AddRange(BuildAppendQueryDelimitedMethods()); + methods.Add(BuildToUriMethod()); return methods.ToArray(); } - protected override CSharpType? GetBaseType() => ClientModelPlugin.Instance.TypeFactory.ClientUriBuilderBaseType; - private MethodProvider BuildResetMethod() { var uriParameter = new ParameterProvider("uri", $"The uri.", typeof(Uri)); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineRequestProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineRequestProvider.cs index a8d36d45be..34dd0074ed 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineRequestProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineRequestProvider.cs @@ -7,6 +7,7 @@ using Microsoft.Generator.CSharp.ClientModel.Snippets; using static Microsoft.Generator.CSharp.Snippets.Snippet; using Microsoft.Generator.CSharp.Statements; +using System; namespace Microsoft.Generator.CSharp.ClientModel.Providers { @@ -19,6 +20,8 @@ public PipelineRequestProvider(ValueExpression original) : base(typeof(PipelineR private static HttpRequestApi? _instance; internal static HttpRequestApi Instance => _instance ??= new PipelineRequestProvider(Empty); + public override Type UriBuilderType => typeof(ClientUriBuilderDefinition); + public override ValueExpression Content() => Original.Property(nameof(PipelineRequest.Content)); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/RestClientProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/RestClientProvider.cs index f09fd6356b..687fa0c8ad 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/RestClientProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/RestClientProvider.cs @@ -160,7 +160,7 @@ private MethodProvider BuildCreateRequestMethod(InputOperation operation) message.ApplyResponseClassifier(classifier.ToApi()), Declare("request", message.Request().ToApi(), out HttpRequestApi request), request.SetMethod(operation.HttpMethod), - Declare("uri", New.Instance(), out ScopedApi uri), + Declare("uri", New.Instance(request.UriBuilderType), out ScopedApi uri), uri.Reset(ClientProvider.EndpointField).Terminate(), .. AppendPathParameters(uri, operation, paramMap), .. AppendQueryParameters(uri, operation, paramMap), @@ -231,7 +231,7 @@ private IEnumerable AppendHeaderParameters(HttpRequestApi r return statements; } - private static List AppendQueryParameters(ScopedApi uri, InputOperation operation, Dictionary paramMap) + private static List AppendQueryParameters(ScopedApi uri, InputOperation operation, Dictionary paramMap) { List statements = new(operation.Parameters.Count); @@ -342,7 +342,7 @@ private static IfStatement BuildQueryParameterNullCheck( return new IfStatement(valueExpression.NotEqual(Null)) { originalStatement }; } - private IEnumerable AppendPathParameters(ScopedApi uri, InputOperation operation, Dictionary paramMap) + private IEnumerable AppendPathParameters(ScopedApi uri, InputOperation operation, Dictionary paramMap) { Dictionary inputParamHash = new(operation.Parameters.ToDictionary(p => p.Name)); List statements = new(operation.Parameters.Count); @@ -356,7 +356,7 @@ private IEnumerable AppendPathParameters(ScopedApi uri, + ScopedApi uri, List statements, Dictionary inputParamHash, Dictionary paramMap, diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ScmMethodProviderCollection.cs index 04d0538fd0..5386c7e070 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -366,7 +366,7 @@ private IReadOnlyList GetProtocolMethodArguments( conversions.Add( isAsync - ? RequestOptionsSnippets.FromCancellationToken(ScmKnownParameters.CancellationToken) + ? IHttpRequestOptionsApiSnippets.FromCancellationToken(ScmKnownParameters.CancellationToken) : ScmKnownParameters.RequestOptions.PositionalReference(Null)); return conversions; } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs index a6183ce503..371cc00c69 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs @@ -23,8 +23,6 @@ public class ScmTypeFactory : TypeFactory public virtual CSharpType TokenCredentialType => throw new NotImplementedException("Token credential is not supported in Scm libraries yet"); - public virtual CSharpType? ClientUriBuilderBaseType => null; - public virtual IClientResponseApi ClientResponseApi => ClientResultProvider.Instance; public virtual IHttpResponseApi HttpResponseApi => PipelineResponseProvider.Instance; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/ClientUriBuilderSnippets.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/ClientUriBuilderSnippets.cs index 844745126f..25926d7968 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/ClientUriBuilderSnippets.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/ClientUriBuilderSnippets.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using System; -using Microsoft.Generator.CSharp.ClientModel.Providers; using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Snippets; using static Microsoft.Generator.CSharp.Snippets.Snippet; @@ -11,25 +10,25 @@ namespace Microsoft.Generator.CSharp.ClientModel.Snippets { internal static class ClientUriBuilderSnippets { - public static InvokeMethodExpression Reset(this ScopedApi uriBuilder, ValueExpression baseUri) + public static InvokeMethodExpression Reset(this ScopedApi uriBuilder, ValueExpression baseUri) => uriBuilder.Invoke("Reset", baseUri); - public static InvokeMethodExpression AppendPath(this ScopedApi uriBuilder, ValueExpression path, bool? shouldEscape) + public static InvokeMethodExpression AppendPath(this ScopedApi uriBuilder, ValueExpression path, bool? shouldEscape) => uriBuilder.Invoke("AppendPath", path, Literal(shouldEscape)); - public static InvokeMethodExpression AppendPathDelimited(this ScopedApi uriBuilder, ValueExpression path, string? format, bool? shouldEscape, string? delimiter = ",") - => uriBuilder.Invoke("AppendPathDelimited", [path, Literal(delimiter), Literal(format), Literal(shouldEscape)]); + public static InvokeMethodExpression AppendPathDelimited(this ScopedApi uriBuilder, ValueExpression path, string? format, bool? shouldEscape, string? delimiter = ",") + => uriBuilder.Invoke("AppendPathDelimited", [path, Literal(delimiter), Literal(format), Literal(shouldEscape)]); - public static InvokeMethodExpression AppendQuery(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, bool shouldEscape) + public static InvokeMethodExpression AppendQuery(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, bool shouldEscape) => uriBuilder.Invoke("AppendQuery", [name, value, Literal(shouldEscape)]); - public static InvokeMethodExpression AppendQuery(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, string? format, bool shouldEscape) + public static InvokeMethodExpression AppendQuery(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, string? format, bool shouldEscape) => uriBuilder.Invoke("AppendQuery", [name, value, Literal(format), Literal(shouldEscape)]); - public static InvokeMethodExpression AppendQueryDelimited(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, string? format, bool shouldEscape, string? delimiter = ",") + public static InvokeMethodExpression AppendQueryDelimited(this ScopedApi uriBuilder, ValueExpression name, ValueExpression value, string? format, bool shouldEscape, string? delimiter = ",") => uriBuilder.Invoke("AppendQueryDelimited", [name, value, Literal(delimiter), Literal(format), Literal(shouldEscape)]); - public static ScopedApi ToUri(this ScopedApi uriBuilder) + public static ScopedApi ToUri(this ScopedApi uriBuilder) => uriBuilder.Invoke("ToUri").As(); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/RequestOptionsSnippets.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/IHttpRequestOptionsApiSnippets.cs similarity index 63% rename from packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/RequestOptionsSnippets.cs rename to packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/IHttpRequestOptionsApiSnippets.cs index 47747e6427..aa9ae331e9 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/RequestOptionsSnippets.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Snippets/IHttpRequestOptionsApiSnippets.cs @@ -4,25 +4,26 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Threading; +using Microsoft.Generator.CSharp.ClientModel.Providers; using Microsoft.Generator.CSharp.Expressions; -using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Snippets; using static Microsoft.Generator.CSharp.Snippets.Snippet; namespace Microsoft.Generator.CSharp.ClientModel.Snippets { - internal static class RequestOptionsSnippets + internal static class IHttpRequestOptionsApiSnippets { - public static ScopedApi FromCancellationToken(ValueExpression cancellationToken) + public static ScopedApi FromCancellationToken(ValueExpression cancellationToken) => new TernaryConditionalExpression( cancellationToken.Property(nameof(CancellationToken.CanBeCanceled)), - New.Instance( + New.Instance( + ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.HttpRequestOptionsType, arguments: [], properties: new Dictionary { { new MemberExpression(null, nameof(RequestOptions.CancellationToken)), cancellationToken } }, useSingleLineForPropertyInitialization: true), - Null).As(); + Null).As(); - public static ValueExpression ErrorOptions(this ScopedApi requestOptions) => requestOptions.Property(nameof(RequestOptions.ErrorOptions)); + public static ValueExpression ErrorOptions(this ScopedApi requestOptions) => requestOptions.Property(nameof(RequestOptions.ErrorOptions)); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.DeclarationStatements.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.DeclarationStatements.cs index b0e3a5ec75..012341b1ef 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.DeclarationStatements.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.DeclarationStatements.cs @@ -37,9 +37,10 @@ public static MethodBodyStatement Declare(string name, DictionaryExpression valu public static MethodBodyStatement Declare(string name, ScopedApi value, out ScopedApi variable) { - var declaration = new VariableExpression(value.Type, name); - variable = declaration.As(value.Type); - return Declare(declaration, value); + var declaration = new CodeWriterDeclaration(name); + var variableExpression = new VariableExpression(TypeReferenceExpression.GetTypeFromDefinition(value.Type)!, declaration); + variable = variableExpression.As(value.Type); + return Declare(variableExpression, value); } public static MethodBodyStatement Declare(VariableExpression variable, ValueExpression value) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.New.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.New.cs index 76cfcaa1fc..7a4ae2c75b 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.New.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.New.cs @@ -73,8 +73,10 @@ public static DictionaryExpression Dictionary(CSharpType keyType, CSharpType val public static ValueExpression Instance(CSharpType type, IReadOnlyList arguments) => new NewInstanceExpression(type, arguments); public static ValueExpression Instance(CSharpType type, params ValueExpression[] arguments) => new NewInstanceExpression(type, arguments); public static ValueExpression Instance(CSharpType type, IReadOnlyDictionary properties) => new NewInstanceExpression(type, [], new ObjectInitializerExpression(properties)); - public static ScopedApi Instance(Type type, params ValueExpression[] arguments) => new NewInstanceExpression(type, arguments).As(type); + public static ScopedApi Instance(Type type, params ValueExpression[] arguments) => new NewInstanceExpression(TypeReferenceExpression.GetTypeFromDefinition(type), arguments).As(type); public static ScopedApi Instance(Type type, IReadOnlyDictionary properties) => new NewInstanceExpression(type, [], new ObjectInitializerExpression(properties)).As(type); + public static ScopedApi Instance(CSharpType type, IEnumerable arguments, IReadOnlyDictionary properties, bool useSingleLineForPropertyInitialization = false) + => new NewInstanceExpression(TypeReferenceExpression.GetTypeFromDefinition(type), [.. arguments], new ObjectInitializerExpression(properties, useSingleLineForPropertyInitialization)).As(type); public static ScopedApi Instance(IEnumerable arguments, IReadOnlyDictionary properties, bool useSingleLineForPropertyInitialization = false) => new NewInstanceExpression(TypeReferenceExpression.GetTypeFromDefinition(typeof(T)), [.. arguments], new ObjectInitializerExpression(properties, useSingleLineForPropertyInitialization)).As(); public static ScopedApi Instance(params ValueExpression[] arguments)