]> git.mxchange.org Git - fba.git/blob - fba/helpers/cookies.py
Continued:
[fba.git] / fba / helpers / cookies.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import logging
18
19 from fba.helpers import domain as domain_helper
20
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
23
24 # Cookies storage
25 _cookies = {}
26
27 def store (domain: str, cookies: dict):
28     logger.debug("domain='%s',cookies()=%d - CALLED!", domain, len(cookies))
29     domain_helper.raise_on(domain)
30     if not isinstance(cookies, dict):
31         raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not of type 'dict'")
32
33     _cookies[domain] = cookies
34
35     logger.debug("EXIT!")
36
37 def get_all(domain: str) -> dict:
38     logger.debug("domain='%s' - CALLED!", domain)
39     domain_helper.raise_on(domain)
40
41     if domain not in _cookies:
42         return dict()
43
44     logger.debug("_cookies[%s]()=%d - EXIT!", domain, len(_cookies[domain]))
45     return _cookies[domain]
46
47 def has (domain: str) -> bool:
48     logger.debug("domain='%s' - CALLED!", domain)
49     domain_helper.raise_on(domain)
50
51     has_cookies = domain in _cookies
52
53     logger.debug("has_cookies='%s' - EXIT!", has_cookies)
54     return has_cookies
55
56 def clear (domain: str):
57     logger.debug("domain='%s' - CALLED!", domain)
58     domain_helper.raise_on(domain)
59
60     if has(domain):
61         logger.debug("Removing cookies for domain='%s' ...", domain)
62         del _cookies[domain]
63
64     logger.debug("EXIT!")