| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- class apikey:
- """
- That class represents API key. It store api key size, prefix and
- key itself. It could be converted into string, to store it in database
- and it would be created by the apikey_factory. Do not create it by hand.
- """
- def __init__(
- self,
- content: str,
- size: int,
- prefix: str,
- prefix_separator: str
- ) -> None:
- """
- That is constructor, it would be generated only by apikey_factory
- not manual in the app. Use apikey_factory, not that function.
- Parameters
- ----------
- content : str
- Full generated API key.
- size : int
- Size of the full API key.
- prefix : str
- API key prefix.
- prefix_separator : str
- Separation character between prefix and API key.
- """
-
- self.__content = content
- self.__size = size
- self.__prefix = prefix
- self.__prefix_separator = prefix_separator
- @property
- def content(self) -> str:
- """
- Full API key, which could be used in app or store in database.
- Returns
- -------
- str
- API key to use.
- """
- return self.__content
-
- @property
- def size(self) -> int:
- """
- Size of the full API key.
- Returns
- -------
- int
- API key size.
- """
- return self.__size
- @property
- def prefix(self) -> str:
- """
- Prefix of the API key. Static part before random characters.
- Returns
- -------
- str
- Prefix of the API key.
- """
- return self.__prefix
- @property
- def prefix_separator(self) -> str:
- """
- Separator between prefix and random part of the API key.
- Returns
- -------
- str
- Separator between prefix and random part API key.
- """
- return self.__prefix_separator
- @property
- def key(self) -> str:
- """
- API key as string. Alias of content property.
- Returns
- -------
- str
- API key as string.
- """
- return self.__content
- def compare(self, target: str | object) -> bool:
- """
- That compare API key with second API key. The second API key could
- be given as string or apikey object.
- Parameters
- ----------
- target : str
- API key to compare with.
- Returns
- -------
- bool
- Comparation result.
- """
- return self.__content == str(target)
- def __eq__(self, target: str | object) -> bool:
- """
- That is alias of compare function.
- Parameters
- ----------
- target : str
- API key to compare with.
- Returns
- -------
- bool
- Comparation result.
- """
- return self.compare(target)
- def __str__(self) -> str:
- """
- That convert API key to string, that mean return full API key.
- Returns
- -------
- str
- Fill API key.
- """
- return self.__content
- def __repr__(self) -> str:
- """
- That return debug info about API key.
-
- Returns
- -------
- str
- API key debug info.
- """
- return "API key: \"" + self.__content + "\""
|