選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

invoke.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. from abc import ABC
  18. import requests
  19. from agent.component.base import ComponentBase, ComponentParamBase
  20. class InvokeParam(ComponentParamBase):
  21. """
  22. Define the Crawler component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.proxy = None
  27. self.headers = ""
  28. self.method = "get"
  29. self.variables = []
  30. self.url = ""
  31. self.timeout = 60
  32. def check(self):
  33. self.check_valid_value(self.method.lower(), "Type of content from the crawler", ['get', 'post', 'put'])
  34. self.check_empty(self.url, "End point URL")
  35. self.check_positive_integer(self.timeout, "Timeout time in second")
  36. class Invoke(ComponentBase, ABC):
  37. component_name = "Invoke"
  38. def _run(self, history, **kwargs):
  39. args = {}
  40. for para in self._param.variables:
  41. if para.get("component_id"):
  42. cpn = self._canvas.get_component(para["component_id"])["obj"]
  43. _, out = cpn.output(allow_partial=False)
  44. args[para["key"]] = "\n".join(out["content"])
  45. else:
  46. args[para["key"]] = "\n".join(para["value"])
  47. url = self._param.url.strip()
  48. if url.find("http") != 0:
  49. url = "http://" + url
  50. method = self._param.method.lower()
  51. headers = {}
  52. if self._param.headers:
  53. headers = json.loads(self._param.headers)
  54. proxies = None
  55. if self._param.proxy:
  56. proxies = {"http": self._param.proxy, "https": self._param.proxy}
  57. if method == 'get':
  58. response = requests.get(url=url,
  59. params=args,
  60. headers=headers,
  61. proxies=proxies,
  62. timeout=self._param.timeout)
  63. return Invoke.be_output(response.text)
  64. if method == 'put':
  65. response = requests.put(url=url,
  66. data=args,
  67. headers=headers,
  68. proxies=proxies,
  69. timeout=self._param.timeout)
  70. return Invoke.be_output(response.text)