Skip to content

Commit c4c8cab

Browse files
authored
Dotnet async templates (#90)
* Update dotnet templates for async operations * Obsolete messages added to template * Dotnet submodule moved to main
1 parent 2947357 commit c4c8cab

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

codegen/Templates/csharp/api.mustache

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Collections.Generic;
77
using System.Text.RegularExpressions;
8+
using System.Threading.Tasks;
89
using {{packageName}}.Interfaces;
910
using {{packageName}}.Internal;
1011
using {{packageName}}.Internal.RequestHandlers;
@@ -65,12 +66,14 @@ namespace {{packageName}}.Api
6566
{{#operation}}
6667

6768
/// <summary>
69+
/// This method is obsolete and will be removed in next releases. Use new async method <see cref="{{nickname}}Async" /> instead.
6870
/// {{summary}} {{notes}}
6971
/// </summary>
7072
/// <param name="request">Request. <see cref="{{nickname}}Request" /></param>
7173
{{#returnType}}/// <returns>
7274
/// <see cref="{{{returnType}}}" />
7375
/// </returns>{{/returnType}}
76+
[System.Obsolete("This method is obsolete and will be removed in next releases. Use new async method \"{{nickname}}Async\" instead.")]
7477
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}}({{nickname}}Request request)
7578
{
7679
{{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set
@@ -134,6 +137,77 @@ namespace {{packageName}}.Api
134137
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
135138
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/returnType}}{{/x-binary-result}}{{/vendorExtensions}}
136139
}
140+
141+
/// <summary>
142+
/// {{summary}} {{notes}}
143+
/// </summary>
144+
/// <param name="request">Request. <see cref="{{nickname}}Request" /></param>
145+
/// <returns>
146+
/// A task that represents the asynchronous operation. {{#returnType}}Task result type is <see cref="{{returnType}}" /> {{/returnType}}
147+
/// </returns>
148+
public async {{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async({{nickname}}Request request)
149+
{
150+
{{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set
151+
if (request.{{baseName}} == null)
152+
{
153+
throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
154+
}
155+
{{/required}}{{/allParams}} // create path and map variables
156+
string resourcePath = _configuration.GetApiRootUrl() + "{{path}}";
157+
resourcePath = Regex
158+
.Replace(resourcePath, "\\*", string.Empty)
159+
.Replace("&amp;", "&")
160+
.Replace("/?", "?");
161+
{{#headerParams}}
162+
{{#-first}}var headerParams = new Dictionary<string, string>();{{/-first}}
163+
{{/headerParams}}
164+
{{#formParams}}
165+
{{#-first}}var formParams = new Dictionary<string, object>();{{/-first}}
166+
{{/formParams}}
167+
{{#pathParams}}
168+
resourcePath = UrlHelper.AddPathParameter(resourcePath, "{{paramName}}", request.{{baseName}});
169+
{{/pathParams}}
170+
{{#queryParams}}
171+
{{#-first}}#pragma warning disable CS0618 // Type or member is obsolete{{/-first}}
172+
resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "{{paramName}}", request.{{baseName}});
173+
{{#-last}}#pragma warning restore CS0618 // Type or member is obsolete{{/-last}}
174+
{{/queryParams}}
175+
{{#hasBodyParam}}string postBody = SerializationHelper.Serialize(request.{{bodyParam.baseName}}); // http body (model) parameter{{/hasBodyParam}}
176+
{{#formParams}}
177+
if (request.{{baseName}} != null)
178+
{
179+
{{#isFile}}
180+
formParams.Add("{{paramName}}", ApiInvoker.ToFileInfo(request.{{baseName}}, "{{baseName}}"));
181+
{{/isFile}}{{^isFile}}formParams.Add("{{baseName}}", request.{{baseName}}); // form parameter{{/isFile}}
182+
}
183+
{{/formParams}}
184+
{{#vendorExtensions}}{{#x-binary-result}} return await _apiInvoker.InvokeBinaryApiAsync(
185+
resourcePath,
186+
"{{httpMethod}}",
187+
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
188+
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
189+
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/x-binary-result}}{{/vendorExtensions}}{{#vendorExtensions}}{{^x-binary-result}}{{#returnType}} string response = await _apiInvoker.InvokeApiAsync(
190+
resourcePath,
191+
"{{httpMethod}}",
192+
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
193+
null,
194+
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});
195+
196+
if (response != null)
197+
{
198+
return ({{returnType}}) SerializationHelper.Deserialize(response, typeof({{returnType}}));
199+
}
200+
201+
return null;
202+
{{/returnType}}
203+
{{^returnType}}
204+
await _apiInvoker.InvokeApiAsync(
205+
resourcePath,
206+
"{{httpMethod}}",
207+
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
208+
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
209+
{{#hasFormParams}}formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/returnType}}{{/x-binary-result}}{{/vendorExtensions}}
210+
}
137211
{{/operation}}
138212
}
139213
{{/operations}}

0 commit comments

Comments
 (0)