Skip to content

Commit

Permalink
Dotnet async templates (#90)
Browse files Browse the repository at this point in the history
* Update dotnet templates for async operations

* Obsolete messages added to template

* Dotnet submodule moved to main
  • Loading branch information
ivankamkin committed Jan 29, 2024
1 parent 2947357 commit c4c8cab
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
74 changes: 74 additions & 0 deletions codegen/Templates/csharp/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using {{packageName}}.Interfaces;
using {{packageName}}.Internal;
using {{packageName}}.Internal.RequestHandlers;
Expand Down Expand Up @@ -65,12 +66,14 @@ namespace {{packageName}}.Api
{{#operation}}

/// <summary>
/// This method is obsolete and will be removed in next releases. Use new async method <see cref="{{nickname}}Async" /> instead.
/// {{summary}} {{notes}}
/// </summary>
/// <param name="request">Request. <see cref="{{nickname}}Request" /></param>
{{#returnType}}/// <returns>
/// <see cref="{{{returnType}}}" />
/// </returns>{{/returnType}}
[System.Obsolete("This method is obsolete and will be removed in next releases. Use new async method \"{{nickname}}Async\" instead.")]
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}}({{nickname}}Request request)
{
{{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set
Expand Down Expand Up @@ -134,6 +137,77 @@ namespace {{packageName}}.Api
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/returnType}}{{/x-binary-result}}{{/vendorExtensions}}
}

/// <summary>
/// {{summary}} {{notes}}
/// </summary>
/// <param name="request">Request. <see cref="{{nickname}}Request" /></param>
/// <returns>
/// A task that represents the asynchronous operation. {{#returnType}}Task result type is <see cref="{{returnType}}" /> {{/returnType}}
/// </returns>
public async {{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async({{nickname}}Request request)
{
{{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set
if (request.{{baseName}} == null)
{
throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
}
{{/required}}{{/allParams}} // create path and map variables
string resourcePath = _configuration.GetApiRootUrl() + "{{path}}";
resourcePath = Regex
.Replace(resourcePath, "\\*", string.Empty)
.Replace("&amp;", "&")
.Replace("/?", "?");
{{#headerParams}}
{{#-first}}var headerParams = new Dictionary<string, string>();{{/-first}}
{{/headerParams}}
{{#formParams}}
{{#-first}}var formParams = new Dictionary<string, object>();{{/-first}}
{{/formParams}}
{{#pathParams}}
resourcePath = UrlHelper.AddPathParameter(resourcePath, "{{paramName}}", request.{{baseName}});
{{/pathParams}}
{{#queryParams}}
{{#-first}}#pragma warning disable CS0618 // Type or member is obsolete{{/-first}}
resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "{{paramName}}", request.{{baseName}});
{{#-last}}#pragma warning restore CS0618 // Type or member is obsolete{{/-last}}
{{/queryParams}}
{{#hasBodyParam}}string postBody = SerializationHelper.Serialize(request.{{bodyParam.baseName}}); // http body (model) parameter{{/hasBodyParam}}
{{#formParams}}
if (request.{{baseName}} != null)
{
{{#isFile}}
formParams.Add("{{paramName}}", ApiInvoker.ToFileInfo(request.{{baseName}}, "{{baseName}}"));
{{/isFile}}{{^isFile}}formParams.Add("{{baseName}}", request.{{baseName}}); // form parameter{{/isFile}}
}
{{/formParams}}
{{#vendorExtensions}}{{#x-binary-result}} return await _apiInvoker.InvokeBinaryApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/x-binary-result}}{{/vendorExtensions}}{{#vendorExtensions}}{{^x-binary-result}}{{#returnType}} string response = await _apiInvoker.InvokeApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
null,
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});

if (response != null)
{
return ({{returnType}}) SerializationHelper.Deserialize(response, typeof({{returnType}}));
}

return null;
{{/returnType}}
{{^returnType}}
await _apiInvoker.InvokeApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/returnType}}{{/x-binary-result}}{{/vendorExtensions}}
}
{{/operation}}
}
{{/operations}}
Expand Down

0 comments on commit c4c8cab

Please sign in to comment.