-
-
Notifications
You must be signed in to change notification settings - Fork 28.8k
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
Whois component maintenance, PR 1 of 3: Light refactoring, adding abstraction #117749
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""A helper class that abstracts away the usage of external whois libraries.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
import whois | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class Domain: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be typing from upstream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take a look at the final code after all 3 planned PRs listed in the description. I believe it will help you understand why I decided to do it with a helper, instead of putting all the processing logic directly in HA classes. |
||
"""A class internally representing a domain.""" | ||
|
||
admin: Any = None | ||
creation_date: Any = None | ||
dnssec: Any = None | ||
expiration_date: Any = None | ||
last_updated: Any = None | ||
name_servers: Any = None | ||
owner: Any = None | ||
registrar: Any = None | ||
reseller: Any = None | ||
registrant: Any = None | ||
status: Any = None | ||
statuses: Any = None | ||
|
||
|
||
class WhoisUnknownTLD(Exception): | ||
"""Exception class when unknown TLD encountered.""" | ||
|
||
|
||
class GenericWhoisError(Exception): | ||
"""Exception class for all other exceptions originating from the external whois library.""" | ||
Comment on lines
+27
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should come from upstream |
||
|
||
|
||
def query(domain: str) -> Domain | None: | ||
"""Wrap around the external whois library call and return internal domain representation.""" | ||
|
||
wh = None | ||
try: | ||
wh = whois.query(domain) | ||
except whois.exceptions.UnknownTld as ex: | ||
raise WhoisUnknownTLD from ex | ||
except Exception as ex: | ||
raise GenericWhoisError from ex | ||
else: | ||
# backward-compatibility | ||
if wh is None: | ||
return None | ||
|
||
# field mapping here | ||
return Domain( | ||
admin=wh.admin, | ||
creation_date=wh.creation_date, | ||
dnssec=wh.dnssec, | ||
expiration_date=wh.expiration_date, | ||
last_updated=wh.last_updated, | ||
name_servers=wh.name_servers, | ||
owner=wh.owner, | ||
registrar=wh.registrar, | ||
reseller=wh.reseller, | ||
registrant=wh.registrant, | ||
status=wh.status, | ||
statuses=wh.statuses, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an uncommon level of abstraction for Home Assistant. This is basically why we use libraries.