(20250210) Worked on the IP addr util a bit.
This commit is contained in:
@@ -160,6 +160,61 @@ def get_ipv4_range(ip_string, as_string = True):
|
||||
return start_ip, end_ip, count
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def to_hyphen_notation(value: str) -> str | None:
|
||||
|
||||
"""
|
||||
Takes in an IP pool (range) in either CIDR notation or already in hyphen-separated notation and parses it into the
|
||||
hyphen-separated notation.
|
||||
:param value: The IP-range in either of the accepted formats.
|
||||
:return: The IP range in hyphen-separated notation.
|
||||
"""
|
||||
|
||||
# Let's start by assuming failure:
|
||||
success = False
|
||||
start_ip = None
|
||||
end_ip = None
|
||||
|
||||
# Ensure that the input is treated as a string:
|
||||
value = str(value)
|
||||
|
||||
# First we check if the IP has been given in the CIDR notation:
|
||||
if not success:
|
||||
try:
|
||||
|
||||
# Try to extract the first and last IP addressed from the input:
|
||||
ip_net = ipaddress.IPv4Network(value, strict = False)
|
||||
start_ip = ip_net.network_address
|
||||
end_ip = ip_net.broadcast_address
|
||||
|
||||
success = True
|
||||
|
||||
# In case the CIDR interpretation doesn't work:
|
||||
except:
|
||||
success = False
|
||||
|
||||
# Now we try to parse the input string as a hyphen-separated input:
|
||||
if not success:
|
||||
try:
|
||||
|
||||
# Split at the hyphen and take the parts:
|
||||
parts = value.split("-")
|
||||
start_ip = parts[0]
|
||||
end_ip = parts[1]
|
||||
|
||||
success = True
|
||||
|
||||
# In case the hyphen-separated interpretation doesn't work:
|
||||
except:
|
||||
success = False
|
||||
|
||||
# Done here:
|
||||
value = f"{start_ip}-{end_ip}" if success else None
|
||||
return value
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
|
||||
@@ -95,13 +95,13 @@ import inspect
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class AsyncRestBase:
|
||||
class AsyncREST:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
http_client: httpx.AsyncClient = None,
|
||||
debug = True,
|
||||
debug_prefix = "GMail | ",
|
||||
debug_prefix = "REST (C) | ",
|
||||
debug_only_errors = True
|
||||
):
|
||||
|
||||
@@ -155,7 +155,8 @@ class AsyncRestBase:
|
||||
self,
|
||||
url: str,
|
||||
headers: dict = None,
|
||||
params: dict = None
|
||||
params: dict = None,
|
||||
auth: httpx.Auth = None
|
||||
) -> ApiResponse:
|
||||
|
||||
"""
|
||||
@@ -163,6 +164,7 @@ class AsyncRestBase:
|
||||
:param url: The URL to call.
|
||||
:param headers: The headers to pass.
|
||||
:param params: The params to send in the query string itself.
|
||||
:param auth: Any authentication credentials that need to be sent.
|
||||
:return: A structured response that includes the raw response, the exception (if any), and so on.
|
||||
"""
|
||||
|
||||
@@ -179,7 +181,8 @@ class AsyncRestBase:
|
||||
response = await self._http_client.get(
|
||||
url = url,
|
||||
headers = headers,
|
||||
params = params
|
||||
params = params,
|
||||
auth = auth
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
@@ -202,7 +205,8 @@ class AsyncRestBase:
|
||||
headers: dict = None,
|
||||
json: dict = None,
|
||||
data: dict = None,
|
||||
content: str | bytes = None
|
||||
content: str | bytes = None,
|
||||
auth: httpx.Auth = None
|
||||
) -> ApiResponse:
|
||||
|
||||
"""
|
||||
@@ -212,6 +216,7 @@ class AsyncRestBase:
|
||||
:param json: The params to send in the JSON body.
|
||||
:param data: The params to send in the form-data in the body.
|
||||
:param content: The raw content to be sent in the body (typically as an octet-stream).
|
||||
:param auth: Any authentication credentials that need to be sent.
|
||||
:return: A structured response that includes the raw response, the exception (if any), and so on.
|
||||
"""
|
||||
|
||||
@@ -230,7 +235,8 @@ class AsyncRestBase:
|
||||
headers = headers,
|
||||
json = json,
|
||||
data = data,
|
||||
content = content
|
||||
content = content,
|
||||
auth = auth
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
@@ -252,7 +258,8 @@ class AsyncRestBase:
|
||||
url: str,
|
||||
headers: dict = None,
|
||||
json: dict = None,
|
||||
data: dict = None
|
||||
data: dict = None,
|
||||
auth: httpx.Auth = None
|
||||
) -> ApiResponse:
|
||||
|
||||
"""
|
||||
@@ -261,6 +268,7 @@ class AsyncRestBase:
|
||||
:param headers: The headers to pass.
|
||||
:param json: The params to send in the JSON body.
|
||||
:param data: The params to send in the form-data in the body.
|
||||
:param auth: Any authentication credentials that need to be sent.
|
||||
:return: A structured response that includes the raw response, the exception (if any), and so on.
|
||||
"""
|
||||
|
||||
@@ -278,7 +286,8 @@ class AsyncRestBase:
|
||||
url = url,
|
||||
headers = headers,
|
||||
json = json,
|
||||
data = data
|
||||
data = data,
|
||||
auth = auth
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
@@ -298,13 +307,15 @@ class AsyncRestBase:
|
||||
async def delete(
|
||||
self,
|
||||
url: str,
|
||||
headers: dict = None
|
||||
headers: dict = None,
|
||||
auth: httpx.Auth = None
|
||||
) -> ApiResponse:
|
||||
|
||||
"""
|
||||
To call an API using the DELETE method.
|
||||
:param url: The URL to call.
|
||||
:param headers: The headers to pass.
|
||||
:param auth: Any authentication credentials that need to be sent.
|
||||
:return: A structured response that includes the raw response, the exception (if any), and so on.
|
||||
"""
|
||||
|
||||
@@ -320,7 +331,8 @@ class AsyncRestBase:
|
||||
# Make the API call:
|
||||
response = await self._http_client.delete(
|
||||
url = url,
|
||||
headers = headers
|
||||
headers = headers,
|
||||
auth = auth
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
|
||||
Reference in New Issue
Block a user