Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct docstrings and incorrect typehints #269

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

## [Unreleased]
- Updates `query_ipv4` method name to `query_address` to be more representative of what it actually does.
- Corrections to some docstrings and typehints.

## [3.1.2] - 2024-11-18
- Fixes a regression caused by [#258](https://github.com/dsgnr/portchecker.io/pull/258)
Expand Down
27 changes: 16 additions & 11 deletions backend/api/app/helpers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_ip_address(address: str) -> bool:
Attempts to parse the provided address string as an IP address.
Returns `True` if the address is valid, otherwise `False`.

Parameters:
Args:
address (str): The string to check for IP address validity.

Returns:
Expand All @@ -40,7 +40,7 @@ def is_address_valid(address: str) -> bool:
private and the environment variable `ALLOW_PRIVATE` is not set, an error is raised.
IPv6 addresses are not supported and will raise a `ValueError`.

Parameters:
Args:
address (str): The IPv4 address string to validate.

Returns:
Expand All @@ -67,7 +67,7 @@ def is_valid_hostname(hostname: str) -> bool:

Validates whether the provided hostname is resolveable.

Parameters:
Args:
hostname (str): The hostname to validate.

Returns:
Expand All @@ -90,27 +90,32 @@ def is_valid_hostname(hostname: str) -> bool:
raise ValueError("Hostname does not appear to resolve") from socket_err


def _check_port_status(address: str, port: int) -> list[str, int]:
def _check_port_status(address: str, port: int) -> dict[str, int | bool]:
"""Check if a specific port on the provided address is open.

Returns a dictionary with the port and the connection status.
Args:
address (str): The IP address to check.
port (int): The port to check on the given address.

Returns:
dict[str, int | bool]: Returns a dictionary with the port and the connection status.
"""
with socket.socket() as sock:
sock.settimeout(1) # Set a timeout of 1 second
result = sock.connect_ex((address, port)) # 0 if open, non-zero if closed
return {"port": port, "status": result == 0}


def check_ports(address: str, ports: dict[int]) -> list[dict[str, int]]:
def check_ports(address: str, ports: list[int]) -> list[dict[str, int | bool]]:
"""Check multiple ports for the provided address with threading.

Args:
address (str): The IP address to check.
address (str): The hostname or IPv4 address to query.
ports (list[int]): List of ports to check on the given address.
max_threads (int): Maximum number of threads to use. Default is 10.

Returns:
list[dict[str, int]]: A list of dictionaries containing port numbers and their statuses.
list[dict[str, int | bool]]:
A list of dictionaries containing the ports checked and their statuses.
"""
results = []
with ThreadPoolExecutor() as executor:
Expand All @@ -133,7 +138,7 @@ def query_address(address: str, ports: list[int]) -> list[dict]:
a resolvable hostname. Then attempts to establish a socket connection to each port
provided in `ports`.

Parameters:
Args:
address (str): The hostname or IPv4 address to query.
ports (list[int]): A list of port numbers to check for connectability.

Expand Down Expand Up @@ -167,7 +172,7 @@ def get_requester(request: Request) -> str:

If none of these headers are found, the function raises a `ValueError`.

Parameters:
Args:
request (Request): The HTTP request containing headers.

Returns:
Expand Down
4 changes: 2 additions & 2 deletions backend/api/app/routes/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
APISchema,
HostAnnotation,
PortAnnotation,
PortCheckAnnotation,
PortCheckStrAnnotation,
RequesterAnnotation,
)

Expand Down Expand Up @@ -43,7 +43,7 @@ def my_ip(request: Request) -> RequesterAnnotation:
@get("/api/{host:str}/{port:int}", media_type=MediaType.TEXT, sync_to_thread=False)
def get_port_check(
request: Request, host: HostAnnotation, port: PortAnnotation
) -> PortCheckAnnotation:
) -> PortCheckStrAnnotation:
"""
A `GET` endpoint to check the status of a specific port on a given
resolvable hostname or IPv4 address.
Expand Down
8 changes: 8 additions & 0 deletions backend/api/app/schemas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
Le(65535),
]

PortCheckStrAnnotation = Annotated[
str,
Parameter(
description="Whether the port was connectable",
examples=[Example(value="True")],
),
]

PortCheckAnnotation = Annotated[
bool,
Parameter(
Expand Down