Add API key field to service configuration and support Bearer token authentication
This commit is contained in:
parent
4b53b0e778
commit
f7ef379a52
@ -44,7 +44,7 @@ def get_all_services() -> List[Dict]:
|
|||||||
print(f"Error getting services: {e}")
|
print(f"Error getting services: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def create_service(name: str, url: str, description: str = "") -> Dict:
|
def create_service(name: str, url: str, description: str = "", apikey: str = "") -> Dict:
|
||||||
"""Create a new Hermes service registration."""
|
"""Create a new Hermes service registration."""
|
||||||
try:
|
try:
|
||||||
service_id = str(uuid.uuid4())
|
service_id = str(uuid.uuid4())
|
||||||
@ -53,6 +53,7 @@ def create_service(name: str, url: str, description: str = "") -> Dict:
|
|||||||
"name": name,
|
"name": name,
|
||||||
"service_url": url,
|
"service_url": url,
|
||||||
"description": description,
|
"description": description,
|
||||||
|
"apikey": apikey, # Store API key for later use
|
||||||
"status": "pending",
|
"status": "pending",
|
||||||
"created_at": datetime.now().isoformat()
|
"created_at": datetime.now().isoformat()
|
||||||
}
|
}
|
||||||
@ -85,15 +86,20 @@ def get_service_by_id(service_id: str) -> Optional[Dict]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Service connection testing
|
# Service connection testing
|
||||||
def test_service_connection(url: str) -> Tuple[bool, str]:
|
def test_service_connection(url: str, apikey: str = "") -> Tuple[bool, str]:
|
||||||
"""Test connection to a Hermes service endpoint.
|
"""Test connection to a Hermes service endpoint.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple[bool, str]: (is_connected, status_message)
|
Tuple[bool, str]: (is_connected, status_message)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
# Prepare headers
|
||||||
|
headers = {}
|
||||||
|
if apikey:
|
||||||
|
headers["Authorization"] = f"Bearer {apikey}"
|
||||||
|
|
||||||
# Test the /health endpoint or similar
|
# Test the /health endpoint or similar
|
||||||
response = requests.get(f"{url.rstrip('/')}/health", timeout=10)
|
response = requests.get(f"{url.rstrip('/')}/health", headers=headers, timeout=10)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return True, "Connected"
|
return True, "Connected"
|
||||||
else:
|
else:
|
||||||
@ -125,6 +131,17 @@ def send_message_to_service(service_id: str, session_id: str, message: str) -> D
|
|||||||
raise ValueError(f"Service {service_id} not found")
|
raise ValueError(f"Service {service_id} not found")
|
||||||
|
|
||||||
service_url = service["service_url"]
|
service_url = service["service_url"]
|
||||||
|
apikey = service.get("apikey", "")
|
||||||
|
|
||||||
|
# Prepare headers
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add Authorization header if API key is provided
|
||||||
|
if apikey:
|
||||||
|
headers["Authorization"] = f"Bearer {apikey}"
|
||||||
|
|
||||||
# Call remote service API
|
# Call remote service API
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
f"{service_url.rstrip('/')}/api/chat",
|
f"{service_url.rstrip('/')}/api/chat",
|
||||||
@ -132,6 +149,7 @@ def send_message_to_service(service_id: str, session_id: str, message: str) -> D
|
|||||||
"session_id": session_id,
|
"session_id": session_id,
|
||||||
"message": message
|
"message": message
|
||||||
},
|
},
|
||||||
|
headers=headers,
|
||||||
timeout=30
|
timeout=30
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|||||||
@ -40,6 +40,13 @@
|
|||||||
"label": "Description",
|
"label": "Description",
|
||||||
"uitype": "str",
|
"uitype": "str",
|
||||||
"placeholder": "Optional description"
|
"placeholder": "Optional description"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "apikey",
|
||||||
|
"label": "API Key",
|
||||||
|
"uitype": "password",
|
||||||
|
"required": false,
|
||||||
|
"placeholder": "API key for authentication (optional)"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,7 +78,17 @@ bricks.RF.register('remove_service', async function(params) {
|
|||||||
|
|
||||||
bricks.RF.register('test_service_connection', async function(params) {
|
bricks.RF.register('test_service_connection', async function(params) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/hermes-web-cli/services/test/?id=' + params.service_id);
|
const formData = params.form_data || {};
|
||||||
|
const serviceId = params.service_id;
|
||||||
|
|
||||||
|
// Include API key in the test request
|
||||||
|
const response = await fetch('/hermes-web-cli/services/test/?id=' + serviceId, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ apikey: formData.apikey || '' })
|
||||||
|
});
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
if (result.status) {
|
if (result.status) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user