apikey.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. class apikey:
  2. """
  3. That class represents API key. It store api key size, prefix and
  4. key itself. It could be converted into string, to store it in database
  5. and it would be created by the apikey_factory. Do not create it by hand.
  6. """
  7. def __init__(
  8. self,
  9. content: str,
  10. size: int,
  11. prefix: str,
  12. prefix_separator: str
  13. ) -> None:
  14. """
  15. That is constructor, it would be generated only by apikey_factory
  16. not manual in the app. Use apikey_factory, not that function.
  17. Parameters
  18. ----------
  19. content : str
  20. Full generated API key.
  21. size : int
  22. Size of the full API key.
  23. prefix : str
  24. API key prefix.
  25. prefix_separator : str
  26. Separation character between prefix and API key.
  27. """
  28. self.__content = content
  29. self.__size = size
  30. self.__prefix = prefix
  31. self.__prefix_separator = prefix_separator
  32. @property
  33. def content(self) -> str:
  34. """
  35. Full API key, which could be used in app or store in database.
  36. Returns
  37. -------
  38. str
  39. API key to use.
  40. """
  41. return self.__content
  42. @property
  43. def size(self) -> int:
  44. """
  45. Size of the full API key.
  46. Returns
  47. -------
  48. int
  49. API key size.
  50. """
  51. return self.__size
  52. @property
  53. def prefix(self) -> str:
  54. """
  55. Prefix of the API key. Static part before random characters.
  56. Returns
  57. -------
  58. str
  59. Prefix of the API key.
  60. """
  61. return self.__prefix
  62. @property
  63. def prefix_separator(self) -> str:
  64. """
  65. Separator between prefix and random part of the API key.
  66. Returns
  67. -------
  68. str
  69. Separator between prefix and random part API key.
  70. """
  71. return self.__prefix_separator
  72. @property
  73. def key(self) -> str:
  74. """
  75. API key as string. Alias of content property.
  76. Returns
  77. -------
  78. str
  79. API key as string.
  80. """
  81. return self.__content
  82. def compare(self, target: str | object) -> bool:
  83. """
  84. That compare API key with second API key. The second API key could
  85. be given as string or apikey object.
  86. Parameters
  87. ----------
  88. target : str
  89. API key to compare with.
  90. Returns
  91. -------
  92. bool
  93. Comparation result.
  94. """
  95. return self.__content == str(target)
  96. def __eq__(self, target: str | object) -> bool:
  97. """
  98. That is alias of compare function.
  99. Parameters
  100. ----------
  101. target : str
  102. API key to compare with.
  103. Returns
  104. -------
  105. bool
  106. Comparation result.
  107. """
  108. return self.compare(target)
  109. def __str__(self) -> str:
  110. """
  111. That convert API key to string, that mean return full API key.
  112. Returns
  113. -------
  114. str
  115. Fill API key.
  116. """
  117. return self.__content
  118. def __repr__(self) -> str:
  119. """
  120. That return debug info about API key.
  121. Returns
  122. -------
  123. str
  124. API key debug info.
  125. """
  126. return "API key: \"" + self.__content + "\""