forked from loafle/openapi-generator-original
Merge branch 'Hadou1-Generic_API_Exception' into develop_2.0
This commit is contained in:
commit
3fe2169ffa
@ -4,6 +4,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using RestSharp;
|
using RestSharp;
|
||||||
|
|
||||||
@ -36,7 +37,16 @@ namespace {{invokerPackage}} {
|
|||||||
|
|
||||||
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
||||||
|
|
||||||
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
|
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
|
||||||
|
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
|
||||||
|
var response = Task.Run(async () => {
|
||||||
|
var resp = await CallApiAsync(Path, Method, QueryParams, PostBody, HeaderParams, FormParams, FileParams, AuthSettings);
|
||||||
|
return resp;
|
||||||
|
});
|
||||||
|
return response.Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Object> CallApiAsync(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
|
||||||
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
|
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
|
||||||
|
|
||||||
var request = new RestRequest(Path, Method);
|
var request = new RestRequest(Path, Method);
|
||||||
@ -67,7 +77,7 @@ namespace {{invokerPackage}} {
|
|||||||
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
|
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
|
||||||
}
|
}
|
||||||
|
|
||||||
return (Object)restClient.Execute(request);
|
return (Object) await restClient.ExecuteTaskAsync(request);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,12 @@ namespace {{invokerPackage}} {
|
|||||||
/// <value>The error code (HTTP status code).</value>
|
/// <value>The error code (HTTP status code).</value>
|
||||||
public int ErrorCode { get; set; }
|
public int ErrorCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the error content (body json object)
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The error content (Http response body).</value>
|
||||||
|
public dynamic ErrorContent { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ApiException"/> class.
|
/// Initializes a new instance of the <see cref="ApiException"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -26,6 +32,11 @@ namespace {{invokerPackage}} {
|
|||||||
this.ErrorCode = errorCode;
|
this.ErrorCode = errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApiException(int errorCode, string message, dynamic errorContent = null) : base(message) {
|
||||||
|
this.ErrorCode = errorCode;
|
||||||
|
this.ErrorContent = errorContent;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using RestSharp;
|
using RestSharp;
|
||||||
using {{invokerPackage}};
|
using {{invokerPackage}};
|
||||||
using {{modelPackage}};
|
using {{modelPackage}};
|
||||||
@ -8,10 +9,30 @@ using {{modelPackage}};
|
|||||||
|
|
||||||
namespace {{package}} {
|
namespace {{package}} {
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
|
|
||||||
|
public interface I{{classname}} {
|
||||||
|
{{#operation}}
|
||||||
|
/// <summary>
|
||||||
|
/// {{summary}} {{notes}}
|
||||||
|
/// </summary>
|
||||||
|
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
|
||||||
|
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
||||||
|
{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// {{summary}} {{notes}}
|
||||||
|
/// </summary>
|
||||||
|
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
|
||||||
|
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
||||||
|
{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
|
||||||
|
{{/operation}}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a collection of functions to interact with the API endpoints
|
/// Represents a collection of functions to interact with the API endpoints
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class {{classname}} {
|
public class {{classname}} : I{{classname}} {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
|
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -61,8 +82,7 @@ namespace {{package}} {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// {{summary}} {{notes}}
|
/// {{summary}} {{notes}}
|
||||||
/// </summary>
|
/// </summary>
|
||||||
{{#allParams}} /// <param name="{{paramName}}">{{description}}</param>
|
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
|
||||||
{{/allParams}}
|
|
||||||
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
||||||
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||||
|
|
||||||
@ -98,12 +118,56 @@ namespace {{package}} {
|
|||||||
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
|
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
|
||||||
|
|
||||||
if (((int)response.StatusCode) >= 400) {
|
if (((int)response.StatusCode) >= 400) {
|
||||||
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);
|
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
|
||||||
}
|
}
|
||||||
{{#returnType}}return ({{{returnType}}}) apiClient.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
|
{{#returnType}}return ({{{returnType}}}) apiClient.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
|
||||||
return;{{/returnType}}
|
return;{{/returnType}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// {{summary}} {{notes}}
|
||||||
|
/// </summary>
|
||||||
|
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}
|
||||||
|
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
||||||
|
public async {{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||||
|
|
||||||
|
{{#allParams}}{{#required}}
|
||||||
|
// verify the required parameter '{{paramName}}' is set
|
||||||
|
if ({{paramName}} == null) throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
|
||||||
|
{{/required}}{{/allParams}}
|
||||||
|
|
||||||
|
var path = "{{path}}";
|
||||||
|
path = path.Replace("{format}", "json");
|
||||||
|
{{#pathParams}}path = path.Replace("{" + "{{baseName}}" + "}", apiClient.ParameterToString({{{paramName}}}));
|
||||||
|
{{/pathParams}}
|
||||||
|
|
||||||
|
var queryParams = new Dictionary<String, String>();
|
||||||
|
var headerParams = new Dictionary<String, String>();
|
||||||
|
var formParams = new Dictionary<String, String>();
|
||||||
|
var fileParams = new Dictionary<String, String>();
|
||||||
|
String postBody = null;
|
||||||
|
|
||||||
|
{{#queryParams}} if ({{paramName}} != null) queryParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // query parameter
|
||||||
|
{{/queryParams}}
|
||||||
|
{{#headerParams}} if ({{paramName}} != null) headerParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // header parameter
|
||||||
|
{{/headerParams}}
|
||||||
|
{{#formParams}}if ({{paramName}} != null) {{#isFile}}fileParams.Add("{{baseName}}", {{paramName}});{{/isFile}}{{^isFile}}formParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // form parameter{{/isFile}}
|
||||||
|
{{/formParams}}
|
||||||
|
{{#bodyParam}}postBody = apiClient.Serialize({{paramName}}); // http body (model) parameter
|
||||||
|
{{/bodyParam}}
|
||||||
|
|
||||||
|
// authentication setting, if any
|
||||||
|
String[] authSettings = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
|
||||||
|
|
||||||
|
// make the HTTP request
|
||||||
|
IRestResponse response = (IRestResponse) await apiClient.CallApiAsync(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
|
||||||
|
if (((int)response.StatusCode) >= 400) {
|
||||||
|
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
|
||||||
|
}
|
||||||
|
{{#returnType}}return ({{{returnType}}}) ApiInvoker.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
|
||||||
|
return;{{/returnType}}
|
||||||
|
}
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
}
|
}
|
||||||
{{/operations}}
|
{{/operations}}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user