Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ internal PipelineMessage CreateGetTreeRequest(RequestOptions options)
return message;
}

internal PipelineMessage CreateGetTreeAsJsonRequest(RequestOptions options)
{
ClientUriBuilder uri = new ClientUriBuilder();
uri.Reset(_endpoint);
uri.AppendPath("/plants/tree/as-plant/json", false);
PipelineMessage message = Pipeline.CreateMessage(uri.ToUri(), "GET", PipelineMessageClassifier200);
PipelineRequest request = message.Request;
request.Headers.Set("Accept", "application/json");
message.Apply(options);
return message;
}

internal PipelineMessage CreateUpdateTreeRequest(BinaryContent content, RequestOptions options)
{
ClientUriBuilder uri = new ClientUriBuilder();
Expand All @@ -39,5 +51,19 @@ internal PipelineMessage CreateUpdateTreeRequest(BinaryContent content, RequestO
message.Apply(options);
return message;
}

internal PipelineMessage CreateUpdateTreeAsJsonRequest(BinaryContent content, RequestOptions options)
{
ClientUriBuilder uri = new ClientUriBuilder();
uri.Reset(_endpoint);
uri.AppendPath("/plants/tree/as-plant/json", false);
PipelineMessage message = Pipeline.CreateMessage(uri.ToUri(), "PUT", PipelineMessageClassifier200);
PipelineRequest request = message.Request;
request.Headers.Set("Content-Type", "application/json");
request.Headers.Set("Accept", "application/json");
request.Content = content;
message.Apply(options);
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,110 @@ public virtual async Task<ClientResult<Tree>> GetTreeAsync(CancellationToken can
}
}

/// <summary>
/// [Protocol Method] Get a tree as a plant
/// <list type="bullet">
/// <item>
/// <description> This <see href="https://aka.ms/azsdk/net/protocol-methods">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios. </description>
/// </item>
/// </list>
/// </summary>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual ClientResult GetTreeAsJson(RequestOptions options)
{
try
{
System.Console.WriteLine("Entering method GetTreeAsJson.");
using PipelineMessage message = CreateGetTreeAsJsonRequest(options);
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method GetTreeAsJson: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method GetTreeAsJson.");
}
}

/// <summary>
/// [Protocol Method] Get a tree as a plant
/// <list type="bullet">
/// <item>
/// <description> This <see href="https://aka.ms/azsdk/net/protocol-methods">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios. </description>
/// </item>
/// </list>
/// </summary>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual async Task<ClientResult> GetTreeAsJsonAsync(RequestOptions options)
{
try
{
System.Console.WriteLine("Entering method GetTreeAsJsonAsync.");
using PipelineMessage message = CreateGetTreeAsJsonRequest(options);
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method GetTreeAsJsonAsync: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method GetTreeAsJsonAsync.");
}
}

/// <summary> Get a tree as a plant. </summary>
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
public virtual ClientResult<Tree> GetTreeAsJson(CancellationToken cancellationToken = default)
{
try
{
System.Console.WriteLine("Entering method GetTreeAsJson.");
ClientResult result = GetTreeAsJson(cancellationToken.ToRequestOptions());
return ClientResult.FromValue((Tree)result, result.GetRawResponse());
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method GetTreeAsJson: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method GetTreeAsJson.");
}
}

/// <summary> Get a tree as a plant. </summary>
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
public virtual async Task<ClientResult<Tree>> GetTreeAsJsonAsync(CancellationToken cancellationToken = default)
{
try
{
System.Console.WriteLine("Entering method GetTreeAsJsonAsync.");
ClientResult result = await GetTreeAsJsonAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((Tree)result, result.GetRawResponse());
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method GetTreeAsJsonAsync: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method GetTreeAsJsonAsync.");
}
}

/// <summary>
/// [Protocol Method] Update a tree as a plant
/// <list type="bullet">
Expand Down Expand Up @@ -255,5 +359,125 @@ public virtual async Task<ClientResult<Tree>> UpdateTreeAsync(Tree tree, Cancell
System.Console.WriteLine("Exiting method UpdateTreeAsync.");
}
}

/// <summary>
/// [Protocol Method] Update a tree as a plant
/// <list type="bullet">
/// <item>
/// <description> This <see href="https://aka.ms/azsdk/net/protocol-methods">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios. </description>
/// </item>
/// </list>
/// </summary>
/// <param name="content"> The content to send as the body of the request. </param>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual ClientResult UpdateTreeAsJson(BinaryContent content, RequestOptions options = null)
{
try
{
System.Console.WriteLine("Entering method UpdateTreeAsJson.");
Argument.AssertNotNull(content, nameof(content));

using PipelineMessage message = CreateUpdateTreeAsJsonRequest(content, options);
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method UpdateTreeAsJson: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method UpdateTreeAsJson.");
}
}

