]> 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 blacklist
20 from fba.helpers import domain as domain_helper
21
22 logging.basicConfig(level=logging.INFO)
23 logger = logging.getLogger(__name__)
24
25 # Cookies storage
26 _cookies = {}
27
28 def store(domain: str, cookies: dict):
29     logger.debug("domain='%s',cookies()=%d - CALLED!", domain, len(cookies))
30     domain_helper.raise_on(domain)
31
32     if blacklist.is_blacklisted(domain):
33         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
34     elif not isinstance(cookies, dict):
35         raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not of type 'dict'")
36
37     _cookies[domain] = cookies
38
39     logger.debug("EXIT!")
40
41 def get_all(domain: str) -> dict:
42     logger.debug("domain='%s' - CALLED!", domain)
43     domain_helper.raise_on(domain)
44
45     if blacklist.is_blacklisted(domain):
46         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
47     elif domain not in _cookies:
48         return dict()
49
50     logger.debug("_cookies[%s]()=%d - EXIT!", domain, len(_cookies[domain]))
51     return _cookies[domain]
52
53 def has(domain: str) -> bool:
54     logger.debug("domain='%s' - CALLED!", domain)
55     domain_helper.raise_on(domain)
56
57     if blacklist.is_blacklisted(domain):
58         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
59
60     has_cookies = domain in _cookies
61
62     logger.debug("has_cookies='%s' - EXIT!", has_cookies)
63     return has_cookies
64
65 def clear(domain: str):
66     logger.debug("domain='%s' - CALLED!", domain)
67     domain_helper.raise_on(domain)
68
69     if blacklist.is_blacklisted(domain):
70         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
71
72     if has(domain):
73         logger.debug("Removing cookies for domain='%s' ...", domain)
74         del _cookies[domain]
75
76     logger.debug("EXIT!")