return hashlib.sha256(domain.encode("utf-8")).hexdigest()
-def log_error(domain: str, response: requests.models.Response):
- # DEBUG: print("DEBUG: domain,response[]:", domain, type(response))
+def log_error(domain: str, error: dict):
+ # DEBUG: print("DEBUG: domain,error[]:", domain, type(error))
if not isinstance(domain, str):
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
# DEBUG: print(f"DEBUG: Writing to error_log is disabled in configuruation file - EXIT!")
return
- # DEBUG: print("DEBUG: BEFORE response[]:", type(response))
- if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
- response = f"response[{type(response)}]='{str(response)}'"
+ # DEBUG: print("DEBUG: BEFORE error[]:", type(error))
+ if isinstance(error, BaseException) or isinstance(error, json.decoder.JSONDecodeError):
+ error = f"error[{type(error)}]='{str(error)}'"
- # DEBUG: print("DEBUG: AFTER response[]:", type(response))
- if isinstance(response, str):
+ # DEBUG: print("DEBUG: AFTER error[]:", type(error))
+ if isinstance(error, str):
cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
domain,
- response,
+ error,
time.time()
])
else:
cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
domain,
- response.status_code,
- response.reason,
+ error["status_code"],
+ error["error_message"],
time.time()
])
# DEBUG: print("DEBUG: rel,href:", link["rel"], link["href"])
if link["rel"] in nodeinfo_identifier:
# DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
- response = fba.fetch_url(
+ data = network.fetch_api_url(
link["href"],
- network.api_headers,
(config.get("connection_timeout"), config.get("read_timeout"))
)
- data = network.json_from_response(response)
- # DEBUG: print("DEBUG: href,response.ok,response.status_code:", link["href"], response.ok, response.status_code)
- if response.ok and len(data) > 0:
+ # DEBUG: print("DEBUG: href,data[]:", link["href"], type(data))
+ if "json" in data:
# DEBUG: print("DEBUG: Found JSON nodeinfo():", len(data))
instances.set_data("detection_mode", domain, "AUTO_DISCOVERY")
instances.set_data("nodeinfo_url" , domain, link["href"])
# DEBUG: print("DEBUG: EXIT!")
-def update_last_error(domain: str, response: requests.models.Response):
- print("DEBUG: domain,response[]:", domain, type(response))
+def update_last_error(domain: str, error: dict):
+ print("DEBUG: domain,error[]:", domain, type(error))
if not isinstance(domain, str):
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
raise ValueError("Parameter 'domain' is empty")
- print("DEBUG: BEFORE response[]:", type(response))
- if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
- response = f"response[{type(response)}]='{str(response)}'"
+ print("DEBUG: BEFORE error[]:", type(error))
+ if isinstance(error, BaseException) or isinstance(error, json.decoder.JSONDecodeError):
+ error = f"error[{type(error)}]='{str(error)}'"
- print("DEBUG: AFTER response[]:", type(response))
- if isinstance(response, str):
- print(f"DEBUG: Setting last_error_details='{response}'")
+ print("DEBUG: AFTER error[]:", type(error))
+ if isinstance(error, str):
+ print(f"DEBUG: Setting last_error_details='{error}'")
set_data("last_status_code" , domain, 999)
- set_data("last_error_details", domain, response)
+ set_data("last_error_details", domain, error)
else:
- print(f"DEBUG: Setting last_error_details='{response.reason}'")
- set_data("last_status_code" , domain, response.status_code)
- set_data("last_error_details", domain, response.reason)
+ print(f"DEBUG: Setting last_error_details='{error['error_message']}'")
+ set_data("last_status_code" , domain, error["status_code"])
+ set_data("last_error_details", domain, error["reason"])
# Running pending updated
print(f"DEBUG: Invoking update_data({domain}) ...")
update_data(domain)
- fba.log_error(domain, response)
+ fba.log_error(domain, error)
print("DEBUG: EXIT!")
from fba import config
from fba import csrf
+from fba import fba
from fba import instances
# HTTP headers for non-API requests
# DEBUG: print(f"DEBUG: Returning json_reply({len(json_reply)})=[]:{type(json_reply)}")
return json_reply
+def fetch_api_url(url: str, timeout: tuple) -> dict:
+ # DEBUG: print(f"DEBUG: url='{url}',timeout()={len(timeout)} - CALLED!")
+ if not isinstance(url, str):
+ raise ValueError(f"Parameter url[]='{type(url)}' is not 'str'")
+ elif not isinstance(timeout, tuple):
+ raise ValueError(f"timeout[]={type(timeout)} is not 'tuple'")
+
+ json_reply = {
+ "status_code": 200,
+ }
+
+ try:
+ # DEBUG: print(f"DEBUG: Fetching url='{url}' ...")
+ response = fba.fetch_url(url, api_headers, timeout)
+
+ json_reply["json"] = json_from_response(response)
+
+ # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},json_reply[]='{type(json_reply)}'")
+ if not response.ok or response.status_code >= 400:
+ print(f"WARNING: Cannot query JSON API: url='{url}',response.status_code='{response.status_code}',json_reply[]='{type(json_reply)}'")
+ json_reply["status_code"] = response.status_code
+ json_reply["error_message"] = response.text
+
+ except requests.exceptions.ConnectionError as exception:
+ # DEBUG: print(f"DEBUG: Fetching '{url}' failed. exception[{type(exception)}]='{str(exception)}'")
+ json_reply["status_code"] = 999
+ json_reply["error_message"] = f"exception['{type(exception)}']='{str(exception)}'"
+ raise exception
+
+ # DEBUG: print(f"DEBUG: Returning json_reply({len(json_reply)})=[]:{type(json_reply)}")
+ return json_reply
+
def get_json_api(domain: str, path: str, timeout: tuple) -> dict:
# DEBUG: print(f"DEBUG: domain='{domain}',path='{path}',data='{data}',timeout()={len(timeout)} - CALLED!")
if not isinstance(domain, str):