import logging
import time
+from urllib.parse import urlparse
+
import reqto
import requests
import urllib3
try:
logger.debug("Fetching url='%s' ...", url)
- response = utils.fetch_url(url, api_headers, timeout)
+ response = fetch_url(url, api_headers, timeout)
logger.debug("response.ok='%s',response.status_code=%d,response.reason='%s'", response.ok, response.status_code, response.reason)
if response.ok and response.status_code == 200:
logger.debug("response[]='%s' - EXIT!", type(response))
return response
+
+def fetch_url(url: str, headers: dict, timeout: tuple) -> requests.models.Response:
+ logger.debug("url='%s',headers()=%d,timeout(%d)='%s' - CALLED!", url, len(headers), len(timeout), timeout)
+
+ if not isinstance(url, str):
+ raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
+ elif url == "":
+ raise ValueError("Parameter 'url' is empty")
+ elif not validators.url(url):
+ raise ValueError(f"Parameter url='{url}' is not a valid URL")
+ elif not isinstance(headers, dict):
+ raise ValueError(f"Parameter headers[]='{type(headers)}' is not of type 'dict'")
+ elif not isinstance(timeout, tuple):
+ raise ValueError(f"Parameter timeout[]='{type(timeout)}' is not of type 'tuple'")
+
+ logger.debug("Parsing url='%s' ...", url)
+ components = urlparse(url)
+
+ # Invoke other function, avoid trailing ?
+ logger.debug("components[%s]='%s'", type(components), components)
+ if components.query != "":
+ logger.debug("Fetching path='%s?%s' from netloc='%s' ...", components.path, components.query, components.netloc)
+ response = fetch_response(
+ components.netloc.split(":")[0],
+ f"{components.path}?{components.query}",
+ headers,
+ timeout
+ )
+ else:
+ logger.debug("Fetching path='%s' from netloc='%s' ...", components.path, components.netloc)
+ response = fetch_response(
+ components.netloc.split(":")[0],
+ components.path if isinstance(components.path, str) and components.path != '' else '/',
+ headers,
+ timeout
+ )
+
+ logger.debug("response[]='%s' - EXIT!", type(response))
+ return response