Coverage for functions \ flipdare \ core \ ip_address_dto.py: 94%
35 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
1#!/usr/bin/env python
2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved.
3#
4# This file is part of Flipdare's proprietary software and contains
5# confidential and copyrighted material. Unauthorised copying,
6# modification, distribution, or use of this file is strictly
7# prohibited without prior written permission from Flipdare Pty Ltd.
8#
9# This software includes third-party components licensed under MIT,
10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details.
11#
14import ipaddress
15from typing import override
18class IpAddressDTO:
19 __slots__ = ("_ip_address",)
21 _ip_address: str
23 def __init__(self, *, _internal: bool = False) -> None:
24 if not _internal:
25 raise RuntimeError("Use IpAddress.create()")
27 @classmethod
28 def create(cls, ip: str) -> "IpAddressDTO":
30 from flipdare.app_env import get_app_environment
32 def _get_validation_error(ip_str: str) -> str | None:
33 try:
34 addr = ipaddress.ip_address(ip_str)
35 if addr.is_loopback and not get_app_environment().is_dev_test:
36 return f"Cant use loopback: {ip_str}"
37 return None
38 except ValueError:
39 return f"Invalid IP address: {ip_str}"
41 ip = ip.strip()
42 # Manually exclude localhost if needed
43 validation_error = _get_validation_error(ip)
44 if validation_error is not None:
45 raise ValueError(validation_error)
47 instance = cls(_internal=True)
48 instance._ip_address = ip
49 return instance
51 @property
52 def address(self) -> str:
53 return self._ip_address
55 @override
56 def __str__(self) -> str:
57 return self._ip_address
59 @override
60 def __repr__(self) -> str:
61 return f"IpAddressDTO(ip_address='{self._ip_address}')"