Create a small API which consolidates some key data from a third party. The API must connect to the third party API “CoinGecko”, the documentation for which can be found here:
https://www.coingecko.com/en/api/documentation.
The CoinGecko url is as follows:
https://api.coingecko.com/api/v3/
For demo purpose, high24, low24 and current price are integrated from coingecko api
- coins/markets
- simple/price
- A new service provider with crypto config is registered to bind the provider interface with implementation
- CryptoServiceInterface is used to define getPriceByCoin & getCoinsMarkets as common functions to fit all crypto providers
- CoinGeckoService.getPriceByCoin implements CryptoServiceInterface with its own CoinPriceRequest & CoinPriceResponse which adapt AbstractCoinPriceRequest & CryptoCoinPriceResponse
- CoinGeckoService.getCoinsMarkets implements CryptoServiceInterface with its own CoinsMarketsRequest & CoinMarketResponse which adapt AbstractCoinsMarketsRequest & CryptoCoinMarketResponse
- The UserConsumerService uses CryptoServiceInterface to wrap the data and returns
- Register a provider in CryptoServiceProvider
- Update config in config/crypto.php
- Create a "NewService" implements CryptoServiceInterface
- Create request and response to adapt CoinPriceRequest, CoinsMarketsRequest, CoinPriceResponse and CoinMarketResponse
- Update .env CRYPTO_PROVIDER to the new provider
- If we want to mix different crypto provider with different apis, we can split CryptoServiceInterface into smaller interface
- If we need more data from crypto provider, we can update CoinPriceResponse and CoinMarketResponse to load more as required
- If some third-party crypto provider has a different payload, we can define its own requests which adapt AbstractCoinPriceRequest and AbstractCoinsMarketsRequest
- If a third-party api is down, right now it returns 500 with and error message defined in Exception/Handler.php, and we can create different Exception json if necessary
- Api payload received
- UserConsumerRequest validates payload
- UserConsumerService is called with crypto service provider config
- CoinGeckoService is called in this case to load data from third-party api
- UserConsumerService returns UserConsumerResource containing all required data
- laravel framework 9
- form request validation
- service provider
- design patterns
- unit tests with mocking
- UserConsumerControllerTest & CoinGeckoServiceTest tests integrate with CoinGecko which will send a real request to this api
- UserConsumerServiceTest mocks the CoinGecko api calls
- start api
php artisan serve
- run unit tests
php artisan test