refactor ipfy source

This commit is contained in:
Dmitry Belyaev 2024-02-20 23:49:31 +03:00
parent 6949baf12e
commit 8a1ba06c88
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -2,20 +2,18 @@ import httpx
from pddnsc.base import BaseSourceProvider from pddnsc.base import BaseSourceProvider
# https://www.ipify.org/
class IPIFYSource(BaseSourceProvider): class IPIFYSource(BaseSourceProvider):
async def fetch_v4(self) -> str: async def fetch_v4(self) -> str:
async with httpx.AsyncClient(transport=self.ipv4t) as client: async with httpx.AsyncClient(transport=self.ipv4t) as client:
response = await client.get("https://api4.ipify.org/?format=json") response = await client.get("https://api4.ipify.org")
if response.status_code == httpx.codes.OK: if response.is_success:
data = response.json() result = response.text.strip() or None
result = None if not isinstance(data, dict) else data.get("ip")
return result return result
async def fetch_v6(self) -> str: async def fetch_v6(self) -> str:
async with httpx.AsyncClient(transport=self.ipv6t) as client: async with httpx.AsyncClient(transport=self.ipv6t) as client:
response = await client.get("https://api6.ipify.org/?format=json") response = await client.get("https://api6.ipify.org")
if response.status_code == httpx.codes.OK: if response.is_success:
data = response.json() result = response.text.strip() or None
result = None if not isinstance(data, dict) else data.get("ip")
return result return result