]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 20 Jun 2023 12:56:07 +0000 (14:56 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 20 Jun 2023 12:56:07 +0000 (14:56 +0200)
- introduced cookies.has()
- only invoke cookies.get_all() when there are cookies stored

fba/helpers/cookies.py
fba/network.py

index 70f45cc2a1fdafd67875ce2ef2f1b0e88f2505af..5a35602bda672028f32f56088832742f35643926 100644 (file)
@@ -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
index 60486b905b7b03110bfc71011183a33da05b8249..1378859356d034e438c0bbdff630a9eff99813d9 100644 (file)
@@ -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: