Underground-Digital/Workflow-Engine
0
1import requests2 3from configs import dify_config4from models.api_based_extension import APIBasedExtensionPoint5 6 7class APIBasedExtensionRequestor:8 timeout: (int, int) = (5, 60)9 """timeout for request connect and read"""10 11 def __init__(self, api_endpoint: str, api_key: str) -> None:12 self.api_endpoint = api_endpoint13 self.api_key = api_key14 15 def request(self, point: APIBasedExtensionPoint, params: dict) -> dict:16 """17 Request the api.18 19 :param point: the api point20 :param params: the request params21 :return: the response json22 """23 headers = {"Content-Type": "application/json", "Authorization": "Bearer {}".format(self.api_key)}24 25 url = self.api_endpoint26 27 try:28 # proxy support for security29 proxies = None30 if dify_config.SSRF_PROXY_HTTP_URL and dify_config.SSRF_PROXY_HTTPS_URL:31 proxies = {32 "http": dify_config.SSRF_PROXY_HTTP_URL,33 "https": dify_config.SSRF_PROXY_HTTPS_URL,34 }35 36 response = requests.request(37 method="POST",38 url=url,39 json={"point": point.value, "params": params},40 headers=headers,41 timeout=self.timeout,42 proxies=proxies,43 )44 except requests.exceptions.Timeout:45 raise ValueError("request timeout")46 except requests.exceptions.ConnectionError:47 raise ValueError("request connection error")48 49 if response.status_code != 200:50 raise ValueError(51 "request error, status_code: {}, content: {}".format(response.status_code, response.text[:100])52 )53 54 return response.json()55 