Skip to content

Commit 4870ed7

Browse files
committed
Merge pull request #220 from darkcube/master
CSharp Changes
2 parents 527c40b + bbe4881 commit 4870ed7

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/main/resources/csharp/api.mustache

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@
88
namespace {{package}} {
99
{{#operations}}
1010
public class {{classname}} {
11-
string basePath = "{{basePath}}";
11+
string basePath;
1212
private readonly ApiInvoker apiInvoker = ApiInvoker.GetInstance();
1313
14+
public {{classname}}(String basePath = "{{basePath}}")
15+
{
16+
this.basePath = basePath;
17+
}
18+
1419
public ApiInvoker getInvoker() {
1520
return apiInvoker;
1621
}
17-
22+
1823
// Sets the endpoint base url for the services being accessed
1924
public void setBasePath(string basePath) {
2025
this.basePath = basePath;
2126
}
22-
27+
2328
// Gets the endpoint base url for the services being accessed
2429
public String getBasePath() {
2530
return basePath;

src/main/resources/csharp/apiInvoker.mustache

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public static ApiInvoker GetInstance() {
1515
return _instance;
1616
}
17-
17+
1818
public void addDefaultHeader(string key, string value) {
1919
defaultHeaderMap.Add(key, value);
2020
}
@@ -46,7 +46,7 @@
4646

4747
public string invokeAPI(string host, string path, string method, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams) {
4848
var b = new StringBuilder();
49-
49+
5050
foreach (var queryParamItem in queryParams)
5151
{
5252
var value = queryParamItem.Value;
@@ -71,7 +71,7 @@
7171
{
7272
client.Headers.Add(defaultHeaderMapItem.Key, defaultHeaderMapItem.Value);
7373
}
74-
74+
7575
switch (method)
7676
{
7777
case "GET":
@@ -84,13 +84,17 @@
8484
swRequestWriter.Close();
8585
break;
8686
default:
87-
throw new ApiException(500, "unknown method type " + method);
87+
throw new ApiException(500, "unknown method type " + method);
8888
}
8989

9090
try
9191
{
9292
var webResponse = (HttpWebResponse)client.GetResponse();
93-
if (webResponse.StatusCode != HttpStatusCode.OK) throw new ApiException((int)webResponse.StatusCode, webResponse.StatusDescription);
93+
if (webResponse.StatusCode != HttpStatusCode.OK)
94+
{
95+
webResponse.Close();
96+
throw new ApiException((int)webResponse.StatusCode, webResponse.StatusDescription);
97+
}
9498

9599
var responseReader = new StreamReader(webResponse.GetResponseStream());
96100
var responseData = responseReader.ReadToEnd();
@@ -104,6 +108,7 @@
104108
if (response != null)
105109
{
106110
statusCode = (int)response.StatusCode;
111+
response.Close();
107112
}
108113
throw new ApiException(statusCode, ex.Message);
109114
}

0 commit comments

Comments
 (0)