Updated get_time tool
This commit is contained in:
parent
4aaade7e69
commit
f48a7bb12c
3 changed files with 39 additions and 14 deletions
|
@ -13,6 +13,16 @@ from typing import Literal
|
|||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class GetTimeInput(BaseModel):
|
||||
"""Represents the input for getting the current time.
|
||||
|
||||
Attributes:
|
||||
timezone: The IANA timezone for which to get the current time.
|
||||
"""
|
||||
|
||||
timezone: str = Field("UTC", description="IANA timezone name (e.g. 'UTC', 'Europe/London')")
|
||||
|
||||
|
||||
class ConvertTimeInput(BaseModel):
|
||||
"""Represents the input for converting a timestamp between timezones.
|
||||
|
||||
|
@ -74,13 +84,15 @@ class UnixToIsoInput(BaseModel):
|
|||
|
||||
|
||||
class TimeResponse(BaseModel):
|
||||
"""Represents the response containing the current UTC time.
|
||||
"""Represents the response containing the current time and timezone.
|
||||
|
||||
Attributes:
|
||||
utc: The current UTC time in ISO 8601 format.
|
||||
time: The current time in ISO 8601 format.
|
||||
tz: The timezone used for the time.
|
||||
"""
|
||||
|
||||
utc: str = Field(..., description="UTC time in ISO format")
|
||||
time: str = Field(..., description="Current time in ISO 8601 format")
|
||||
tz: str = Field(..., description="IANA timezone name used")
|
||||
|
||||
|
||||
class ConvertedTimeResponse(BaseModel):
|
||||
|
|
|
@ -14,7 +14,6 @@ import pytz
|
|||
from dateutil import parser as dateutil_parser
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from openapi_mcp_server.core.config import get_app_config
|
||||
from openapi_mcp_server.tools.base import BaseTool
|
||||
|
||||
from .models import (
|
||||
|
@ -22,6 +21,7 @@ from .models import (
|
|||
ConvertTimeInput,
|
||||
ElapsedTimeInput,
|
||||
ElapsedTimeResponse,
|
||||
GetTimeInput,
|
||||
ParsedTimestampResponse,
|
||||
ParseTimestampInput,
|
||||
TimeResponse,
|
||||
|
@ -56,7 +56,8 @@ class TimeTool(BaseTool):
|
|||
Defaults to 'UTC'.
|
||||
|
||||
Returns:
|
||||
A TimeResponse object containing the current time in ISO format.
|
||||
A TimeResponse object containing the current time in ISO format
|
||||
and the timezone used.
|
||||
|
||||
Raises:
|
||||
HTTPException: If the provided timezone is invalid.
|
||||
|
@ -64,7 +65,7 @@ class TimeTool(BaseTool):
|
|||
try:
|
||||
tz = pytz.timezone(timezone)
|
||||
now = datetime.now(tz)
|
||||
return TimeResponse(utc=now.isoformat())
|
||||
return TimeResponse(time=now.isoformat(), tz=timezone)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid timezone: {e}") from e
|
||||
|
||||
|
@ -195,18 +196,20 @@ class TimeTool(BaseTool):
|
|||
"""
|
||||
router = APIRouter()
|
||||
|
||||
@router.get(
|
||||
@router.post(
|
||||
"/get_time",
|
||||
response_model=TimeResponse,
|
||||
summary="Get current time in specified IANA timezone (defaults to UTC)",
|
||||
)
|
||||
def get_time(timezone: str = get_app_config().default_timezone) -> TimeResponse:
|
||||
def get_time(data: GetTimeInput | None = None) -> TimeResponse:
|
||||
"""Retrieves the current time in the specified timezone.
|
||||
|
||||
Returns:
|
||||
TimeResponse: The current time in the specified timezone.
|
||||
"""
|
||||
return TimeTool.get_current(timezone)
|
||||
if data is None:
|
||||
data = GetTimeInput()
|
||||
return TimeTool.get_current(data.timezone)
|
||||
|
||||
@router.post(
|
||||
"/unix_to_iso",
|
||||
|
|
|
@ -8,6 +8,7 @@ error conditions.
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from openapi_mcp_server.server import app
|
||||
|
@ -25,12 +26,21 @@ def test_get_current_time() -> None:
|
|||
"""Test the /time/get_time endpoint.
|
||||
|
||||
This test verifies that the endpoint returns the current time in the specified timezone.
|
||||
It checks for a 200 OK status code and that the response contains a non-empty 'utc' field.
|
||||
It checks for a 200 OK status code and that the response contains non-empty 'time' and 'tz' fields.
|
||||
"""
|
||||
response = client.get("/time/get_time?timezone=UTC")
|
||||
assert response.status_code == 200
|
||||
assert "utc" in response.json()
|
||||
assert response.json()["utc"] is not None
|
||||
response = client.post("/time/get_time", json={"timezone": "UTC"})
|
||||
if response.status_code != 200:
|
||||
pytest.fail(f"Expected status code 200, got {response.status_code}")
|
||||
|
||||
response_data = response.json()
|
||||
if "time" not in response_data:
|
||||
pytest.fail("Response missing 'time' field")
|
||||
if "tz" not in response_data:
|
||||
pytest.fail("Response missing 'tz' field")
|
||||
if response_data["time"] is None:
|
||||
pytest.fail("'time' field should not be None")
|
||||
if response_data["tz"] != "UTC":
|
||||
pytest.fail(f"Expected timezone 'UTC', got '{response_data['tz']}'")
|
||||
|
||||
|
||||
def test_unix_to_iso() -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue