]> git.mxchange.org Git - fba.git/blob - fba/helpers/cookies.py
5a35602bda672028f32f56088832742f35643926
[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 # Cookies stor
18 _cookies = {}
19
20 def store (domain: str, cookies: dict):
21     # DEBUG: print(f"DEBUG: domain='{domain}',cookies()={len(cookies)} - CALLED!")
22     if not isinstance(domain, str):
23         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
24     elif domain == "":
25         raise ValueError("Parameter 'domain' is empty")
26     elif not isinstance(cookies, dict):
27         raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not 'dict'")
28
29     _cookies[domain] = cookies
30
31     # DEBUG: print(f"DEBUG: EXIT!")
32
33 def get_all(domain: str) -> dict:
34     # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
35     if not isinstance(domain, str):
36         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
37     elif domain == "":
38         raise ValueError("Parameter 'domain' is empty")
39     elif not has(domain):
40         raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?")
41
42     # DEBUG: print(f"DEBUG: _cookies[{domain}]()={len(_cookies[domain])} - EXIT!")
43     return _cookies[domain]
44
45 def has (domain: str) -> bool:
46     # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
47     if not isinstance(domain, str):
48         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
49     elif domain == "":
50         raise ValueError("Parameter 'domain' is empty")
51
52     has_cookies = domain in _cookies
53
54     # DEBUG: print(f"DEBUG: has_cookies='{has_cookies}' - EXIT!")
55     return has_cookies