Source code for cadrumo.application.exchange_rate_provider
"""Host-composed exchange-rate provider for euro conversion.Which rate authority converts foreign amounts is decided once by the processhost -- the ``aeat`` console script, the workbench, the MCP server -- and readhere by every conversion path. Application and adapter code never constructs atransport of its own, so a host that must not reach the network (a test run)composes a different provider without any branch in the code that converts."""from__future__importannotationsfromcollections.abcimportCallable,GeneratorfromcontextlibimportcontextmanagerfromcontextvarsimportContextVarfrom..core.errors.hierarchyimportInternalInvariantErrorfrom..domain.currency.serviceimportExchangeRateProvidertypeExchangeRateProviderFactory=Callable[[],ExchangeRateProvider]"""Return the host's provider; called on each use so importing it stays lazy."""_BOUND_EXCHANGE_RATE_PROVIDER_FACTORY:ContextVar[ExchangeRateProviderFactory]=ContextVar("cadrumo_exchange_rate_provider_factory",)
[docs]@contextmanagerdefbind_exchange_rate_provider_factory(factory:ExchangeRateProviderFactory,)->Generator[ExchangeRateProviderFactory]:"""Bind the host's exchange-rate provider factory for one runtime scope."""token=_BOUND_EXCHANGE_RATE_PROVIDER_FACTORY.set(factory)try:yieldfactoryfinally:_BOUND_EXCHANGE_RATE_PROVIDER_FACTORY.reset(token)
[docs]defexchange_rate_provider()->ExchangeRateProvider:"""Return the provider the process host composed."""try:factory=_BOUND_EXCHANGE_RATE_PROVIDER_FACTORY.get()exceptLookupErroraserror:raiseInternalInvariantError("the exchange-rate provider has not been composed by the host")fromerrorreturnfactory()