/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs
C# | 283 lines | 149 code | 35 blank | 99 comment | 26 complexity | 0a67ac2411cf0ab952f3495d335b5851 MD5 | raw file
1using System;
2using System.Reflection;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7
8namespace IO.Swagger.Client
9{
10 /// <summary>
11 /// Represents a set of configuration settings
12 /// </summary>
13 public class Configuration
14 {
15 /// <summary>
16 /// Initializes a new instance of the Configuration class with different settings
17 /// </summary>
18 /// <param name="apiClient">Api client</param>
19 /// <param name="defaultHeader">Dictionary of default HTTP header</param>
20 /// <param name="username">Username</param>
21 /// <param name="password">Password</param>
22 /// <param name="accessToken">accessToken</param>
23 /// <param name="apiKey">Dictionary of API key</param>
24 /// <param name="apiKeyPrefix">Dictionary of API key prefix</param>
25 /// <param name="tempFolderPath">Temp folder path</param>
26 /// <param name="dateTimeFormat">DateTime format string</param>
27 /// <param name="timeout">HTTP connection timeout (in milliseconds)</param>
28 public Configuration(ApiClient apiClient = null,
29 Dictionary<String, String> defaultHeader = null,
30 string username = null,
31 string password = null,
32 string accessToken = null,
33 Dictionary<String, String> apiKey = null,
34 Dictionary<String, String> apiKeyPrefix = null,
35 string tempFolderPath = null,
36 string dateTimeFormat = null,
37 int timeout = 100000,
38 string userAgent = "Swagger-Codegen/1.0.0/csharp"
39 )
40 {
41 setApiClientUsingDefault(apiClient);
42
43 Username = username;
44 Password = password;
45 AccessToken = accessToken;
46 UserAgent = userAgent;
47
48 if (defaultHeader != null)
49 DefaultHeader = defaultHeader;
50 if (apiKey != null)
51 ApiKey = apiKey;
52 if (apiKeyPrefix != null)
53 ApiKeyPrefix = apiKeyPrefix;
54
55 TempFolderPath = tempFolderPath;
56 DateTimeFormat = dateTimeFormat;
57 Timeout = timeout;
58 }
59
60 /// <summary>
61 /// Initializes a new instance of the Configuration class.
62 /// </summary>
63 /// <param name="apiClient">Api client.</param>
64 public Configuration(ApiClient apiClient)
65 {
66 setApiClientUsingDefault(apiClient);
67 }
68
69 /// <summary>
70 /// Version of the package.
71 /// </summary>
72 /// <value>Version of the package.</value>
73 public const string Version = "1.0.0";
74
75 /// <summary>
76 /// Gets or sets the default Configuration.
77 /// </summary>
78 /// <value>Configuration.</value>
79 public static Configuration Default = new Configuration();
80
81 /// <summary>
82 /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
83 /// </summary>
84 /// <value>Timeout.</value>
85 public int Timeout
86 {
87 get { return ApiClient.RestClient.Timeout; }
88
89 set
90 {
91 if (ApiClient != null)
92 ApiClient.RestClient.Timeout = value;
93 }
94 }
95
96 /// <summary>
97 /// Gets or sets the default API client for making HTTP calls.
98 /// </summary>
99 /// <value>The API client.</value>
100 public ApiClient ApiClient;
101
102 /// <summary>
103 /// Set the ApiClient using Default or ApiClient instance.
104 /// </summary>
105 /// <param name="apiClient">An instance of ApiClient.</param>
106 /// <returns></returns>
107 public void setApiClientUsingDefault (ApiClient apiClient = null)
108 {
109 if (apiClient == null)
110 {
111 if (Default != null && Default.ApiClient == null)
112 Default.ApiClient = new ApiClient();
113
114 ApiClient = Default != null ? Default.ApiClient : new ApiClient();
115 }
116 else
117 {
118 if (Default != null && Default.ApiClient == null)
119 Default.ApiClient = apiClient;
120
121 ApiClient = apiClient;
122 }
123 }
124
125 private Dictionary<String, String> _defaultHeaderMap = new Dictionary<String, String>();
126
127 /// <summary>
128 /// Gets or sets the default header.
129 /// </summary>
130 public Dictionary<String, String> DefaultHeader
131 {
132 get { return _defaultHeaderMap; }
133
134 set
135 {
136 _defaultHeaderMap = value;
137 }
138 }
139
140 /// <summary>
141 /// Add default header.
142 /// </summary>
143 /// <param name="key">Header field name.</param>
144 /// <param name="value">Header field value.</param>
145 /// <returns></returns>
146 public void AddDefaultHeader(string key, string value)
147 {
148 _defaultHeaderMap.Add(key, value);
149 }
150
151 /// <summary>
152 /// Gets or sets the HTTP user agent.
153 /// </summary>
154 /// <value>Http user agent.</value>
155 public String UserAgent { get; set; }
156
157 /// <summary>
158 /// Gets or sets the username (HTTP basic authentication).
159 /// </summary>
160 /// <value>The username.</value>
161 public String Username { get; set; }
162
163 /// <summary>
164 /// Gets or sets the password (HTTP basic authentication).
165 /// </summary>
166 /// <value>The password.</value>
167 public String Password { get; set; }
168
169 /// <summary>
170 /// Gets or sets the access token for OAuth2 authentication.
171 /// </summary>
172 /// <value>The access token.</value>
173 public String AccessToken { get; set; }
174
175 /// <summary>
176 /// Gets or sets the API key based on the authentication name.
177 /// </summary>
178 /// <value>The API key.</value>
179 public Dictionary<String, String> ApiKey = new Dictionary<String, String>();
180
181 /// <summary>
182 /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
183 /// </summary>
184 /// <value>The prefix of the API key.</value>
185 public Dictionary<String, String> ApiKeyPrefix = new Dictionary<String, String>();
186
187 /// <summary>
188 /// Get the API key with prefix.
189 /// </summary>
190 /// <param name="apiKeyIdentifier">API key identifier (authentication scheme).</param>
191 /// <returns>API key with prefix.</returns>
192 public string GetApiKeyWithPrefix (string apiKeyIdentifier)
193 {
194 var apiKeyValue = "";
195 ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue);
196 var apiKeyPrefix = "";
197 if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix))
198 return apiKeyPrefix + " " + apiKeyValue;
199 else
200 return apiKeyValue;
201 }
202
203 private string _tempFolderPath = Path.GetTempPath();
204
205 /// <summary>
206 /// Gets or sets the temporary folder path to store the files downloaded from the server.
207 /// </summary>
208 /// <value>Folder path.</value>
209 public String TempFolderPath
210 {
211 get { return _tempFolderPath; }
212
213 set
214 {
215 if (String.IsNullOrEmpty(value))
216 {
217 _tempFolderPath = value;
218 return;
219 }
220
221 // create the directory if it does not exist
222 if (!Directory.Exists(value))
223 Directory.CreateDirectory(value);
224
225 // check if the path contains directory separator at the end
226 if (value[value.Length - 1] == Path.DirectorySeparatorChar)
227 _tempFolderPath = value;
228 else
229 _tempFolderPath = value + Path.DirectorySeparatorChar;
230 }
231 }
232
233 private const string ISO8601_DATETIME_FORMAT = "o";
234
235 private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
236
237 /// <summary>
238 /// Gets or sets the the date time format used when serializing in the ApiClient
239 /// By default, it's set to ISO 8601 - "o", for others see:
240 /// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
241 /// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
242 /// No validation is done to ensure that the string you're providing is valid
243 /// </summary>
244 /// <value>The DateTimeFormat string</value>
245 public String DateTimeFormat
246 {
247 get
248 {
249 return _dateTimeFormat;
250 }
251 set
252 {
253 if (string.IsNullOrEmpty(value))
254 {
255 // Never allow a blank or null string, go back to the default
256 _dateTimeFormat = ISO8601_DATETIME_FORMAT;
257 return;
258 }
259
260 // Caution, no validation when you choose date time format other than ISO 8601
261 // Take a look at the above links
262 _dateTimeFormat = value;
263 }
264 }
265
266 /// <summary>
267 /// Returns a string with essential information for debugging.
268 /// </summary>
269 public static String ToDebugReport()
270 {
271 String report = "C# SDK (IO.Swagger) Debug Report:\n";
272 report += " OS: " + Environment.OSVersion + "\n";
273 report += " .NET Framework Version: " + Assembly
274 .GetExecutingAssembly()
275 .GetReferencedAssemblies()
276 .Where(x => x.Name == "System.Core").First().Version.ToString() + "\n";
277 report += " Version of the API: 1.0.0\n";
278 report += " SDK Package Version: 1.0.0\n";
279
280 return report;
281 }
282 }
283}