Skip to content

Commit 165efdb

Browse files
committed
rename apiinvoker to apiclient, add configuration, make apiclient an instance
1 parent f807e34 commit 165efdb

File tree

9 files changed

+128
-63
lines changed

9 files changed

+128
-63
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/CSharpClientCodegen.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public CSharpClientCodegen() {
4141

4242
additionalProperties.put("invokerPackage", invokerPackage);
4343

44-
supportingFiles.add(new SupportingFile("apiInvoker.mustache",
45-
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.cs"));
46-
supportingFiles.add(new SupportingFile("apiException.mustache",
44+
supportingFiles.add(new SupportingFile("Configuration.mustache",
45+
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Configuration.cs"));
46+
supportingFiles.add(new SupportingFile("ApiClient.mustache",
47+
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiClient.cs"));
48+
supportingFiles.add(new SupportingFile("ApiException.mustache",
4749
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.cs"));
4850
supportingFiles.add(new SupportingFile("Newtonsoft.Json.dll", "bin", "Newtonsoft.Json.dll"));
4951
supportingFiles.add(new SupportingFile("compile.mustache", "", "compile.bat"));

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ using Newtonsoft.Json;
88
using RestSharp;
99

1010
namespace {{invokerPackage}} {
11-
public class ApiInvoker {
12-
public ApiInvoker() {
11+
public class ApiClient {
12+
public ApiClient() {
1313
this.basePath = "{{basePath}}";
14+
this.restClient = new RestClient(this.basePath);
1415
}
1516

16-
public ApiInvoker(String basePath) {
17+
public ApiClient(String basePath) {
1718
this.basePath = basePath;
19+
this.restClient = new RestClient(this.basePath);
1820
}
1921

2022
public string basePath { get; set; }
@@ -43,10 +45,10 @@ namespace {{invokerPackage}} {
4345
request.AddParameter(param.Key, param.Value);
4446
4547
// add file parameter, if any
46-
foreach(KeyValuePair<string, string> param in FormParams)
48+
foreach(KeyValuePair<string, string> param in FileParams)
4749
request.AddFile(param.Key, param.Value);
4850
49-
if (PostBody == null) {
51+
if (PostBody != null) {
5052
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
5153
}
5254

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Text;
7+
using {{invokerPackage}};
8+
9+
namespace {{invokerPackage}} {
10+
public class Configuration{
11+
public static ApiClient apiClient = new ApiClient();
12+
13+
}
14+
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ namespace {{package}} {
1010
{{#operations}}
1111
public class {{classname}} {
1212
string basePath;
13-
public ApiInvoker apiClient {get; set;}
13+
public ApiClient apiClient {get; set;}
1414

1515
public {{classname}}(String basePath = "{{basePath}}")
1616
{
1717
this.basePath = basePath;
18-
this.apiClient = new ApiInvoker(basePath);
18+
this.apiClient = new ApiClient(basePath);
19+
}
20+
21+
/// <summary>
22+
/// Create a new object
23+
/// </summary>
24+
/// <param name="apiClient"> an instance of ApiClient
25+
/// <returns></returns>
26+
public {{classname}}(ApiClient apiClient = null) {
27+
if (apiClient == null) { // use the default one in Configuration
28+
this.apiClient = Configuration.apiClient;
29+
} else {
30+
this.apiClient = apiClient;
31+
}
1932
}
2033

2134
/// <summary>
@@ -36,7 +49,6 @@ namespace {{package}} {
3649
}
3750

3851
{{#operation}}
39-
4052
/// <summary>
4153
/// {{summary}} {{notes}}
4254
/// </summary>
@@ -71,7 +83,7 @@ namespace {{package}} {
7183
{{/bodyParam}}
7284

7385
// make the HTTP request
74-
IRestResponse response = (IRestResponse) apiClient.CallApi("{{path}}", Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams);
86+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams);
7587

7688
if (((int)response.StatusCode) >= 400) {
7789
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);

samples/client/petstore/csharp/src/main/csharp/io/swagger/Api/PetApi.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ namespace IO.Swagger.Api {
88

99
public class PetApi {
1010
string basePath;
11-
public ApiInvoker apiClient {get; set;}
11+
public ApiClient apiClient {get; set;}
1212

1313
public PetApi(String basePath = "http://petstore.swagger.io/v2")
1414
{
1515
this.basePath = basePath;
16-
this.apiClient = new ApiInvoker(basePath);
16+
this.apiClient = new ApiClient(basePath);
17+
}
18+
19+
/// <summary>
20+
/// Create a new object
21+
/// </summary>
22+
/// <param name="apiClient"> an instance of ApiClient
23+
/// <returns></returns>
24+
public PetApi(ApiClient apiClient = null) {
25+
if (apiClient == null) { // use the default one in Configuration
26+
this.apiClient = Configuration.apiClient;
27+
} else {
28+
this.apiClient = apiClient;
29+
}
1730
}
1831

1932
/// <summary>
@@ -34,7 +47,6 @@ public String GetBasePath() {
3447
}
3548

3649

37-
3850
/// <summary>
3951
/// Update an existing pet
4052
/// </summary>
@@ -61,7 +73,7 @@ public void UpdatePet (Pet Body) {
6173

6274

6375
// make the HTTP request
64-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet", Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
76+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.PUT, queryParams, postBody, headerParams, formParams, fileParams);
6577

6678
if (((int)response.StatusCode) >= 400) {
6779
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content);
@@ -70,7 +82,6 @@ public void UpdatePet (Pet Body) {
7082
return;
7183
}
7284

73-
7485
/// <summary>
7586
/// Add a new pet to the store
7687
/// </summary>
@@ -97,7 +108,7 @@ public void AddPet (Pet Body) {
97108

98109

99110
// make the HTTP request
100-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
111+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
101112

102113
if (((int)response.StatusCode) >= 400) {
103114
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content);
@@ -106,7 +117,6 @@ public void AddPet (Pet Body) {
106117
return;
107118
}
108119

109-
110120
/// <summary>
111121
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
112122
/// </summary>
@@ -133,15 +143,14 @@ public List<Pet> FindPetsByStatus (List<string> Status) {
133143

134144

135145
// make the HTTP request
136-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/findByStatus", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
146+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
137147

138148
if (((int)response.StatusCode) >= 400) {
139149
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content);
140150
}
141151
return (List<Pet>) apiClient.Deserialize(response.Content, typeof(List<Pet>));
142152
}
143153

144-
145154
/// <summary>
146155
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
147156
/// </summary>
@@ -168,15 +177,14 @@ public List<Pet> FindPetsByTags (List<string> Tags) {
168177

169178

170179
// make the HTTP request
171-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/findByTags", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
180+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
172181

173182
if (((int)response.StatusCode) >= 400) {
174183
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content);
175184
}
176185
return (List<Pet>) apiClient.Deserialize(response.Content, typeof(List<Pet>));
177186
}
178187

179-
180188
/// <summary>
181189
/// Find pet by ID Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
182190
/// </summary>
@@ -206,15 +214,14 @@ public Pet GetPetById (long? PetId) {
206214

207215

208216
// make the HTTP request
209-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
217+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
210218

211219
if (((int)response.StatusCode) >= 400) {
212220
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content);
213221
}
214222
return (Pet) apiClient.Deserialize(response.Content, typeof(Pet));
215223
}
216224

217-
218225
/// <summary>
219226
/// Updates a pet in the store with form data
220227
/// </summary>
@@ -248,7 +255,7 @@ public void UpdatePetWithForm (string PetId, string Name, string Status) {
248255

249256

250257
// make the HTTP request
251-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
258+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
252259

253260
if (((int)response.StatusCode) >= 400) {
254261
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content);
@@ -257,7 +264,6 @@ public void UpdatePetWithForm (string PetId, string Name, string Status) {
257264
return;
258265
}
259266

260-
261267
/// <summary>
262268
/// Deletes a pet
263269
/// </summary>
@@ -289,7 +295,7 @@ public void DeletePet (string ApiKey, long? PetId) {
289295

290296

291297
// make the HTTP request
292-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}", Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
298+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
293299

294300
if (((int)response.StatusCode) >= 400) {
295301
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content);
@@ -298,7 +304,6 @@ public void DeletePet (string ApiKey, long? PetId) {
298304
return;
299305
}
300306

301-
302307
/// <summary>
303308
/// uploads an image
304309
/// </summary>
@@ -332,7 +337,7 @@ public void UploadFile (long? PetId, string AdditionalMetadata, string File) {
332337

333338

334339
// make the HTTP request
335-
IRestResponse response = (IRestResponse) apiClient.CallApi("/pet/{petId}/uploadImage", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
340+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
336341

337342
if (((int)response.StatusCode) >= 400) {
338343
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content);

samples/client/petstore/csharp/src/main/csharp/io/swagger/Api/StoreApi.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ namespace IO.Swagger.Api {
88

99
public class StoreApi {
1010
string basePath;
11-
public ApiInvoker apiClient {get; set;}
11+
public ApiClient apiClient {get; set;}
1212

1313
public StoreApi(String basePath = "http://petstore.swagger.io/v2")
1414
{
1515
this.basePath = basePath;
16-
this.apiClient = new ApiInvoker(basePath);
16+
this.apiClient = new ApiClient(basePath);
17+
}
18+
19+
/// <summary>
20+
/// Create a new object
21+
/// </summary>
22+
/// <param name="apiClient"> an instance of ApiClient
23+
/// <returns></returns>
24+
public StoreApi(ApiClient apiClient = null) {
25+
if (apiClient == null) { // use the default one in Configuration
26+
this.apiClient = Configuration.apiClient;
27+
} else {
28+
this.apiClient = apiClient;
29+
}
1730
}
1831

1932
/// <summary>
@@ -34,7 +47,6 @@ public String GetBasePath() {
3447
}
3548

3649

37-
3850
/// <summary>
3951
/// Returns pet inventories by status Returns a map of status codes to quantities
4052
/// </summary>
@@ -59,15 +71,14 @@ public String GetBasePath() {
5971

6072

6173
// make the HTTP request
62-
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/inventory", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
74+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
6375

6476
if (((int)response.StatusCode) >= 400) {
6577
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content);
6678
}
6779
return (Dictionary<String, int?>) apiClient.Deserialize(response.Content, typeof(Dictionary<String, int?>));
6880
}
6981

70-
7182
/// <summary>
7283
/// Place an order for a pet
7384
/// </summary>
@@ -94,15 +105,14 @@ public Order PlaceOrder (Order Body) {
94105

95106

96107
// make the HTTP request
97-
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order", Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
108+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.POST, queryParams, postBody, headerParams, formParams, fileParams);
98109

99110
if (((int)response.StatusCode) >= 400) {
100111
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content);
101112
}
102113
return (Order) apiClient.Deserialize(response.Content, typeof(Order));
103114
}
104115

105-
106116
/// <summary>
107117
/// Find purchase order by ID For valid response try integer IDs with value &lt;= 5 or &gt; 10. Other values will generated exceptions
108118
/// </summary>
@@ -132,15 +142,14 @@ public Order GetOrderById (string OrderId) {
132142

133143

134144
// make the HTTP request
135-
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order/{orderId}", Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
145+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams);
136146

137147
if (((int)response.StatusCode) >= 400) {
138148
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content);
139149
}
140150
return (Order) apiClient.Deserialize(response.Content, typeof(Order));
141151
}
142152

143-
144153
/// <summary>
145154
/// Delete purchase order by ID For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
146155
/// </summary>
@@ -170,7 +179,7 @@ public void DeleteOrder (string OrderId) {
170179

171180

172181
// make the HTTP request
173-
IRestResponse response = (IRestResponse) apiClient.CallApi("/store/order/{orderId}", Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
182+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.DELETE, queryParams, postBody, headerParams, formParams, fileParams);
174183

175184
if (((int)response.StatusCode) >= 400) {
176185
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content);

0 commit comments

Comments
 (0)