Skip to content

Commit ab5b2da

Browse files
committed
Merge branch 'Generic_API_Exception' of ssh://github.com/Hadou1/swagger-codegen into Hadou1-Generic_API_Exception
2 parents b4f153c + e737964 commit ab5b2da

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using System.IO;
44
using System.Linq;
55
using System.Net;
66
using System.Text;
7+
using System.Threading.Tasks;
78
using Newtonsoft.Json;
89
using RestSharp;
910

@@ -36,7 +37,16 @@ namespace {{invokerPackage}} {
3637

3738
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
3839

39-
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
40+
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
41+
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
42+
var response = Task.Run(async () => {
43+
var resp = await CallApiAsync(Path, Method, QueryParams, PostBody, HeaderParams, FormParams, FileParams, AuthSettings);
44+
return resp;
45+
});
46+
return response.Result;
47+
}
48+
49+
public async Task<Object> CallApiAsync(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
4050
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
4151
4252
var request = new RestRequest(Path, Method);
@@ -67,7 +77,7 @@ namespace {{invokerPackage}} {
6777
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
6878
}
6979

70-
return (Object)restClient.Execute(request);
80+
return (Object) await restClient.ExecuteTaskAsync(request);
7181

7282
}
7383

modules/swagger-codegen/src/main/resources/csharp/ApiException.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace {{invokerPackage}} {
1111
/// <value>The error code (HTTP status code).</value>
1212
public int ErrorCode { get; set; }
1313

14+
/// <summary>
15+
/// Gets or sets the error content (body json object)
16+
/// </summary>
17+
/// <value>The error content (Http response body).</value>
18+
public dynamic ErrorContent { get; private set; }
19+
1420
/// <summary>
1521
/// Initializes a new instance of the <see cref="ApiException"/> class.
1622
/// </summary>
@@ -26,6 +32,11 @@ namespace {{invokerPackage}} {
2632
this.ErrorCode = errorCode;
2733
}
2834

35+
public ApiException(int errorCode, string message, dynamic errorContent = null) : base(message) {
36+
this.ErrorCode = errorCode;
37+
this.ErrorContent = errorContent;
38+
}
39+
2940
}
3041

3142
}

modules/swagger-codegen/src/main/resources/csharp/api.mustache

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using RestSharp;
45
using {{invokerPackage}};
56
using {{modelPackage}};
@@ -8,10 +9,30 @@ using {{modelPackage}};
89

910
namespace {{package}} {
1011
{{#operations}}
12+
13+
public interface I{{classname}} {
14+
{{#operation}}
15+
/// <summary>
16+
/// {{summary}} {{notes}}
17+
/// </summary>
18+
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
19+
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
20+
{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
21+
22+
/// <summary>
23+
/// {{summary}} {{notes}}
24+
/// </summary>
25+
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
26+
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
27+
{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
28+
{{/operation}}
29+
}
30+
1131
/// <summary>
1232
/// Represents a collection of functions to interact with the API endpoints
1333
/// </summary>
14-
public class {{classname}} {
34+
public class {{classname}} : I{{classname}} {
35+
1536
/// <summary>
1637
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
1738
/// </summary>
@@ -61,8 +82,7 @@ namespace {{package}} {
6182
/// <summary>
6283
/// {{summary}} {{notes}}
6384
/// </summary>
64-
{{#allParams}} /// <param name="{{paramName}}">{{description}}</param>
65-
{{/allParams}}
85+
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
6686
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
6787
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
6888
@@ -98,12 +118,56 @@ namespace {{package}} {
98118
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
99119

100120
if (((int)response.StatusCode) >= 400) {
101-
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);
121+
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
102122
}
103123
{{#returnType}}return ({{{returnType}}}) apiClient.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
104124
return;{{/returnType}}
105125
}
126+
127+
/// <summary>
128+
/// {{summary}} {{notes}}
129+
/// </summary>
130+
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
131+
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
132+
public async {{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
133+
134+
{{#allParams}}{{#required}}
135+
// verify the required parameter '{{paramName}}' is set
136+
if ({{paramName}} == null) throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
137+
{{/required}}{{/allParams}}
138+
139+
var path = "{{path}}";
140+
path = path.Replace("{format}", "json");
141+
{{#pathParams}}path = path.Replace("{" + "{{baseName}}" + "}", apiClient.ParameterToString({{{paramName}}}));
142+
{{/pathParams}}
143+
144+
var queryParams = new Dictionary<String, String>();
145+
var headerParams = new Dictionary<String, String>();
146+
var formParams = new Dictionary<String, String>();
147+
var fileParams = new Dictionary<String, String>();
148+
String postBody = null;
149+
150+
{{#queryParams}} if ({{paramName}} != null) queryParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // query parameter
151+
{{/queryParams}}
152+
{{#headerParams}} if ({{paramName}} != null) headerParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // header parameter
153+
{{/headerParams}}
154+
{{#formParams}}if ({{paramName}} != null) {{#isFile}}fileParams.Add("{{baseName}}", {{paramName}});{{/isFile}}{{^isFile}}formParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // form parameter{{/isFile}}
155+
{{/formParams}}
156+
{{#bodyParam}}postBody = apiClient.Serialize({{paramName}}); // http body (model) parameter
157+
{{/bodyParam}}
158+
159+
// authentication setting, if any
160+
String[] authSettings = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
161+
162+
// make the HTTP request
163+
IRestResponse response = (IRestResponse) await apiClient.CallApiAsync(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
164+
if (((int)response.StatusCode) >= 400) {
165+
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
166+
}
167+
{{#returnType}}return ({{{returnType}}}) ApiInvoker.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
168+
return;{{/returnType}}
169+
}
106170
{{/operation}}
107-
}
171+
}
108172
{{/operations}}
109173
}

0 commit comments

Comments
 (0)