PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/NET6/Hermod/Hermod/HTTP/Request/APIKey_Id.cs

https://github.com/Vanaheimr/Hermod
C# | 387 lines | 150 code | 108 blank | 129 comment | 9 complexity | 5e113a6d01a07144ecfe48ac739e03b1 MD5 | raw file
  1. /*
  2. * Copyright (c) 2010-2022 GraphDefined GmbH <achim.friedland@graphdefined.com>
  3. * This file is part of Vanaheimr Hermod <https://www.github.com/Vanaheimr/Hermod>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #region Usings
  18. using System;
  19. using org.GraphDefined.Vanaheimr.Illias;
  20. #endregion
  21. namespace org.GraphDefined.Vanaheimr.Hermod.HTTP
  22. {
  23. /// <summary>
  24. /// Extension methods for API key identifications.
  25. /// </summary>
  26. public static class APIKeyIdExtensions
  27. {
  28. /// <summary>
  29. /// Indicates whether this API key identifications is null or empty.
  30. /// </summary>
  31. /// <param name="APIKey">An API key identifications.</param>
  32. public static Boolean IsNullOrEmpty(this APIKey_Id? APIKey)
  33. => !APIKey.HasValue || APIKey.Value.IsNullOrEmpty;
  34. /// <summary>
  35. /// Indicates whether this API key identifications is NOT null or empty.
  36. /// </summary>
  37. /// <param name="APIKey">An API key identifications.</param>
  38. public static Boolean IsNotNullOrEmpty(this APIKey_Id? APIKey)
  39. => APIKey.HasValue && APIKey.Value.IsNotNullOrEmpty;
  40. }
  41. /// <summary>
  42. /// An API key.
  43. /// </summary>
  44. public readonly struct APIKey_Id : IId,
  45. IEquatable<APIKey_Id>,
  46. IComparable<APIKey_Id>
  47. {
  48. #region Data
  49. /// <summary>
  50. /// The internal identification.
  51. /// </summary>
  52. private readonly String InternalId;
  53. /// <summary>
  54. /// Private non-cryptographic random number generator.
  55. /// </summary>
  56. private static readonly Random _random = new Random();
  57. #endregion
  58. #region Properties
  59. /// <summary>
  60. /// Indicates whether this identification is null or empty.
  61. /// </summary>
  62. public Boolean IsNullOrEmpty
  63. => InternalId.IsNullOrEmpty();
  64. /// <summary>
  65. /// Indicates whether this identification is NOT null or empty.
  66. /// </summary>
  67. public Boolean IsNotNullOrEmpty
  68. => InternalId.IsNotNullOrEmpty();
  69. /// <summary>
  70. /// The length of the API key identificator.
  71. /// </summary>
  72. public UInt64 Length
  73. => (UInt64) InternalId?.Length;
  74. #endregion
  75. #region Constructor(s)
  76. /// <summary>
  77. /// Create a new API key identification based on the given string.
  78. /// </summary>
  79. private APIKey_Id(String Text)
  80. {
  81. InternalId = Text;
  82. }
  83. #endregion
  84. #region (static) Random(Length)
  85. /// <summary>
  86. /// Create a random API key identification.
  87. /// </summary>
  88. /// <param name="Length">The expected length of the organization identification.</param>
  89. public static APIKey_Id Random(UInt16? Length = 64)
  90. => new APIKey_Id(_random.RandomString(Length ?? 64));
  91. #endregion
  92. #region Parse (Text)
  93. /// <summary>
  94. /// Parse the given string as an API key identification.
  95. /// </summary>
  96. /// <param name="Text">A text-representation of an API key identification.</param>
  97. public static APIKey_Id Parse(String Text)
  98. {
  99. if (TryParse(Text, out APIKey_Id apiKeyId))
  100. return apiKeyId;
  101. throw new ArgumentException("Invalid text-representation of an API key identification: '" + Text + "'!",
  102. nameof(Text));
  103. }
  104. #endregion
  105. #region TryParse(Text)
  106. /// <summary>
  107. /// Try to parse the given string as an API key identification.
  108. /// </summary>
  109. /// <param name="Text">A text-representation of an API key identification.</param>
  110. public static APIKey_Id? TryParse(String Text)
  111. {
  112. if (TryParse(Text, out APIKey_Id apiKeyId))
  113. return apiKeyId;
  114. return null;
  115. }
  116. #endregion
  117. #region TryParse(Text, out APIKeyId)
  118. /// <summary>
  119. /// Try to parse the given string as an API key identification.
  120. /// </summary>
  121. /// <param name="Text">A text-representation of an API key identification.</param>
  122. /// <param name="APIKeyId">The parsed API key identification.</param>
  123. public static Boolean TryParse(String Text, out APIKey_Id APIKeyId)
  124. {
  125. Text = Text?.Trim();
  126. if (Text.IsNotNullOrEmpty())
  127. {
  128. try
  129. {
  130. APIKeyId = new APIKey_Id(Text);
  131. return true;
  132. }
  133. catch
  134. { }
  135. }
  136. APIKeyId = default;
  137. return false;
  138. }
  139. #endregion
  140. #region Clone
  141. /// <summary>
  142. /// Clone this API key identification.
  143. /// </summary>
  144. public APIKey_Id Clone
  145. => new APIKey_Id(
  146. new String(InternalId?.ToCharArray())
  147. );
  148. #endregion
  149. #region Operator overloading
  150. #region Operator == (APIKeyIdId1, APIKeyIdId2)
  151. /// <summary>
  152. /// Compares two instances of this object.
  153. /// </summary>
  154. /// <param name="APIKeyIdId1">An API key identification.</param>
  155. /// <param name="APIKeyIdId2">Another API key identification.</param>
  156. /// <returns>true|false</returns>
  157. public static Boolean operator == (APIKey_Id APIKeyIdId1,
  158. APIKey_Id APIKeyIdId2)
  159. => APIKeyIdId1.Equals(APIKeyIdId2);
  160. #endregion
  161. #region Operator != (APIKeyIdId1, APIKeyIdId2)
  162. /// <summary>
  163. /// Compares two instances of this object.
  164. /// </summary>
  165. /// <param name="APIKeyIdId1">An API key identification.</param>
  166. /// <param name="APIKeyIdId2">Another API key identification.</param>
  167. /// <returns>true|false</returns>
  168. public static Boolean operator != (APIKey_Id APIKeyIdId1,
  169. APIKey_Id APIKeyIdId2)
  170. => !APIKeyIdId1.Equals(APIKeyIdId2);
  171. #endregion
  172. #region Operator < (APIKeyIdId1, APIKeyIdId2)
  173. /// <summary>
  174. /// Compares two instances of this object.
  175. /// </summary>
  176. /// <param name="APIKeyIdId1">An API key identification.</param>
  177. /// <param name="APIKeyIdId2">Another API key identification.</param>
  178. /// <returns>true|false</returns>
  179. public static Boolean operator < (APIKey_Id APIKeyIdId1,
  180. APIKey_Id APIKeyIdId2)
  181. => APIKeyIdId1.CompareTo(APIKeyIdId2) < 0;
  182. #endregion
  183. #region Operator <= (APIKeyIdId1, APIKeyIdId2)
  184. /// <summary>
  185. /// Compares two instances of this object.
  186. /// </summary>
  187. /// <param name="APIKeyIdId1">An API key identification.</param>
  188. /// <param name="APIKeyIdId2">Another API key identification.</param>
  189. /// <returns>true|false</returns>
  190. public static Boolean operator <= (APIKey_Id APIKeyIdId1,
  191. APIKey_Id APIKeyIdId2)
  192. => APIKeyIdId1.CompareTo(APIKeyIdId2) <= 0;
  193. #endregion
  194. #region Operator > (APIKeyIdId1, APIKeyIdId2)
  195. /// <summary>
  196. /// Compares two instances of this object.
  197. /// </summary>
  198. /// <param name="APIKeyIdId1">An API key identification.</param>
  199. /// <param name="APIKeyIdId2">Another API key identification.</param>
  200. /// <returns>true|false</returns>
  201. public static Boolean operator > (APIKey_Id APIKeyIdId1,
  202. APIKey_Id APIKeyIdId2)
  203. => APIKeyIdId1.CompareTo(APIKeyIdId2) > 0;
  204. #endregion
  205. #region Operator >= (APIKeyIdId1, APIKeyIdId2)
  206. /// <summary>
  207. /// Compares two instances of this object.
  208. /// </summary>
  209. /// <param name="APIKeyIdId1">An API key identification.</param>
  210. /// <param name="APIKeyIdId2">Another API key identification.</param>
  211. /// <returns>true|false</returns>
  212. public static Boolean operator >= (APIKey_Id APIKeyIdId1,
  213. APIKey_Id APIKeyIdId2)
  214. => APIKeyIdId1.CompareTo(APIKeyIdId2) >= 0;
  215. #endregion
  216. #endregion
  217. #region IComparable<APIKey_Id> Members
  218. #region CompareTo(Object)
  219. /// <summary>
  220. /// Compares two instances of this object.
  221. /// </summary>
  222. /// <param name="Object">An object to compare with.</param>
  223. public Int32 CompareTo(Object Object)
  224. => Object is APIKey_Id apiKeyId
  225. ? CompareTo(apiKeyId)
  226. : throw new ArgumentException("The given object is not an API key identification!",
  227. nameof(Object));
  228. #endregion
  229. #region CompareTo(APIKeyId)
  230. /// <summary>
  231. /// Compares two instances of this object.
  232. /// </summary>
  233. /// <param name="APIKeyId">An object to compare with.</param>
  234. public Int32 CompareTo(APIKey_Id APIKeyId)
  235. => String.Compare(InternalId,
  236. APIKeyId.InternalId,
  237. StringComparison.Ordinal);
  238. #endregion
  239. #endregion
  240. #region IEquatable<APIKey_Id> Members
  241. #region Equals(Object)
  242. /// <summary>
  243. /// Compares two instances of this object.
  244. /// </summary>
  245. /// <param name="Object">An object to compare with.</param>
  246. /// <returns>true|false</returns>
  247. public override Boolean Equals(Object Object)
  248. => Object is APIKey_Id apiKeyId &&
  249. Equals(apiKeyId);
  250. #endregion
  251. #region Equals(APIKeyId)
  252. /// <summary>
  253. /// Compares two API key identifications for equality.
  254. /// </summary>
  255. /// <param name="APIKeyId">An API key identification to compare with.</param>
  256. /// <returns>True if both match; False otherwise.</returns>
  257. public Boolean Equals(APIKey_Id APIKeyId)
  258. => String.Equals(InternalId,
  259. APIKeyId.InternalId,
  260. StringComparison.Ordinal);
  261. #endregion
  262. #endregion
  263. #region GetHashCode()
  264. /// <summary>
  265. /// Return the hash code of this object.
  266. /// </summary>
  267. /// <returns>The hash code of this object.</returns>
  268. public override Int32 GetHashCode()
  269. => InternalId?.GetHashCode() ?? 0;
  270. #endregion
  271. #region (override) ToString()
  272. /// <summary>
  273. /// Return a text-representation of this object.
  274. /// </summary>
  275. public override String ToString()
  276. => InternalId ?? "";
  277. #endregion
  278. }
  279. }