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

Apply async best practices in libraries #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<#@ template language="C#" #>
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
Expand All @@ -21,6 +21,7 @@ using System.Threading.Tasks;
using System.Net.Http.Formatting;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;
using <#= Configuration.Namespace#>.Models;

Expand Down Expand Up @@ -139,9 +140,9 @@ namespace <#= Configuration.Namespace#>.Interfaces

<# if (Configuration.GenerateAsyncReturnTypes == false || String.IsNullOrEmpty(concreteReturnType)) { #>
/// <returns></returns>
Task<HttpResponseMessage> <#= method.Name #>Async(<#= parameterList#>);
Task<HttpResponseMessage> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken));
<# } else { #>
Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#>);
Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken));
<# } #>

<# foreach(var p in method.UrlParameters) {#>
Expand Down Expand Up @@ -379,9 +380,9 @@ namespace <#= Configuration.Namespace#>.Clients
/// <param name="<#= p.Name #>"><#= p.Description.ToSummary() #></param>
<# } #>
/// <returns></returns>
protected virtual async Task<HttpResponseMessage> <#= method.Name #>AsyncMsg(<#= parameterList#>)
protected virtual async Task<HttpResponseMessage> <#= method.Name #>AsyncMsg(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken))
{
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>);
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false);
}

<# if (Configuration.GenerateAsyncReturnTypes == false || String.IsNullOrEmpty(concreteReturnType)) { #>
Expand All @@ -392,9 +393,9 @@ namespace <#= Configuration.Namespace#>.Clients
/// <param name="<#= p.Name #>"><#= p.Description.ToSummary() #></param>
<# } #>
/// <returns></returns>
public virtual async Task<HttpResponseMessage> <#= method.Name #>Async(<#= parameterList#>)
public virtual async Task<HttpResponseMessage> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken))
{
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>);
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false);
}

<# } else { #>
Expand All @@ -405,9 +406,9 @@ namespace <#= Configuration.Namespace#>.Clients
/// <param name="<#= p.Name #>"><#= p.Description.ToSummary() #></param>
<# } #>
/// <returns></returns>
public virtual async Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#>)
public virtual async Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken))
{
var result = await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>);
var result = await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false);

EnsureSuccess(result);

Expand Down Expand Up @@ -437,4 +438,3 @@ namespace <#= Configuration.Namespace#>.Clients
<# } #>
}
#endregion