...

/

Solution: Hybrid Service Adapter for Fallback APIs

Solution: Hybrid Service Adapter for Fallback APIs

Wrap two currency conversion APIs behind a unified adapter that normalizes responses and automatically falls back when the primary fails.

We'll cover the following...

Solution explanation

  • Lines 2–7: We simulate the primary API, which sometimes fails to represent an unreliable third-party service.

    • Its successful response includes { ok, rate, vendor }.

    • When it fails, it throws an error—typical for modern async HTTP clients.

  • Lines 10–14: The fallback API always succeeds.

    • Its response format differs: { success, conversionRate, source }.

    • This mismatch is what the adapter will normalize.

  • Lines 17–20: The HybridCurrencyAdapter constructor accepts both clients.

    • The adapter itself doesn’t know which will succeed; it only coordinates ...