mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""HTTP Request Node - make HTTP API calls
|
|
|
|
Node metadata is loaded from: ../../templates/metadata/nodes/http_request.yaml
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from ..entities import ExecutionContext
|
|
from ..node import WorkflowNode, workflow_node
|
|
|
|
@workflow_node('http_request')
|
|
class HTTPRequestNode(WorkflowNode):
|
|
"""HTTP request node - make HTTP API calls"""
|
|
|
|
category = 'action'
|
|
|
|
async def execute(self, inputs: dict[str, Any], context: ExecutionContext) -> dict[str, Any]:
|
|
import aiohttp
|
|
|
|
url = self.get_config('url', '')
|
|
method = self.get_config('method', 'GET')
|
|
timeout = self.get_config('timeout', 30)
|
|
content_type = self.get_config('content_type', 'application/json')
|
|
|
|
headers = inputs.get('headers', {})
|
|
headers['Content-Type'] = content_type
|
|
|
|
auth_type = self.get_config('auth_type', 'none')
|
|
auth_config = self.get_config('auth_config', {})
|
|
|
|
if auth_type == 'bearer':
|
|
headers['Authorization'] = f'Bearer {auth_config.get("token", "")}'
|
|
elif auth_type == 'api_key':
|
|
header_name = auth_config.get('header', 'X-API-Key')
|
|
headers[header_name] = auth_config.get('key', '')
|
|
|
|
body = inputs.get('body')
|
|
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.request(
|
|
method=method,
|
|
url=url,
|
|
json=body if content_type == 'application/json' else None,
|
|
data=body if content_type != 'application/json' else None,
|
|
headers=headers,
|
|
timeout=aiohttp.ClientTimeout(total=timeout),
|
|
) as response:
|
|
try:
|
|
response_data = await response.json()
|
|
except Exception:
|
|
response_data = await response.text()
|
|
|
|
return {
|
|
'response': response_data,
|
|
'status_code': response.status,
|
|
'headers': dict(response.headers),
|
|
}
|
|
except Exception as e:
|
|
return {'response': None, 'status_code': 0, 'headers': {}, 'error': str(e)}
|