- Updated IMVDBApi to use Annotated types for query parameters and response models. - Removed commented-out sync_video_search method to clean up the code. - Refactored types.py to replace TypedDict with Pydantic's ModelBase for better validation and documentation. - Enhanced field descriptions in VideoType and SearchResponseType for clearer API documentation.
63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
from typing import Annotated, Any, Self
|
|
|
|
from beets import logging
|
|
from httpx_auth import HeaderApiKey
|
|
from lapidary.runtime import (
|
|
Body,
|
|
ClientBase,
|
|
Query,
|
|
Response,
|
|
Responses,
|
|
get,
|
|
)
|
|
|
|
from beetsplug.beets_music_videos.types import SearchResponseType
|
|
|
|
log = logging.getLogger("beets")
|
|
|
|
|
|
class IMVDBApi(ClientBase): # type: ignore
|
|
"""Lapidary-based IMVDB client. Use api_key in security; do not hardcode."""
|
|
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
base_url: str = "https://imvdb.com/api/v1",
|
|
**kwargs: Any,
|
|
) -> None: # type: ignore
|
|
# Some APIs return 500/502 for non-browser User-Agent; send a browser-like one.
|
|
headers = dict(kwargs.pop("headers", {}))
|
|
headers.setdefault(
|
|
"User-Agent",
|
|
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
)
|
|
super().__init__( # type: ignore
|
|
base_url=base_url,
|
|
security=[{"api_key": [api_key]}],
|
|
headers=headers,
|
|
**kwargs,
|
|
)
|
|
# Send API key as header IMVDB-APP-KEY on every request.
|
|
self.lapidary_authenticate(
|
|
api_key=HeaderApiKey(api_key, header_name="IMVDB-APP-KEY"),
|
|
)
|
|
|
|
@get("/search/videos") # type: ignore[misc]
|
|
def video_search(
|
|
self: Self,
|
|
*,
|
|
q: Annotated[str, Query()],
|
|
) -> Annotated[
|
|
SearchResponseType,
|
|
Responses(
|
|
responses={
|
|
"2xx": Response(Body({"application/json": SearchResponseType})),
|
|
}
|
|
),
|
|
]:
|
|
"""Search IMVDB; returns response with .results (list of VideoType).
|
|
|
|
Implemented at runtime by Lapidary; this stub is never executed.
|
|
"""
|
|
raise NotImplementedError
|