Skip to content

Commit ac3bc46

Browse files
committed
add new class that respects fully qualified URLs but validates Hosts are Anvil hosts
1 parent 28e4d3f commit ac3bc46

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

python_anvil/api_resources/requests.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,32 @@ class PlainRequest(BaseAnvilHttpRequest):
161161

162162
def get_url(self):
163163
return f"{self.API_HOST}/{self.API_BASE}"
164+
165+
166+
class FullyQualifiedRequest(BaseAnvilHttpRequest):
167+
"""A request class that validates URLs are fully qualified and point to Anvil domains."""
168+
169+
VALID_HOSTS = [
170+
"https://app.useanvil.com",
171+
# Future Anvil specific URLs
172+
]
173+
174+
def __init__(self, client, options=None):
175+
super().__init__(client, options)
176+
177+
def get_url(self):
178+
return "" # Not used since we expect full URLs
179+
180+
def _validate_url(self, url):
181+
if not any(url.startswith(host) for host in self.VALID_HOSTS):
182+
raise ValueError(
183+
f"URL must start with one of: {', '.join(self.VALID_HOSTS)}"
184+
)
185+
186+
def get(self, url, params=None, **kwargs):
187+
self._validate_url(url)
188+
return super().get(url, params, **kwargs)
189+
190+
def post(self, url, data=None, **kwargs):
191+
self._validate_url(url)
192+
return super().post(url, data, **kwargs)

0 commit comments

Comments
 (0)