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);

View File

@ -8,12 +8,25 @@ namespace IO.Swagger.Api {
public class PetApi {
string basePath;
public ApiInvoker apiClient {get; set;}
public ApiClient apiClient {get; set;}
public PetApi(String basePath = "http://petstore.swagger.io/v2")
{
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 PetApi(ApiClient apiClient = null) {
if (apiClient == null) { // use the default one in Configuration
this.apiClient = Configuration.apiClient;
} else {
this.apiClient = apiClient;
}
}
/// <summary>
@ -34,7 +47,6 @@ namespace IO.Swagger.Api {
}
/// <summary>
/// Update an existing pet
/// </summary>
@ -61,7 +73,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet", Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content);
@ -70,7 +82,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Add a new pet to the store
/// </summary>
@ -97,7 +108,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content);
@ -106,7 +117,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
/// </summary>
@ -133,7 +143,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/findByStatus", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content);
@ -141,7 +151,6 @@ namespace IO.Swagger.Api {
return (List<Pet>) apiClient.Deserialize(response.Content, typeof(List<Pet>));
}
/// <summary>
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
/// </summary>
@ -168,7 +177,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/findByTags", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content);
@ -176,7 +185,6 @@ namespace IO.Swagger.Api {
return (List<Pet>) apiClient.Deserialize(response.Content, typeof(List<Pet>));
}
/// <summary>
/// Find pet by ID Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
/// </summary>
@ -206,7 +214,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content);
@ -214,7 +222,6 @@ namespace IO.Swagger.Api {
return (Pet) apiClient.Deserialize(response.Content, typeof(Pet));
}
/// <summary>
/// Updates a pet in the store with form data
/// </summary>
@ -248,7 +255,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content);
@ -257,7 +264,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Deletes a pet
/// </summary>
@ -289,7 +295,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content);
@ -298,7 +304,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// uploads an image
/// </summary>
@ -332,7 +337,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}/uploadImage", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content);

View File

@ -8,12 +8,25 @@ namespace IO.Swagger.Api {
public class StoreApi {
string basePath;
public ApiInvoker apiClient {get; set;}
public ApiClient apiClient {get; set;}
public StoreApi(String basePath = "http://petstore.swagger.io/v2")
{
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 StoreApi(ApiClient apiClient = null) {
if (apiClient == null) { // use the default one in Configuration
this.apiClient = Configuration.apiClient;
} else {
this.apiClient = apiClient;
}
}
/// <summary>
@ -34,7 +47,6 @@ namespace IO.Swagger.Api {
}
/// <summary>
/// Returns pet inventories by status Returns a map of status codes to quantities
/// </summary>
@ -59,7 +71,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/inventory", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content);
@ -67,7 +79,6 @@ namespace IO.Swagger.Api {
return (Dictionary<String, int?>) apiClient.Deserialize(response.Content, typeof(Dictionary<String, int?>));
}
/// <summary>
/// Place an order for a pet
/// </summary>
@ -94,7 +105,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content);
@ -102,7 +113,6 @@ namespace IO.Swagger.Api {
return (Order) apiClient.Deserialize(response.Content, typeof(Order));
}
/// <summary>
/// Find purchase order by ID For valid response try integer IDs with value &lt;= 5 or &gt; 10. Other values will generated exceptions
/// </summary>
@ -132,7 +142,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order/{orderId}", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content);
@ -140,7 +150,6 @@ namespace IO.Swagger.Api {
return (Order) apiClient.Deserialize(response.Content, typeof(Order));
}
/// <summary>
/// Delete purchase order by ID For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
/// </summary>
@ -170,7 +179,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order/{orderId}", Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content);

View File

@ -8,12 +8,25 @@ namespace IO.Swagger.Api {
public class UserApi {
string basePath;
public ApiInvoker apiClient {get; set;}
public ApiClient apiClient {get; set;}
public UserApi(String basePath = "http://petstore.swagger.io/v2")
{
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 UserApi(ApiClient apiClient = null) {
if (apiClient == null) { // use the default one in Configuration
this.apiClient = Configuration.apiClient;
} else {
this.apiClient = apiClient;
}
}
/// <summary>
@ -34,7 +47,6 @@ namespace IO.Swagger.Api {
}
/// <summary>
/// Create user This can only be done by the logged in user.
/// </summary>
@ -61,7 +73,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUser: " + response.Content);
@ -70,7 +82,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Creates list of users with given input array
/// </summary>
@ -97,7 +108,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/createWithArray", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithArrayInput: " + response.Content);
@ -106,7 +117,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Creates list of users with given input array
/// </summary>
@ -133,7 +143,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/createWithList", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithListInput: " + response.Content);
@ -142,7 +152,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Logs user into the system
/// </summary>
@ -171,7 +180,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/login", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.Content);
@ -179,7 +188,6 @@ namespace IO.Swagger.Api {
return (string) apiClient.Deserialize(response.Content, typeof(string));
}
/// <summary>
/// Logs out current logged in user session
/// </summary>
@ -204,7 +212,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/logout", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling LogoutUser: " + response.Content);
@ -213,7 +221,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Get user by user name
/// </summary>
@ -243,7 +250,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/{username}", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.Content);
@ -251,7 +258,6 @@ namespace IO.Swagger.Api {
return (User) apiClient.Deserialize(response.Content, typeof(User));
}
/// <summary>
/// Updated user This can only be done by the logged in user.
/// </summary>
@ -283,7 +289,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/{username}", Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdateUser: " + response.Content);
@ -292,7 +298,6 @@ namespace IO.Swagger.Api {
return;
}
/// <summary>
/// Delete user This can only be done by the logged in user.
/// </summary>
@ -322,7 +327,7 @@ namespace IO.Swagger.Api {
// make the HTTP request
IRestResponse response = (IRestResponse) apiClient.CallApi("/user/{username}", Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteUser: " + response.Content);

View File

@ -8,13 +8,15 @@ using Newtonsoft.Json;
using RestSharp;
namespace IO.Swagger.Client {
public class ApiInvoker {
public ApiInvoker() {
public class ApiClient {
public ApiClient() {
this.basePath = "http://petstore.swagger.io/v2";
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 IO.Swagger.Client {
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 IO.Swagger.Client;
namespace IO.Swagger.Client {
public class Configuration{
public static ApiClient apiClient = new ApiClient();
}
}