added fitness
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
class WgerClient:
|
||||
def __init__(self) -> None:
|
||||
settings = get_settings()
|
||||
self.base_url = settings.wger_base_url.rstrip("/")
|
||||
|
||||
def search_exercises(self, query: str, limit: int = 8) -> list[dict[str, Any]]:
|
||||
with httpx.Client(timeout=20.0) as client:
|
||||
response = client.get(
|
||||
f"{self.base_url}/exercise/search/",
|
||||
params={"term": query, "language": "ru"},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
sug = data.get("suggestions", data) if isinstance(data, dict) else []
|
||||
if isinstance(sug, dict):
|
||||
results = sug.get("results", [])
|
||||
elif isinstance(sug, list):
|
||||
results = sug
|
||||
else:
|
||||
results = []
|
||||
|
||||
out: list[dict[str, Any]] = []
|
||||
for item in results[:limit]:
|
||||
if isinstance(item, dict):
|
||||
name = item.get("value") or item.get("name") or str(item)
|
||||
out.append({"name": name, "data": item})
|
||||
elif isinstance(item, str):
|
||||
out.append({"name": item})
|
||||
if out:
|
||||
return out
|
||||
|
||||
response2 = client.get(
|
||||
f"{self.base_url}/exerciseinfo/",
|
||||
params={"language": 2, "limit": limit},
|
||||
)
|
||||
response2.raise_for_status()
|
||||
for item in (response2.json().get("results") or [])[:limit]:
|
||||
name = item.get("name") or f"#{item.get('id')}"
|
||||
if query.lower() in name.lower():
|
||||
out.append({"id": item.get("id"), "name": name, "category": item.get("category")})
|
||||
return out[:limit]
|
||||
Reference in New Issue
Block a user