rename apiinvoker to apiclient, add configuration, make apiclient an instance

This commit is contained in:
wing328
2015-05-28 18:24:03 +08:00
parent f807e34b2b
commit 165efdbdbb
9 changed files with 128 additions and 63 deletions

View File

@@ -41,9 +41,11 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
additionalProperties.put("invokerPackage", invokerPackage);
supportingFiles.add(new SupportingFile("apiInvoker.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.cs"));
supportingFiles.add(new SupportingFile("apiException.mustache",
supportingFiles.add(new SupportingFile("Configuration.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Configuration.cs"));
supportingFiles.add(new SupportingFile("ApiClient.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiClient.cs"));
supportingFiles.add(new SupportingFile("ApiException.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.cs"));
supportingFiles.add(new SupportingFile("Newtonsoft.Json.dll", "bin", "Newtonsoft.Json.dll"));
supportingFiles.add(new SupportingFile("compile.mustache", "", "compile.bat"));

View File

@@ -8,13 +8,15 @@ using Newtonsoft.Json;
using RestSharp;
namespace {{invokerPackage}} {
public class ApiInvoker {
public ApiInvoker() {
public class ApiClient {
public ApiClient() {
this.basePath = "{{basePath}}";
this.restClient = new RestClient(this.basePath);
}
public ApiInvoker(String basePath) {
public ApiClient(String basePath) {
this.basePath = basePath;
this.restClient = new RestClient(this.basePath);
}
public string basePath { get; set; }
@@ -43,10 +45,10 @@ namespace {{invokerPackage}} {
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(KeyValuePair<string, string> param in FormParams)
foreach(KeyValuePair<string, string> param in FileParams)
request.AddFile(param.Key, param.Value);
if (PostBody == null) {
if (PostBody != null) {
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using {{invokerPackage}};
namespace {{invokerPackage}} {
public class Configuration{
public static ApiClient apiClient = new ApiClient();
}
}

View File

@@ -10,12 +10,25 @@ namespace {{package}} {
{{#operations}}
public class {{classname}} {
string basePath;
public ApiInvoker apiClient {get; set;}
public ApiClient apiClient {get; set;}
public {{classname}}(String basePath = "{{basePath}}")
{
this.basePath = basePath;
this.apiClient = new ApiInvoker(basePath);
this.apiClient = new ApiClient(basePath);
}
/// <summary>
/// Create a new object
/// </summary>
/// <param name="apiClient"> an instance of ApiClient
/// <returns></returns>
public {{classname}}(ApiClient apiClient = null) {
if (apiClient == null) { // use the default one in Configuration
this.apiClient = Configuration.apiClient;
} else {
this.apiClient = apiClient;
}
}
/// <summary>
@@ -36,7 +49,6 @@ namespace {{package}} {
}
{{#operation}}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
@@ -71,7 +83,7 @@ namespace {{package}} {
{{/bodyParam}}
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("{{path}}", Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);