11"""OpenRouter pricing service with dynamic pricing from API."""
22
33import logging
4- from typing import Any
4+ from typing import Any , Literal
55
66import httpx
7- from rsb .models .base_model import BaseModel
7+ from pydantic import PrivateAttr
8+ from rsb .models import BaseModel , Field
89
910from agentle .responses .pricing .modality import Modality
1011
@@ -28,31 +29,25 @@ class OpenRouterPricingService(BaseModel):
2829 _models_cache: Internal cache of model pricing data
2930 """
3031
31- type : str = "openrouter"
32+ type : Literal [ "openrouter" ] = Field ( default = "openrouter" )
3233 api_key : str | None = None
3334 base_url : str = "https://openrouter.ai/api/v1"
34- http_client : httpx .AsyncClient | None = None
35+ _http_client : httpx .AsyncClient | None = PrivateAttr ( default = None )
3536 _models_cache : dict [str , dict [str , Any ]] | None = None
3637
37- def __init__ (
38- self ,
39- api_key : str | None = None ,
40- base_url : str = "https://openrouter.ai/api/v1" ,
41- http_client : httpx .AsyncClient | None = None ,
42- ):
43- """
44- Initialize the OpenRouter pricing service.
38+ @property
39+ def http_client (self ) -> httpx .AsyncClient :
40+ if self ._http_client is None :
41+ raise ValueError ("Client is None." )
4542
46- Args:
47- api_key: OpenRouter API key. If not provided, reads from OPENROUTER_API_KEY env var.
48- base_url: Base URL for OpenRouter API
49- http_client: Optional custom HTTP client for requests
50- """
51- super ().__init__ ()
52- self .api_key = api_key
53- self .base_url = base_url
54- self .http_client = http_client
55- self ._models_cache = None
43+ return self ._http_client
44+
45+ def model_post_init (self , context : Any , / ) -> None :
46+ super ().model_post_init (context )
47+ self ._http_client = httpx .AsyncClient ()
48+
49+ def change_http_client (self , client : httpx .AsyncClient ) -> None :
50+ self ._http_client = client
5651
5752 async def _fetch_models (self ) -> dict [str , dict [str , Any ]]:
5853 """
@@ -109,8 +104,7 @@ async def _fetch_models(self) -> dict[str, dict[str, Any]]:
109104 self ._models_cache = {}
110105 return self ._models_cache
111106 finally :
112- if self .http_client is None :
113- await client .aclose ()
107+ await client .aclose ()
114108
115109 async def get_input_price_per_million (
116110 self ,
0 commit comments