11import json
22import logging
3+ import re
34from http .client import HTTPResponse
45from typing import Dict , Union , List , Optional
56from urllib .error import HTTPError
@@ -16,15 +17,21 @@ class WebhookClient:
1617 logger = logging .getLogger (__name__ )
1718
1819 def __init__ (
19- self , url : str , timeout : int = 30 , default_headers : Dict [str , str ] = {}
20+ self ,
21+ url : str ,
22+ timeout : int = 30 ,
23+ proxy : Optional [str ] = None ,
24+ default_headers : Dict [str , str ] = {},
2025 ):
2126 """API client for Incoming Webhooks and response_url
2227 :param url: a complete URL to send data (e.g., https://hooks.slack.com/XXX)
2328 :param timeout: request timeout (in seconds)
29+ :param proxy: proxy URL (e.g., localhost:9000, http://localhost:9000)
2430 :param default_headers: request headers to add to all requests
2531 """
2632 self .url = url
2733 self .timeout = timeout
34+ self .proxy = proxy
2835 self .default_headers = default_headers
2936
3037 def send (
@@ -93,6 +100,10 @@ def _perform_http_request(
93100 req = Request (
94101 method = "POST" , url = url , data = body .encode ("utf-8" ), headers = headers
95102 )
103+ if self .proxy is not None :
104+ host = re .sub ("^https?://" , "" , self .proxy )
105+ req .set_proxy (host , "http" )
106+ req .set_proxy (host , "https" )
96107 else :
97108 raise SlackRequestError (f"Invalid URL detected: { url } " )
98109
0 commit comments