ash-jc-allen / laravel-exchange-rates

A Laravel wrapper package for interacting with the exchangeratesapi.io API.
MIT License
449 stars 49 forks source link

Is it possible to creare a failover with this package ? #135

Closed michabbb closed 1 year ago

michabbb commented 1 year ago

hi,

so the config implies i have to choose a specific provider/driver. but a package that supports multiple providers is predestined to make use of a "failover". so when this package supports 3 providers, I would expect to have a config like:

return [
   'driver' => 'provider1',
   'faileover' => ['provider2','provider3']
];

so when fetching exchange rates from provider1 failed, the package should try to fetch the data from the "failovers" in exactly that order.

but my question is more general: is it possible to set the provider/driver by code at the current state of this package ?

thanks !

ash-jc-allen commented 1 year ago

Hey @michabbb!

I do think that the idea of having a failover driver could be a nice-to-have for the package. But at the moment, I've got no plans of adding anything like that, sorry.

At the moment, it's possible to choose your driver at runtime like shown in the docs (https://github.com/ash-jc-allen/laravel-exchange-rates#drivers). For example:

// Using the "ExchangeRate" class:
$result = app(ExchangeRate::class)
    ->driver('exchange-rates-data-api')
    ->exchangeRate('GBP', ['EUR', 'USD']);

// Using the "ExchangeRate" facade:
$result = ExchangeRate::driver('exchange-rates-data-api')
    ->exchangeRate('GBP', ['EUR', 'USD']);

However, because of the way I've built the package so far (without the intention of there being a failover), the drivers get the API key from the same config variable. So if you wanted to use a different service on the fly, I think you'd probably want to override the config when switching drivers. For example:

ExchangeRate::driver('exchange-rates-data-api')
    ->exchangeRate('GBP', ['EUR', 'USD']);

// If the call didn't work, try with a different provider...

config(['laravel-exchange-rates.api_key' => 'MY OTHER API KEY']);

ExchangeRate::driver('exchange-rate-host')
    ->exchangeRate('GBP', ['EUR', 'USD']);

It's not ideal, but it should hopefully do the job 🙂

michabbb commented 1 year ago

Thanks for the detailed answer 👍🙏