]> git.mxchange.org Git - fba.git/blobdiff - fba/http/network.py
Continued:
[fba.git] / fba / http / network.py
index 5158c94e810e1deff26702ff9671bd968b59082f..43f1ae41114d135d72ff6ec1f780e65ce804d253 100644 (file)
@@ -17,6 +17,8 @@
 import logging
 import time
 
+from urllib.parse import urlparse
+
 import reqto
 import requests
 import urllib3
@@ -137,7 +139,7 @@ def fetch_api_url(url: str, timeout: tuple) -> dict:
 
     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:
@@ -305,3 +307,42 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple, allow_
 
     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