Skip to content

Commit 5f36ad3

Browse files
committed
add authentication support
1 parent 165efdb commit 5f36ad3

File tree

16 files changed

+569
-119
lines changed

16 files changed

+569
-119
lines changed

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

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,41 @@ using Newtonsoft.Json;
88
using RestSharp;
99

1010
namespace {{invokerPackage}} {
11+
/// <summary>
12+
/// API client is mainly responible for making the HTTP call to the API backend
13+
/// </summary>
1114
public class ApiClient {
12-
public ApiClient() {
13-
this.basePath = "{{basePath}}";
14-
this.restClient = new RestClient(this.basePath);
15-
}
1615
17-
public ApiClient(String basePath) {
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="ApiClient"/> class.
18+
/// </summary>
19+
/// <param name="basePath">The base path.</param>
20+
public ApiClient(String basePath="{{basePath}}") {
1821
this.basePath = basePath;
1922
this.restClient = new RestClient(this.basePath);
2023
}
2124

25+
/// <summary>
26+
/// Gets or sets the base path.
27+
/// </summary>
28+
/// <value>The base path.</value>
2229
public string basePath { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the RestClient
33+
/// </summary>
34+
/// <value>The RestClient.</value>
2335
public RestClient restClient { get; set; }
36+
2437
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
2538

2639
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
27-
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams) {
40+
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
2841
2942
var request = new RestRequest(Path, Method);
3043
44+
UpdateParamsForAuth(QueryParams, HeaderParams, AuthSettings);
45+
3146
// add default header, if any
3247
foreach(KeyValuePair<string, string> defaultHeader in this.defaultHeaderMap)
3348
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
@@ -126,5 +141,59 @@ namespace {{invokerPackage}} {
126141
throw new ApiException(500, e.Message);
127142
}
128143
}
144+
145+
/// <summary>
146+
/// Get the API key with prefix
147+
/// </summary>
148+
/// <param name="obj"> Object
149+
/// <returns>API key with prefix</returns>
150+
public string GetApiKeyWithPrefix (string apiKey)
151+
{
152+
var apiKeyValue = "";
153+
Configuration.apiKey.TryGetValue (apiKey, out apiKeyValue);
154+
var apiKeyPrefix = "";
155+
if (Configuration.apiKeyPrefix.TryGetValue (apiKey, out apiKeyPrefix)) {
156+
return apiKeyPrefix + " " + apiKeyValue;
157+
} else {
158+
return apiKeyValue;
159+
}
160+
}
161+
162+
/// <summary>
163+
/// Update parameters based on authentication
164+
/// </summary>
165+
/// <param name="QueryParams">Query parameters</param>
166+
/// <param name="HeaderParams">Header parameters</param>
167+
/// <param name="AuthSettings">Authentication settings</param>
168+
public void UpdateParamsForAuth(Dictionary<String, String> QueryParams, Dictionary<String, String> HeaderParams, string[] AuthSettings) {
169+
if (AuthSettings == null || AuthSettings.Length == 0)
170+
return;
171+
172+
foreach (string auth in AuthSettings) {
173+
// determine which one to use
174+
switch(auth) {
175+
{{#authMethods}}
176+
case "{{name}}":
177+
{{#isApiKey}}{{#isKeyInHeader}}HeaderParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}QueryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}HeaderParams["Authorization"] = "Basic " + Base64Encode(Configuration.username + ":" + Configuration.password);{{/isBasic}}
178+
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
179+
break;
180+
{{/authMethods}}
181+
default:
182+
//TODO show warning about security definition not found
183+
break;
184+
}
185+
}
186+
187+
}
188+
189+
/// <summary>
190+
/// Encode string in base64 format
191+
/// </summary>
192+
/// <param name="text">String to be encoded</param>
193+
public static string Base64Encode(string text) {
194+
var textByte = System.Text.Encoding.UTF8.GetBytes(text);
195+
return System.Convert.ToBase64String(textByte);
196+
}
197+
129198
}
130199
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,41 @@ using System.Text;
77
using {{invokerPackage}};
88

99
namespace {{invokerPackage}} {
10+
/// <summary>
11+
/// Represents a set of configuration settings
12+
/// </summary>
1013
public class Configuration{
14+
15+
/// <summary>
16+
/// Gets or sets the API client. This is the default API client for making HTTP calls.
17+
/// </summary>
18+
/// <value>The API client.</value>
1119
public static ApiClient apiClient = new ApiClient();
1220
21+
/// <summary>
22+
/// Gets or sets the username (HTTP basic authentication)
23+
/// </summary>
24+
/// <value>The username.</value>
25+
public static String username { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets the password (HTTP basic authentication)
29+
/// </summary>
30+
/// <value>The password.</value>
31+
public static String password { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets the API key based on the authentication name
35+
/// </summary>
36+
/// <value>The API key.</value>
37+
public static Dictionary<String, String> apiKey = new Dictionary<String, String>();
38+
39+
/// <summary>
40+
/// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name
41+
/// </summary>
42+
/// <value>The prefix of the API key.</value>
43+
public static Dictionary<String, String> apiKeyPrefix = new Dictionary<String, String>();
44+
45+
1346
}
1447
}

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ using {{modelPackage}};
88

99
namespace {{package}} {
1010
{{#operations}}
11+
/// <summary>
12+
/// Represents a collection of functions to interact with the API endpoints
13+
/// </summary>
1114
public class {{classname}} {
12-
string basePath;
13-
public ApiClient apiClient {get; set;}
14-
15-
public {{classname}}(String basePath = "{{basePath}}")
16-
{
17-
this.basePath = basePath;
18-
this.apiClient = new ApiClient(basePath);
19-
}
20-
2115
/// <summary>
22-
/// Create a new object
16+
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
2317
/// </summary>
24-
/// <param name="apiClient"> an instance of ApiClient
18+
/// <param name="apiClient"> an instance of ApiClient (optional)
2519
/// <returns></returns>
2620
public {{classname}}(ApiClient apiClient = null) {
2721
if (apiClient == null) { // use the default one in Configuration
@@ -32,22 +26,37 @@ namespace {{package}} {
3226
}
3327

3428
/// <summary>
35-
/// Sets the endpoint base url for the services being accessed
29+
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
3630
/// </summary>
37-
/// <param name="basePath"> Base URL
3831
/// <returns></returns>
39-
public void SetBasePath(string basePath) {
40-
this.basePath = basePath;
32+
public {{classname}}(String basePath)
33+
{
34+
this.apiClient = new ApiClient(basePath);
35+
}
36+
37+
/// <summary>
38+
/// Sets the base path of the API client.
39+
/// </summary>
40+
/// <value>The base path</value>
41+
public void SetBasePath(String basePath) {
42+
this.apiClient.basePath = basePath;
4143
}
4244

4345
/// <summary>
44-
/// Gets the endpoint base url for the services being accessed
45-
/// <returns>Base URL</returns>
46+
/// Gets the base path of the API client.
4647
/// </summary>
47-
public String GetBasePath() {
48-
return this.basePath;
48+
/// <value>The base path</value>
49+
public String GetBasePath(String basePath) {
50+
return this.apiClient.basePath;
4951
}
5052

53+
/// <summary>
54+
/// Gets or sets the API client.
55+
/// </summary>
56+
/// <value>The API client</value>
57+
public ApiClient apiClient {get; set;}
58+
59+
5160
{{#operation}}
5261
/// <summary>
5362
/// {{summary}} {{notes}}
@@ -82,8 +91,11 @@ namespace {{package}} {
8291
{{#bodyParam}}postBody = apiClient.Serialize({{paramName}}); // http body (model) parameter
8392
{{/bodyParam}}
8493

94+
// authentication setting, if any
95+
String[] authSettings = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
96+
8597
// make the HTTP request
86-
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams);
98+
IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
8799

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

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
using System;
22

33
namespace {{invokerPackage}} {
4-
4+
/// <summary>
5+
/// API Exception
6+
/// </summary>
57
public class ApiException : Exception {
6-
8+
/// <summary>
9+
/// Gets or sets the error code (HTTP status code)
10+
/// </summary>
11+
/// <value>The error code (HTTP status code).</value>
712
public int ErrorCode { get; set; }
813

14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="ApiException"/> class.
16+
/// </summary>
17+
/// <param name="basePath">The base path.</param>
918
public ApiException() {}
1019

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="ApiException"/> class.
22+
/// </summary>
23+
/// <param name="errorCode">HTTP status code.</param>
24+
/// <param name="message">Error message.</param>
1125
public ApiException(int errorCode, string message) : base(message) {
1226
this.ErrorCode = errorCode;
1327
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ using System.Text;
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.Runtime.Serialization;
6+
using Newtonsoft.Json;
67

78
{{#models}}
89
{{#model}}
910
namespace {{package}} {
11+
12+
/// <summary>
13+
/// {{description}}
14+
/// </summary>
1015
[DataContract]
1116
public class {{classname}} {
1217
{{#vars}}
@@ -15,6 +20,11 @@ namespace {{package}} {
1520
public {{{datatype}}} {{name}} { get; set; }
1621

1722
{{/vars}}
23+
24+
/// <summary>
25+
/// Get the string presentation of the object
26+
/// </summary>
27+
/// <returns>String presentation of the object</returns>
1828
public override string ToString() {
1929
var sb = new StringBuilder();
2030
sb.Append("class {{classname}} {\n");
@@ -24,7 +34,16 @@ namespace {{package}} {
2434
sb.Append("}\n");
2535
return sb.ToString();
2636
}
27-
}
37+
38+
/// <summary>
39+
/// Get the JSON string presentation of the object
40+
/// </summary>
41+
/// <returns>JSON string presentation of the object</returns>
42+
public string ToJson() {
43+
return JsonConvert.SerializeObject(this, Formatting.Indented);
44+
}
45+
46+
}
2847
{{/model}}
2948
{{/models}}
3049
}

0 commit comments

Comments
 (0)