@@ -32,6 +32,56 @@ def __init__(self, client: WebhookHttpClient) -> None:
3232 """
3333 self ._client = client
3434
35+ def _build_webhook_urls (self , token : str , alias : str | None = None ) -> dict [str , str ]:
36+ """Build all URL variants for a webhook token.
37+
38+ Args:
39+ token: The webhook UUID
40+ alias: Optional custom alias
41+
42+ Returns:
43+ Dict with url, subdomain_url, api_url, email, and dns keys
44+ """
45+ identifier = alias if alias else token
46+ return {
47+ "url" : f"{ WEBHOOK_SITE_API } /{ identifier } " ,
48+ "subdomain_url" : f"https://{ token } .webhook.site" ,
49+ "api_url" : f"{ WEBHOOK_SITE_API } /token/{ token } " ,
50+ "email" : f"{ token } @email.webhook.site" ,
51+ "dns" : f"{ token } .dnshook.site" ,
52+ }
53+
54+ async def _validate_token_exists (self , webhook_token : str ) -> ToolResult | None :
55+ """Validate that a webhook token exists.
56+
57+ Args:
58+ webhook_token: The webhook UUID to validate
59+
60+ Returns:
61+ None if token is valid, ToolResult with error if invalid
62+ """
63+ try :
64+ await self ._client .get (f"/token/{ webhook_token } " )
65+ return None # Token is valid
66+ except WebhookApiError as e :
67+ if e .status_code == 404 :
68+ return ToolResult (
69+ success = False ,
70+ message = f"Token '{ webhook_token } ' not found or expired" ,
71+ data = None
72+ )
73+ return ToolResult (
74+ success = False ,
75+ message = f"Failed to validate token: { str (e )} " ,
76+ data = None
77+ )
78+ except Exception as e :
79+ return ToolResult (
80+ success = False ,
81+ message = f"Failed to validate token: { str (e )} " ,
82+ data = None
83+ )
84+
3585 async def create (self ) -> ToolResult :
3686 """Create a new webhook with default settings.
3787
@@ -44,23 +94,15 @@ async def create(self) -> ToolResult:
4494
4595 token = data .get ("uuid" )
4696 alias = data .get ("alias" )
47- url = f"{ WEBHOOK_SITE_API } /{ alias if alias else token } "
48- subdomain_url = f"https://{ token } .webhook.site"
49- email = f"{ token } @email.webhook.site"
50- api_url = f"{ WEBHOOK_SITE_API } /token/{ token } "
51- dns = f"{ token } .dnshook.site"
97+ urls = self ._build_webhook_urls (token , alias )
5298
5399 return ToolResult (
54100 success = True ,
55- message = f"Webhook created! Send requests to: { url } " ,
101+ message = f"Webhook created! Send requests to: { urls [ ' url' ] } " ,
56102 data = {
57103 "token" : token ,
58104 "alias" : alias ,
59- "url" : url ,
60- "subdomain_url" : subdomain_url ,
61- "api_url" : api_url ,
62- "email" : email ,
63- "dns" : dns ,
105+ ** urls ,
64106 "expires_at" : data .get ("expires_at" ),
65107 "default_status" : data .get ("default_status" ),
66108 "default_content" : data .get ("default_content" ),
@@ -87,23 +129,15 @@ async def create_with_config(self, config: WebhookConfig) -> ToolResult:
87129
88130 token = data .get ("uuid" )
89131 alias = data .get ("alias" )
90- url = f"{ WEBHOOK_SITE_API } /{ alias if alias else token } "
91- subdomain_url = f"https://{ token } .webhook.site"
92- email = f"{ token } @email.webhook.site"
93- api_url = f"{ WEBHOOK_SITE_API } /token/{ token } "
94- dns = f"{ token } .dnshook.site"
132+ urls = self ._build_webhook_urls (token , alias )
95133
96134 return ToolResult (
97135 success = True ,
98- message = f"Webhook created with custom config! URL: { url } " ,
136+ message = f"Webhook created with custom config! URL: { urls [ ' url' ] } " ,
99137 data = {
100138 "token" : token ,
101139 "alias" : alias ,
102- "url" : url ,
103- "subdomain_url" : subdomain_url ,
104- "api_url" : api_url ,
105- "email" : email ,
106- "dns" : dns ,
140+ ** urls ,
107141 "default_status" : data .get ("default_status" ),
108142 "default_content" : data .get ("default_content" ),
109143 "default_content_type" : data .get ("default_content_type" ),
@@ -241,27 +275,9 @@ async def get_url(self, webhook_token: str, validate: bool = False) -> ToolResul
241275 ToolResult with token and full URL
242276 """
243277 if validate :
244- try :
245- await self ._client .get (f"/token/{ webhook_token } " )
246- # If we get here, token is valid
247- except WebhookApiError as e :
248- if e .status_code == 404 :
249- return ToolResult (
250- success = False ,
251- message = f"Token '{ webhook_token } ' not found or expired" ,
252- data = None
253- )
254- return ToolResult (
255- success = False ,
256- message = f"Failed to validate token: { str (e )} " ,
257- data = None
258- )
259- except Exception as e :
260- return ToolResult (
261- success = False ,
262- message = f"Failed to validate token: { str (e )} " ,
263- data = None
264- )
278+ validation_error = await self ._validate_token_exists (webhook_token )
279+ if validation_error :
280+ return validation_error
265281
266282 url = f"{ WEBHOOK_SITE_API } /{ webhook_token } "
267283
@@ -287,27 +303,9 @@ async def get_email(self, webhook_token: str, validate: bool = False) -> ToolRes
287303 ToolResult with token, email address, and URL
288304 """
289305 if validate :
290- try :
291- await self ._client .get (f"/token/{ webhook_token } " )
292- # If we get here, token is valid
293- except WebhookApiError as e :
294- if e .status_code == 404 :
295- return ToolResult (
296- success = False ,
297- message = f"Token '{ webhook_token } ' not found or expired" ,
298- data = None
299- )
300- return ToolResult (
301- success = False ,
302- message = f"Failed to validate token: { str (e )} " ,
303- data = None
304- )
305- except Exception as e :
306- return ToolResult (
307- success = False ,
308- message = f"Failed to validate token: { str (e )} " ,
309- data = None
310- )
306+ validation_error = await self ._validate_token_exists (webhook_token )
307+ if validation_error :
308+ return validation_error
311309
312310 email = f"{ webhook_token } @email.webhook.site"
313311 url = f"{ WEBHOOK_SITE_API } /{ webhook_token } "
@@ -336,27 +334,9 @@ async def get_dns(self, webhook_token: str, validate: bool = False) -> ToolResul
336334 ToolResult with token, DNS domain, example subdomain usage, and URL
337335 """
338336 if validate :
339- try :
340- await self ._client .get (f"/token/{ webhook_token } " )
341- # If we get here, token is valid
342- except WebhookApiError as e :
343- if e .status_code == 404 :
344- return ToolResult (
345- success = False ,
346- message = f"Token '{ webhook_token } ' not found or expired" ,
347- data = None
348- )
349- return ToolResult (
350- success = False ,
351- message = f"Failed to validate token: { str (e )} " ,
352- data = None
353- )
354- except Exception as e :
355- return ToolResult (
356- success = False ,
357- message = f"Failed to validate token: { str (e )} " ,
358- data = None
359- )
337+ validation_error = await self ._validate_token_exists (webhook_token )
338+ if validation_error :
339+ return validation_error
360340
361341 dns_domain = f"{ webhook_token } .dnshook.site"
362342 url = f"{ WEBHOOK_SITE_API } /{ webhook_token } "
0 commit comments