-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_session_manager.py
More file actions
120 lines (102 loc) · 4.28 KB
/
http_session_manager.py
File metadata and controls
120 lines (102 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
HTTP Session Manager for ComfyUI
Manages persistent HTTP sessions with configuration and state
"""
from typing import Dict, Any, Optional
from .http_client import HTTPClient
class HTTPSessionManager:
"""HTTP Session Manager Node for ComfyUI"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"session_name": ("STRING", {"default": "default_session"}),
"base_url": ("STRING", {"default": ""}),
"timeout": ("INT", {"default": 30, "min": 1, "max": 300}),
},
"optional": {
"headers": ("STRING", {"default": "{}"}),
"cookies": ("STRING", {"default": "{}"}),
"auth_type": (["none", "basic", "bearer", "api_key", "token"], {"default": "none"}),
"username": ("STRING", {"default": ""}),
"password": ("STRING", {"default": ""}),
"token": ("STRING", {"default": ""}),
"api_key": ("STRING", {"default": ""}),
"api_key_header": ("STRING", {"default": "X-API-Key"}),
"verify_ssl": ("BOOLEAN", {"default": True}),
"proxy_url": ("STRING", {"default": ""}),
"max_retries": ("INT", {"default": 3, "min": 1, "max": 10}),
"retry_delay": ("FLOAT", {"default": 1.0, "min": 0.1, "max": 10.0}),
}
}
RETURN_TYPES = ("HTTP_SESSION",)
RETURN_NAMES = ("session",)
FUNCTION = "create_session"
CATEGORY = "HTTP/Session"
# Global session storage
_sessions: Dict[str, HTTPClient] = {}
def create_session(self, session_name: str, base_url: str, timeout: int,
headers: str = "{}", cookies: str = "{}", auth_type: str = "none",
username: str = "", password: str = "", token: str = "",
api_key: str = "", api_key_header: str = "X-API-Key",
verify_ssl: bool = True, proxy_url: str = "",
max_retries: int = 3, retry_delay: float = 1.0):
"""Create or get existing HTTP session"""
# Create new client or get existing one
if session_name not in self._sessions:
self._sessions[session_name] = HTTPClient()
client = self._sessions[session_name]
# Configure client
client.set_timeout(timeout)
client.set_ssl_verify(verify_ssl)
client.max_retries = max_retries
client.retry_delay = retry_delay
# Set proxy if provided
if proxy_url:
client.set_proxy(proxy_url)
# Parse and set headers
try:
import json
headers_dict = json.loads(headers) if headers and headers != "{}" else {}
if headers_dict:
client.set_headers(headers_dict)
except Exception:
pass
# Parse and set cookies
try:
import json
cookies_dict = json.loads(cookies) if cookies and cookies != "{}" else {}
if cookies_dict:
client.set_cookies(cookies_dict)
except Exception:
pass
# Set authentication
if auth_type != "none":
client.set_auth(auth_type, username, password, token, api_key, api_key_header)
# Store session configuration
session_config = {
"client": client,
"base_url": base_url,
"session_name": session_name,
"timeout": timeout,
"verify_ssl": verify_ssl,
"max_retries": max_retries,
"retry_delay": retry_delay
}
return (session_config,)
@classmethod
def get_session(cls, session_name: str) -> Optional[HTTPClient]:
"""Get existing session by name"""
return cls._sessions.get(session_name)
@classmethod
def close_session(cls, session_name: str):
"""Close and remove session"""
if session_name in cls._sessions:
cls._sessions[session_name].close()
del cls._sessions[session_name]
@classmethod
def close_all_sessions(cls):
"""Close all sessions"""
for client in cls._sessions.values():
client.close()
cls._sessions.clear()