move translator_client
This commit is contained in:
@@ -54,5 +54,5 @@ class EndpointGroup(BaseModel):
|
||||
languages: List[str]
|
||||
endpoints: List[Endpoint]
|
||||
translator_config: TranslatorConfig = TranslatorConfig()
|
||||
current_state: str = AnnouncementStates.IDLE
|
||||
current_state: AnnouncementStates = AnnouncementStates.IDLE
|
||||
anouncement_start_time: float = 0.0
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"""
|
||||
API client functions for interacting with the Translator API.
|
||||
"""
|
||||
import requests
|
||||
from typing import List, Optional, Dict, Any, Tuple
|
||||
from enum import Enum
|
||||
|
||||
|
||||
from multilang_translator.translator_api.translator_models import AnnouncementStates, Endpoint, EndpointGroup
|
||||
|
||||
|
||||
# This can be overridden through environment variables
|
||||
API_BASE_URL = "http://localhost:7999"
|
||||
|
||||
def get_groups() -> List[EndpointGroup]:
|
||||
"""Get all endpoint groups."""
|
||||
response = requests.get(f"{API_BASE_URL}/groups")
|
||||
response.raise_for_status()
|
||||
return [EndpointGroup.model_validate(group) for group in response.json()]
|
||||
|
||||
def get_group(group_id: int) -> Optional[EndpointGroup]:
|
||||
"""Get a specific endpoint group by ID."""
|
||||
response = requests.get(f"{API_BASE_URL}/groups/{group_id}")
|
||||
if response.status_code == 404:
|
||||
return None
|
||||
response.raise_for_status()
|
||||
return EndpointGroup.model_validate(response.json())
|
||||
|
||||
def create_group(group: EndpointGroup) -> EndpointGroup:
|
||||
"""Create a new endpoint group."""
|
||||
response = requests.post(f"{API_BASE_URL}/groups", json=group.model_dump())
|
||||
response.raise_for_status()
|
||||
return EndpointGroup.model_validate(response.json())
|
||||
|
||||
def update_group(group_id: int, updated_group: EndpointGroup) -> EndpointGroup:
|
||||
"""Update an existing endpoint group."""
|
||||
response = requests.put(f"{API_BASE_URL}/groups/{group_id}", json=updated_group.model_dump())
|
||||
response.raise_for_status()
|
||||
return EndpointGroup.model_validate(response.json())
|
||||
|
||||
def delete_group(group_id: int) -> None:
|
||||
"""Delete an endpoint group."""
|
||||
response = requests.delete(f"{API_BASE_URL}/groups/{group_id}")
|
||||
response.raise_for_status()
|
||||
|
||||
def start_announcement(text: str, group_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
Start a new announcement.
|
||||
|
||||
Args:
|
||||
text: The text content of the announcement
|
||||
group_id: The ID of the endpoint group to send the announcement to
|
||||
|
||||
Returns:
|
||||
Dictionary with status information
|
||||
"""
|
||||
response = requests.post(f"{API_BASE_URL}/announcement", params={"text": text, "group_id": group_id})
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def get_group_state(group_id: int) -> Tuple[str, float]:
|
||||
"""
|
||||
Get the status of the current announcement for a specific group.
|
||||
|
||||
Args:
|
||||
group_id: The ID of the group to check the announcement status for
|
||||
|
||||
Returns:
|
||||
Tuple containing (state_name, state_value)
|
||||
"""
|
||||
response = requests.get(f"{API_BASE_URL}/groups/{group_id}/state")
|
||||
response.raise_for_status()
|
||||
state_data = response.json()
|
||||
return (state_data["name"], state_data["value"])
|
||||
|
||||
|
||||
def get_available_endpoints() -> List[Endpoint]:
|
||||
"""Get all available endpoints."""
|
||||
response = requests.get(f"{API_BASE_URL}/endpoints")
|
||||
response.raise_for_status()
|
||||
endpoints_dict = response.json()
|
||||
# API returns a dictionary with endpoint IDs as keys
|
||||
# Convert this to a list of Endpoint objects
|
||||
return [Endpoint.model_validate(endpoint_data) for endpoint_id, endpoint_data in endpoints_dict.items()]
|
||||
|
||||
def get_available_languages() -> List[str]:
|
||||
"""Get all available languages for announcements."""
|
||||
response = requests.get(f"{API_BASE_URL}/languages")
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
Reference in New Issue
Block a user