Skip to content

Commit

Permalink
feat: update API schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Jun 24, 2022
1 parent 10c3928 commit 116425c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
11 changes: 10 additions & 1 deletion abuse_whois/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from .errors import InvalidAddressError
from .ip import resolve_ip_address
from .schemas import Contact, Contacts
from .utils import get_hostname, is_domain, is_ip_address, is_supported_address
from .utils import (
get_hostname,
get_registered_domain,
is_domain,
is_ip_address,
is_supported_address,
)

try:
import importlib.metadata as importlib_metadata
Expand All @@ -27,10 +33,12 @@ async def get_abuse_contacts(address: str) -> Contacts:

hostname = get_hostname(address)
ip_address: Optional[str] = None
registered_domain: Optional[str] = None

shared_hosting_provider = get_shared_hosting_provider(hostname)

if is_domain(hostname):
registered_domain = get_registered_domain(hostname)
registrar = await get_contact_from_whois(hostname)

# get IP address by domain
Expand All @@ -49,6 +57,7 @@ async def get_abuse_contacts(address: str) -> Contacts:
address=address,
hostname=hostname,
ip_address=ip_address,
registered_domain=registered_domain,
shared_hosting_provider=shared_hosting_provider,
registrar=registrar,
hosting_provider=hosting_provider,
Expand Down
28 changes: 18 additions & 10 deletions abuse_whois/schemas/contact.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any, Literal, Optional

from pydantic import Field

Expand All @@ -8,9 +8,11 @@


class Contact(APIModel):
provider: str
address: str
type: str = Field(default="email")
provider: str = Field(..., description="Provider name")
address: str = Field(..., description="Contact address")
type: Literal["email", "form"] = Field(
"email", description="Type of contact method"
)

def __init__(self, **data: Any):
super().__init__(**data)
Expand All @@ -24,9 +26,15 @@ def __init__(self, **data: Any):

class Contacts(APIModel):
address: str
hostname: str
ip_address: Optional[str] = None

shared_hosting_provider: Optional[Contact] = None
registrar: Optional[Contact] = None
hosting_provider: Optional[Contact] = None
hostname: str = Field(..., description="Host name")

ip_address: Optional[str] = Field(None, description="IP address")
registered_domain: Optional[str] = Field(
None, description="Registered domain (a.k.a. free level domain)"
)

shared_hosting_provider: Optional[Contact] = Field(
None, description="Shared hosting provider"
)
registrar: Optional[Contact] = Field(None, description="Registrar")
hosting_provider: Optional[Contact] = Field(None, description="Hosting provider")
4 changes: 4 additions & 0 deletions abuse_whois/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pathlib
from functools import lru_cache
from typing import Dict, List, Optional, Union, cast
from urllib.parse import urlparse

Expand All @@ -8,6 +9,8 @@
from pydantic import BaseModel, ValidationError
from pydantic.networks import AnyHttpUrl

from abuse_whois import settings


class URLModel(BaseModel):
url: AnyHttpUrl
Expand Down Expand Up @@ -61,6 +64,7 @@ def is_supported_address(v: str) -> bool:
return False


@lru_cache(maxsize=settings.WHOIS_LOOKUP_CACHE_SIZE)
def get_registered_domain(v: str) -> Optional[str]:
parsed = tldextract.extract(v)

Expand Down

0 comments on commit 116425c

Please sign in to comment.