/// <summary>
/// [Protocol Method] Update a tree as a plant
/// <list type="bullet">
/// <item>
/// <description> This <see href="https://aka.ms/azsdk/net/protocol-methods">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios. </description>
/// </item>
/// </list>
/// </summary>
/// <param name="content"> The content to send as the body of the request. </param>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual async Task<ClientResult> UpdateTreeAsJsonAsync(BinaryContent content, RequestOptions options = null)
{
try
{
System.Console.WriteLine("Entering method UpdateTreeAsJsonAsync.");
Argument.AssertNotNull(content, nameof(content));

using PipelineMessage message = CreateUpdateTreeAsJsonRequest(content, options);
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method UpdateTreeAsJsonAsync: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method UpdateTreeAsJsonAsync.");
}
}

/// <summary> Update a tree as a plant. </summary>
/// <param name="tree"></param>
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
/// <exception cref="ArgumentNullException"> <paramref name="tree"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
public virtual ClientResult<Tree> UpdateTreeAsJson(Tree tree, CancellationToken cancellationToken = default)
{
try
{
System.Console.WriteLine("Entering method UpdateTreeAsJson.");
Argument.AssertNotNull(tree, nameof(tree));

ClientResult result = UpdateTreeAsJson(tree, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((Tree)result, result.GetRawResponse());
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method UpdateTreeAsJson: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method UpdateTreeAsJson.");
}
}

/// <summary> Update a tree as a plant. </summary>
/// <param name="tree"></param>
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
/// <exception cref="ArgumentNullException"> <paramref name="tree"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
public virtual async Task<ClientResult<Tree>> UpdateTreeAsJsonAsync(Tree tree, CancellationToken cancellationToken = default)
{
try
{
System.Console.WriteLine("Entering method UpdateTreeAsJsonAsync.");
Argument.AssertNotNull(tree, nameof(tree));

ClientResult result = await UpdateTreeAsJsonAsync(tree, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((Tree)result, result.GetRawResponse());
}
catch (Exception ex)
{
System.Console.WriteLine($"An exception was thrown in method UpdateTreeAsJsonAsync: {ex}");
throw;
}
finally
{
System.Console.WriteLine("Exiting method UpdateTreeAsJsonAsync.");
}
}
}
}
17 changes: 16 additions & 1 deletion docs/samples/client/csharp/SampleService/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,6 @@ model Plant {
}

@doc("Tree is a specific type of plant")
@usage(Usage.xml | Usage.output | Usage.json)
model Tree extends Plant {
species: "tree";

Expand All @@ -809,13 +808,29 @@ interface PlantOperations {
@body body: Tree;
};

@doc("Get a tree as a plant")
@get
@route("/tree/as-plant/json")
getTreeAsJson(): {
@header contentType: "application/json";
@body body: Tree;
};

@doc("Update a tree as a plant")
@put
@route("/tree/as-plant")
updateTree(@body tree: Tree, @header contentType: "application/xml"): {
@header contentType: "application/xml";
@body body: Tree;
};

@doc("Update a tree as a plant")
@put
@route("/tree/as-plant/json")
updateTreeAsJson(@body tree: Tree, @header contentType: "application/json"): {
@header contentType: "application/json";
@body body: Tree;
};
}

@clientInitialization({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ protected override MethodProvider[] BuildMethods()
if (ScmCodeModelGenerator.Instance.TypeFactory.RootInputModels.Contains(_inputModel))
{
methods.Add(BuildImplicitToBinaryContent());
// Add internal ToBinaryContent helper for format-specific serialization
if (_supportsJson && _supportsXml)
{
methods.Add(BuildToBinaryContentMethod());
}
}

if (ScmCodeModelGenerator.Instance.TypeFactory.RootOutputModels.Contains(_inputModel))
Expand Down Expand Up @@ -326,6 +331,23 @@ private MethodProvider BuildImplicitToBinaryContent()
this);
}

private MethodProvider BuildToBinaryContentMethod()
{
var formatParameter = new ParameterProvider("format", $"The format to use for serialization", typeof(string));

// ModelReaderWriterOptions options = new ModelReaderWriterOptions(format);
// return BinaryContent.Create(this, options);
var requestContentType = ScmCodeModelGenerator.Instance.TypeFactory.RequestContentApi.RequestContentType;
return new MethodProvider(
new MethodSignature($"To{requestContentType.Name}", FormattableStringHelpers.FromString($"Converts the model to {requestContentType.Name} using the specified format"), MethodSignatureModifiers.Internal, requestContentType, null, [formatParameter]),
new MethodBodyStatement[]
{
Declare("options", typeof(ModelReaderWriterOptions), New.Instance(typeof(ModelReaderWriterOptions), formatParameter), out var options),
Return(RequestContentApiSnippets.Create(This, options.As<ModelReaderWriterOptions>()))
},
this);
}

/// <summary>
/// Builds the types that the model type serialization implements.
/// </summary>
Expand Down
Loading
Loading