From 6397213eacef096588eef50af5cf4c39a1f18158 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 20 Jun 2023 14:56:07 +0200 Subject: [PATCH] Continued: - introduced cookies.has() - only invoke cookies.get_all() when there are cookies stored --- fba/helpers/cookies.py | 14 +++++++++++++- fba/network.py | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/fba/helpers/cookies.py b/fba/helpers/cookies.py index 70f45cc..5a35602 100644 --- a/fba/helpers/cookies.py +++ b/fba/helpers/cookies.py @@ -36,8 +36,20 @@ def get_all(domain: str) -> dict: raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'") elif domain == "": raise ValueError("Parameter 'domain' is empty") - elif not domain in _cookies: + elif not has(domain): raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?") # DEBUG: print(f"DEBUG: _cookies[{domain}]()={len(_cookies[domain])} - EXIT!") return _cookies[domain] + +def has (domain: str) -> bool: + # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!") + if not isinstance(domain, str): + raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'") + elif domain == "": + raise ValueError("Parameter 'domain' is empty") + + has_cookies = domain in _cookies + + # DEBUG: print(f"DEBUG: has_cookies='{has_cookies}' - EXIT!") + return has_cookies diff --git a/fba/network.py b/fba/network.py index 60486b9..1378859 100644 --- a/fba/network.py +++ b/fba/network.py @@ -82,7 +82,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = {}) -> data=data, headers={**api_headers, **headers}, timeout=(config.get("connection_timeout"), config.get("read_timeout")), - cookies=cookies.get_all(domain) + cookies=cookies.get_all(domain) if cookies.has(domain) else {} ) json_reply["json"] = json_from_response(response) @@ -171,7 +171,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: f"https://{domain}{path}", headers={**api_headers, **headers}, timeout=timeout, - cookies=cookies.get_all(domain) + cookies=cookies.get_all(domain) if cookies.has(domain) else {} ) except exceptions as exception: @@ -269,7 +269,7 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple) -> req f"https://{domain}{path}", headers=headers, timeout=timeout, - cookies=cookies.get_all(domain) + cookies=cookies.get_all(domain) if cookies.has(domain) else {} ) except exceptions as exception: -- 2.39.5