Skip to content

Commit

Permalink
Added XXXWithHttpMessages() method
Browse files Browse the repository at this point in the history
  • Loading branch information
stankovski committed May 27, 2016
1 parent f427898 commit e4be13a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="SyncWrapperGenerationMode.cs" />
<Compile Include="TemplateModels\ExtensionsTemplateModel.cs" />
<Compile Include="TemplateModels\MethodGroupTemplateModel.cs" />
<Compile Include="TemplateModels\EnumTemplateModel.cs" />
Expand Down
16 changes: 16 additions & 0 deletions AutoRest/Generators/CSharp/CSharp/SyncWrapperGenerationMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.


namespace Microsoft.Rest.Generator.CSharp
{
/// <summary>
/// Defines supported modes for sync wrapper generation
/// </summary>
public enum SyncWrapperGenerationMode
{
All = 0,
Essential = 1,
None = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,39 @@ public virtual string GetAsyncMethodParameterDeclaration()
}

/// <summary>
/// Generate the method parameter declaration for async methods and extensions
/// Generate the method parameter declaration for sync methods and extensions
/// </summary>
/// <param name="addCustomHeaderParameters">If true add the customHeader to the parameters</param>
/// <returns>Generated string of parameters</returns>
public virtual string GetAsyncMethodParameterDeclaration(bool addCustomHeaderParameters)
public virtual string GetSyncMethodParameterDeclaration(bool addCustomHeaderParameters)
{
var declarations = this.SyncMethodParameterDeclaration;

if (!string.IsNullOrEmpty(declarations))
if (!string.IsNullOrEmpty(declarations) && addCustomHeaderParameters)
{
declarations += ", ";
}
if (addCustomHeaderParameters)
{
declarations += "Dictionary<string, List<string>> customHeaders = null, ";
declarations += "Dictionary<string, List<string>> customHeaders = null";
}

return declarations;
}

/// <summary>
/// Generate the method parameter declaration for async methods and extensions
/// </summary>
/// <param name="addCustomHeaderParameters">If true add the customHeader to the parameters</param>
/// <returns>Generated string of parameters</returns>
public virtual string GetAsyncMethodParameterDeclaration(bool addCustomHeaderParameters)
{
var declarations = this.GetSyncMethodParameterDeclaration(addCustomHeaderParameters);

if (!string.IsNullOrEmpty(declarations))
{
declarations += ", ";
}
declarations += "CancellationToken cancellationToken = default(CancellationToken)";

return declarations;
Expand All @@ -129,12 +146,12 @@ public string SyncMethodInvocationArgs
/// <summary>
/// Get the invocation args for an invocation with an async method
/// </summary>
public string GetAsyncMethodInvocationArgs (string customHeaderReference)
public string GetAsyncMethodInvocationArgs (string customHeaderReference, string cancellationTokenReference = "cancellationToken")
{
List<string> invocationParams = new List<string>();
LocalParameters.ForEach(p => invocationParams.Add(p.Name));
invocationParams.Add(customHeaderReference);
invocationParams.Add("cancellationToken");
invocationParams.Add(cancellationTokenReference);
return string.Join(", ", invocationParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ foreach (var parameter in Model.LocalParameters)
@:Task.Factory.StartNew(s => ((I@(Model.MethodGroupName))s).@(Model.Name)Async(@(Model.SyncMethodInvocationArgs)), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
}
@:}

@EmptyLine
@if (!String.IsNullOrEmpty(Model.Description) || !String.IsNullOrEmpty(Model.Summary))
{
Expand Down Expand Up @@ -93,5 +94,33 @@ foreach (var parameter in Model.LocalParameters)
@:await operations.@(Model.Name)WithHttpMessagesAsync(@(Model.GetAsyncMethodInvocationArgs("null"))).ConfigureAwait(false);
}
@:}

@EmptyLine
@if (!String.IsNullOrEmpty(Model.Description) || !String.IsNullOrEmpty(Model.Summary))
{
@:/// <summary>
@:@WrapComment("/// ", String.IsNullOrEmpty(Model.Summary) ? Model.Description.EscapeXmlComment() : Model.Summary.EscapeXmlComment())
@:/// </summary>
}
@if (!String.IsNullOrEmpty(Model.Description) && !String.IsNullOrEmpty(Model.Summary))
{
@:@WrapComment("/// ", Model.Description.EscapeXmlComment())
}
@:/// <param name='operations'>
@:/// The operations group for this extension method.
@:/// </param>
foreach (var parameter in Model.LocalParameters)
{
@:/// <param name='@parameter.Name'>
@:@WrapComment("/// ", parameter.Documentation.EscapeXmlComment())
@:/// </param>
}
@:/// <param name='customHeaders'>
@:/// Headers that will be added to request.
@:/// </param>
@:public static @Model.OperationResponseReturnTypeString @(Model.Name)WithHttpMessages(@Model.GetExtensionParameters(Model.GetSyncMethodParameterDeclaration(true)))
@:{
@: return operations.@(Model.Name)WithHttpMessagesAsync(@(Model.GetAsyncMethodInvocationArgs("customHeaders", "CancellationToken.None"))).ConfigureAwait(false).GetAwaiter().GetResult();
@:}
@:
}

0 comments on commit e4be13a

Please sign in to comment.