Skip to content

Commit 6c85587

Browse files
committed
fix: validate engine_id and normalize api_url inputs
1 parent d48212e commit 6c85587

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/lingodotdev/engine.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
3434
@validator("api_url", pre=True, always=True)
3535
@classmethod
3636
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
37-
if v is None or v == "https://engine.lingo.dev":
38-
engine_id = values.get("engine_id")
39-
if engine_id:
40-
return "https://api.lingo.dev"
37+
default_url = "https://engine.lingo.dev"
4138
if v is None:
42-
return "https://engine.lingo.dev"
39+
v = default_url
4340
if not v.startswith(("http://", "https://")):
4441
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
45-
return v.rstrip("/")
42+
v = v.rstrip("/")
43+
if v == default_url and values.get("engine_id"):
44+
return "https://api.lingo.dev"
45+
return v
4646

4747

4848
class LocalizationParams(BaseModel):
@@ -600,6 +600,7 @@ async def quick_translate(
600600
source_locale: Source language code (optional, auto-detected if None)
601601
api_url: API endpoint URL
602602
fast: Enable fast mode for quicker translations
603+
engine_id: Optional engine ID for vNext API.
603604
604605
Returns:
605606
Translated content (same type as input)
@@ -619,7 +620,7 @@ async def quick_translate(
619620
"es"
620621
)
621622
"""
622-
config: Dict[str, Any] = {
623+
config = {
623624
"api_key": api_key,
624625
"api_url": api_url,
625626
}
@@ -662,6 +663,7 @@ async def quick_batch_translate(
662663
source_locale: Source language code (optional, auto-detected if None)
663664
api_url: API endpoint URL
664665
fast: Enable fast mode for quicker translations
666+
engine_id: Optional engine ID for vNext API.
665667
666668
Returns:
667669
List of translated content (one for each target locale)
@@ -674,7 +676,7 @@ async def quick_batch_translate(
674676
)
675677
# Results: ["Hola mundo", "Bonjour le monde", "Hallo Welt"]
676678
"""
677-
config: Dict[str, Any] = {
679+
config = {
678680
"api_key": api_key,
679681
"api_url": api_url,
680682
}

tests/test_engine.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ def setup_method(self):
652652
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
653653
self.engine = LingoDotDevEngine(self.config)
654654

655+
def teardown_method(self):
656+
"""Clean up engine client"""
657+
if self.engine._client and not self.engine._client.is_closed:
658+
asyncio.get_event_loop().run_until_complete(self.engine.close())
659+
655660
def test_engine_id_empty_string_treated_as_none(self):
656661
"""Test that empty engine_id is treated as None"""
657662
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})

0 commit comments

Comments
 (0)