· 4 years ago · Jun 03, 2021, 08:28 AM
1// Decompiled with JetBrains decompiler
2// Type: Ory.Hydra.Client.Client.Configuration
3// Assembly: Ory.Hydra.Client, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
4// MVID: 35CAB0C4-A8FB-4735-8DBA-1A706EE4B395
5// Assembly location: C:\Users\VitaliiZhukov\.nuget\packages\ory.hydra.client\1.10.2\lib\netstandard2.0\Ory.Hydra.Client.dll
6
7using System;
8using System.Collections.Concurrent;
9using System.Collections.Generic;
10using System.IO;
11using System.Linq;
12using System.Net;
13using System.Security.Cryptography.X509Certificates;
14
15namespace Ory.Hydra.Client.Client
16{
17 /// <summary>Represents a set of configuration settings</summary>
18 public class Configuration : IReadableConfiguration
19 {
20 /// <summary>Version of the package.</summary>
21 /// <value>Version of the package.</value>
22 public const string Version = "1.10.2";
23 /// <summary>Identifier for ISO 8601 DateTime Format</summary>
24 /// <remarks>See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 for more information.</remarks>
25 public const string ISO8601_DATETIME_FORMAT = "o";
26 /// <summary>
27 /// Default creation of exceptions for a given method name and response object
28 /// </summary>
29 public static readonly ExceptionFactory DefaultExceptionFactory = (ExceptionFactory) ((methodName, response) =>
30 {
31 int statusCode = (int) response.StatusCode;
32 return statusCode >= 400 ? (Exception) new ApiException(statusCode, string.Format("Error calling {0}: {1}", (object) methodName, (object) response.RawContent), (object) response.RawContent, response.Headers) : (Exception) null;
33 });
34 /// <summary>
35 /// Defines the base path of the target API server.
36 /// Example: http://localhost:3000/v1/
37 /// </summary>
38 private string _basePath;
39 /// <summary>
40 /// Gets or sets the API key based on the authentication name.
41 /// This is the key and value comprising the "secret" for acessing an API.
42 /// </summary>
43 /// <value>The API key.</value>
44 private IDictionary<string, string> _apiKey;
45 /// <summary>
46 /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
47 /// </summary>
48 /// <value>The prefix of the API key.</value>
49 private IDictionary<string, string> _apiKeyPrefix;
50 private string _dateTimeFormat = "o";
51 private string _tempFolderPath = Path.GetTempPath();
52 /// <summary>Gets or sets the servers defined in the OpenAPI spec.</summary>
53 /// <value>The servers</value>
54 private IList<IReadOnlyDictionary<string, object>> _servers;
55
56 /// <summary>
57 /// Initializes a new instance of the <see cref="T:Ory.Hydra.Client.Client.Configuration" /> class
58 /// </summary>
59 public Configuration()
60 {
61 this.Proxy = (WebProxy) null;
62 this.UserAgent = "OpenAPI-Generator/1.10.2/csharp";
63 this.BasePath = "http://localhost";
64 this.DefaultHeaders = (IDictionary<string, string>) new ConcurrentDictionary<string, string>();
65 this.ApiKey = (IDictionary<string, string>) new ConcurrentDictionary<string, string>();
66 this.ApiKeyPrefix = (IDictionary<string, string>) new ConcurrentDictionary<string, string>();
67 this.Servers = (IList<IReadOnlyDictionary<string, object>>) new List<IReadOnlyDictionary<string, object>>()
68 {
69 (IReadOnlyDictionary<string, object>) new Dictionary<string, object>()
70 {
71 {
72 "url",
73 (object) ""
74 },
75 {
76 "description",
77 (object) "No description provided"
78 }
79 }
80 };
81 this.Timeout = 100000;
82 }
83
84 /// <summary>
85 /// Initializes a new instance of the <see cref="T:Ory.Hydra.Client.Client.Configuration" /> class
86 /// </summary>
87 public Configuration(
88 IDictionary<string, string> defaultHeaders,
89 IDictionary<string, string> apiKey,
90 IDictionary<string, string> apiKeyPrefix,
91 string basePath = "http://localhost")
92 : this()
93 {
94 if (string.IsNullOrWhiteSpace(basePath))
95 throw new ArgumentException("The provided basePath is invalid.", nameof (basePath));
96 if (defaultHeaders == null)
97 throw new ArgumentNullException(nameof (defaultHeaders));
98 if (apiKey == null)
99 throw new ArgumentNullException(nameof (apiKey));
100 if (apiKeyPrefix == null)
101 throw new ArgumentNullException(nameof (apiKeyPrefix));
102 this.BasePath = basePath;
103 foreach (KeyValuePair<string, string> defaultHeader in (IEnumerable<KeyValuePair<string, string>>) defaultHeaders)
104 this.DefaultHeaders.Add(defaultHeader);
105 foreach (KeyValuePair<string, string> keyValuePair in (IEnumerable<KeyValuePair<string, string>>) apiKey)
106 this.ApiKey.Add(keyValuePair);
107 foreach (KeyValuePair<string, string> keyValuePair in (IEnumerable<KeyValuePair<string, string>>) apiKeyPrefix)
108 this.ApiKeyPrefix.Add(keyValuePair);
109 }
110
111 /// <summary>Gets or sets the base path for API access.</summary>
112 public virtual string BasePath
113 {
114 get => this._basePath;
115 set => this._basePath = value;
116 }
117
118 /// <summary>Gets or sets the default header.</summary>
119 [Obsolete("Use DefaultHeaders instead.")]
120 public virtual IDictionary<string, string> DefaultHeader
121 {
122 get => this.DefaultHeaders;
123 set => this.DefaultHeaders = value;
124 }
125
126 /// <summary>Gets or sets the default headers.</summary>
127 public virtual IDictionary<string, string> DefaultHeaders { get; set; }
128
129 /// <summary>
130 /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
131 /// </summary>
132 public virtual int Timeout { get; set; }
133
134 /// <summary>Gets or sets the proxy</summary>
135 /// <value>Proxy.</value>
136 public virtual WebProxy Proxy { get; set; }
137
138 /// <summary>Gets or sets the HTTP user agent.</summary>
139 /// <value>Http user agent.</value>
140 public virtual string UserAgent { get; set; }
141
142 /// <summary>Gets or sets the username (HTTP basic authentication).</summary>
143 /// <value>The username.</value>
144 public virtual string Username { get; set; }
145
146 /// <summary>Gets or sets the password (HTTP basic authentication).</summary>
147 /// <value>The password.</value>
148 public virtual string Password { get; set; }
149
150 /// <summary>Gets the API key with prefix.</summary>
151 /// <param name="apiKeyIdentifier">API key identifier (authentication scheme).</param>
152 /// <returns>API key with prefix.</returns>
153 public string GetApiKeyWithPrefix(string apiKeyIdentifier)
154 {
155 string str1;
156 this.ApiKey.TryGetValue(apiKeyIdentifier, out str1);
157 string str2;
158 return this.ApiKeyPrefix.TryGetValue(apiKeyIdentifier, out str2) ? str2 + " " + str1 : str1;
159 }
160
161 /// <summary>
162 /// Gets or sets certificate collection to be sent with requests.
163 /// </summary>
164 /// <value>X509 Certificate collection.</value>
165 public X509CertificateCollection ClientCertificates { get; set; }
166
167 /// <summary>
168 /// Gets or sets the access token for OAuth2 authentication.
169 ///
170 /// This helper property simplifies code generation.
171 /// </summary>
172 /// <value>The access token.</value>
173 public virtual string AccessToken { get; set; }
174
175 /// <summary>
176 /// Gets or sets the temporary folder path to store the files downloaded from the server.
177 /// </summary>
178 /// <value>Folder path.</value>
179 public virtual string TempFolderPath
180 {
181 get => this._tempFolderPath;
182 set
183 {
184 if (string.IsNullOrEmpty(value))
185 {
186 this._tempFolderPath = Path.GetTempPath();
187 }
188 else
189 {
190 if (!Directory.Exists(value))
191 Directory.CreateDirectory(value);
192 if ((int) value[value.Length - 1] == (int) Path.DirectorySeparatorChar)
193 this._tempFolderPath = value;
194 else
195 this._tempFolderPath = value + Path.DirectorySeparatorChar.ToString();
196 }
197 }
198 }
199
200 /// <summary>
201 /// Gets or sets the date time format used when serializing in the ApiClient
202 /// By default, it's set to ISO 8601 - "o", for others see:
203 /// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
204 /// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
205 /// No validation is done to ensure that the string you're providing is valid
206 /// </summary>
207 /// <value>The DateTimeFormat string</value>
208 public virtual string DateTimeFormat
209 {
210 get => this._dateTimeFormat;
211 set
212 {
213 if (string.IsNullOrEmpty(value))
214 this._dateTimeFormat = "o";
215 else
216 this._dateTimeFormat = value;
217 }
218 }
219
220 /// <summary>
221 /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
222 ///
223 /// Whatever you set here will be prepended to the value defined in AddApiKey.
224 ///
225 /// An example invocation here might be:
226 /// <example>
227 /// ApiKeyPrefix["Authorization"] = "Bearer";
228 /// </example>
229 /// … where ApiKey["Authorization"] would then be used to set the value of your bearer token.
230 ///
231 /// <remarks>
232 /// OAuth2 workflows should set tokens via AccessToken.
233 /// </remarks>
234 /// </summary>
235 /// <value>The prefix of the API key.</value>
236 public virtual IDictionary<string, string> ApiKeyPrefix
237 {
238 get => this._apiKeyPrefix;
239 set => this._apiKeyPrefix = value != null ? value : throw new InvalidOperationException("ApiKeyPrefix collection may not be null.");
240 }
241
242 /// <summary>
243 /// Gets or sets the API key based on the authentication name.
244 /// </summary>
245 /// <value>The API key.</value>
246 public virtual IDictionary<string, string> ApiKey
247 {
248 get => this._apiKey;
249 set => this._apiKey = value != null ? value : throw new InvalidOperationException("ApiKey collection may not be null.");
250 }
251
252 /// <summary>Gets or sets the servers.</summary>
253 /// <value>The servers.</value>
254 public virtual IList<IReadOnlyDictionary<string, object>> Servers
255 {
256 get => this._servers;
257 set => this._servers = value != null ? value : throw new InvalidOperationException("Servers may not be null.");
258 }
259
260 /// <summary>
261 /// Returns URL based on server settings without providing values
262 /// for the variables
263 /// </summary>
264 /// <param name="index">Array index of the server settings.</param>
265 /// <return>The server URL.</return>
266 public string GetServerUrl(int index) => this.GetServerUrl(index, (Dictionary<string, string>) null);
267
268 /// <summary>Returns URL based on server settings.</summary>
269 /// <param name="index">Array index of the server settings.</param>
270 /// <param name="inputVariables">Dictionary of the variables and the corresponding values.</param>
271 /// <return>The server URL.</return>
272 public string GetServerUrl(int index, Dictionary<string, string> inputVariables)
273 {
274 if (index < 0 || index >= this.Servers.Count)
275 throw new InvalidOperationException(string.Format("Invalid index {0} when selecting the server. Must be less than {1}.", (object) index, (object) this.Servers.Count));
276 if (inputVariables == null)
277 inputVariables = new Dictionary<string, string>();
278 IReadOnlyDictionary<string, object> server = this.Servers[index];
279 string str = (string) server["url"];
280 foreach (KeyValuePair<string, object> keyValuePair in (IEnumerable<KeyValuePair<string, object>>) server["variables"])
281 {
282 IReadOnlyDictionary<string, object> readOnlyDictionary = (IReadOnlyDictionary<string, object>) keyValuePair.Value;
283 if (inputVariables.ContainsKey(keyValuePair.Key))
284 {
285 if (!((List<string>) readOnlyDictionary["enum_values"]).Contains(inputVariables[keyValuePair.Key]))
286 throw new InvalidOperationException(string.Format("The variable `{0}` in the server URL has invalid value #{1}. Must be {2}", (object) keyValuePair.Key, (object) inputVariables[keyValuePair.Key], (object) (List<string>) readOnlyDictionary["enum_values"]));
287 str = str.Replace("{" + keyValuePair.Key + "}", inputVariables[keyValuePair.Key]);
288 }
289 else
290 str = str.Replace("{" + keyValuePair.Key + "}", (string) readOnlyDictionary["default_value"]);
291 }
292 return str;
293 }
294
295 /// <summary>
296 /// Returns a string with essential information for debugging.
297 /// </summary>
298 public static string ToDebugReport() => "C# SDK (Ory.Hydra.Client) Debug Report:\n" + " OS: " + Environment.OSVersion?.ToString() + "\n" + " .NET Framework Version: " + Environment.Version?.ToString() + "\n" + " Version of the API: v1.10.2\n" + " SDK Package Version: 1.10.2\n";
299
300 /// <summary>Add Api Key Header.</summary>
301 /// <param name="key">Api Key name.</param>
302 /// <param name="value">Api Key value.</param>
303 /// <returns></returns>
304 public void AddApiKey(string key, string value) => this.ApiKey[key] = value;
305
306 /// <summary>Sets the API key prefix.</summary>
307 /// <param name="key">Api Key name.</param>
308 /// <param name="value">Api Key value.</param>
309 public void AddApiKeyPrefix(string key, string value) => this.ApiKeyPrefix[key] = value;
310
311 /// <summary>Merge configurations.</summary>
312 /// <param name="first">First configuration.</param>
313 /// <param name="second">Second configuration.</param>
314 /// <return>Merged configuration.</return>
315 public static IReadableConfiguration MergeConfigurations(
316 IReadableConfiguration first,
317 IReadableConfiguration second)
318 {
319 if (second == null)
320 return first ?? GlobalConfiguration.Instance;
321 Dictionary<string, string> dictionary1 = first.ApiKey.ToDictionary<KeyValuePair<string, string>, string, string>((Func<KeyValuePair<string, string>, string>) (kvp => kvp.Key), (Func<KeyValuePair<string, string>, string>) (kvp => kvp.Value));
322 Dictionary<string, string> dictionary2 = first.ApiKeyPrefix.ToDictionary<KeyValuePair<string, string>, string, string>((Func<KeyValuePair<string, string>, string>) (kvp => kvp.Key), (Func<KeyValuePair<string, string>, string>) (kvp => kvp.Value));
323 Dictionary<string, string> dictionary3 = first.DefaultHeaders.ToDictionary<KeyValuePair<string, string>, string, string>((Func<KeyValuePair<string, string>, string>) (kvp => kvp.Key), (Func<KeyValuePair<string, string>, string>) (kvp => kvp.Value));
324 foreach (KeyValuePair<string, string> keyValuePair in (IEnumerable<KeyValuePair<string, string>>) second.ApiKey)
325 dictionary1[keyValuePair.Key] = keyValuePair.Value;
326 foreach (KeyValuePair<string, string> keyValuePair in (IEnumerable<KeyValuePair<string, string>>) second.ApiKeyPrefix)
327 dictionary2[keyValuePair.Key] = keyValuePair.Value;
328 foreach (KeyValuePair<string, string> defaultHeader in (IEnumerable<KeyValuePair<string, string>>) second.DefaultHeaders)
329 dictionary3[defaultHeader.Key] = defaultHeader.Value;
330 return (IReadableConfiguration) new Configuration()
331 {
332 ApiKey = (IDictionary<string, string>) dictionary1,
333 ApiKeyPrefix = (IDictionary<string, string>) dictionary2,
334 DefaultHeaders = (IDictionary<string, string>) dictionary3,
335 BasePath = (second.BasePath ?? first.BasePath),
336 Timeout = second.Timeout,
337 Proxy = (second.Proxy ?? first.Proxy),
338 UserAgent = (second.UserAgent ?? first.UserAgent),
339 Username = (second.Username ?? first.Username),
340 Password = (second.Password ?? first.Password),
341 AccessToken = (second.AccessToken ?? first.AccessToken),
342 TempFolderPath = (second.TempFolderPath ?? first.TempFolderPath),
343 DateTimeFormat = (second.DateTimeFormat ?? first.DateTimeFormat)
344 };
345 }
346 }
347}
348