Skip to content

Commit 0b1f0db

Browse files
committed
Testing if the domain is reachable
1 parent b825d82 commit 0b1f0db

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/osw/express.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from uuid import uuid4
1212
from warnings import warn
1313

14+
import requests
15+
from pydantic.v1 import validator
1416
from typing_extensions import (
1517
IO,
1618
Any,
@@ -64,6 +66,12 @@ class OswExpress(OSW):
6466
class Config:
6567
arbitrary_types_allowed = True
6668

69+
@validator("domain")
70+
def validate_domain(cls, v):
71+
pattern = r"^(?!-)[A-Za-z0-9.-]{1,63}(?<!-)\.[A-Za-z]{2,}$"
72+
assert re.match(pattern, v), "The domain is not valid."
73+
return v
74+
6775
@overload
6876
def __init__(
6977
self,
@@ -122,7 +130,19 @@ def __init__(
122130
# that filepath will be used
123131
else:
124132
cred_mngr.save_credentials_to_file()
125-
133+
# Test if domain is reachable
134+
try:
135+
url = f"https://{domain}/wiki/Main_Page"
136+
response = requests.get(url)
137+
if response.status_code == 200:
138+
pass # Domain is reachable
139+
else:
140+
raise ConnectionError(
141+
f"Could not connect to '{domain}'. "
142+
f"Response: {response.status_code}"
143+
)
144+
except Exception as e:
145+
raise ConnectionError(f"Could not connect to '{domain}'. Error: {e}")
126146
site = WtSite(WtSite.WtSiteConfig(iri=domain, cred_mngr=cred_mngr))
127147
super().__init__(**{"site": site, "domain": domain})
128148
self.cred_mngr = cred_mngr

0 commit comments

Comments
 (0)