P.H.O, a sample project with some Clean Architecture patterns
This REST API provides a single endpoints to get the top three largest asteroids, if any, with potential risk of impact in the next specified days:
GET /asteroids?days=7
[
{
"name": "378358 (2007 LD)",
"diameter": 0.5991597306,
"velocity": 54127.4382012985,
"date": "2023-02-05"
},
{
"name": "(2022 UE1)",
"diameter": 0.31735300639999997,
"velocity": 55153.0307056723,
"date": "2023-02-04"
},
{
"name": "511684 (2015 BN509)",
"diameter": 0.29753812155,
"velocity": 43502.2461627361,
"date": "2023-02-04"
}
]
The service uses the Nasa NeoWs API to get the information about the asteroids. You need to register to get an API key.
Alternatively, you can use the DEMO_KEY
key, but you'll be limited to 30 requests per IP address per hour.
The easiest way to run the service is through Docker.
If you don't have Docker installed on your system, take a look here first.
This command will run a container named pho
, listening on port 5000 (choose another port number if that one is already in use):
docker image build -t pho:latest .
docker container run -d -p 5000:80 --name pho -e NasaNeoService__ApiKey=DEMO_KEY pho:latest
Alternatively, given that the Docker image is published to Docker Hub (repository paolofulgoni/pho), you don't need to build the image from source.
docker container run -d -p 5000:80 --name pho -e NasaNeoService__ApiKey=DEMO_KEY paolofulgoni/pho:latest
You can now call the endpoints with your tool of choice, e.g. HTTPie:
http "localhost:5000/asteroids?days=7"
If you prefer to use the Swagger UI, run the service in Development
mode and open your browser to http://localhost:5000/swagger
docker container run -d -p 5000:80 --name pho -e NasaNeoService__ApiKey=DEMO_KEY -e ASPNETCORE_ENVIRONMENT=Development paolofulgoni/pho
To stop the service, run this command:
docker container stop pho
When you're done, remove the container and the image:
docker container rm pho
docker image rm pho
You can easily run the service from your computer, but you'll have to compile it first. Therefore, you need to:
- Install the .NET 7 SDK
- Clone the repository locally
Then use the dotnet CLI to run the service. Make sure you're on the root folder of the project, then type:
dotnet run --project ./src/Pho.Web
This will use the Development
Hosting environment, therefore you can open a browser to http://localhost:5000/swagger and have fun with the Swagger UI.
Press CTRL+C
when you're done.
Make sure .NET 7 SDK is installed on your dev environment. Then just open the project with your IDE of choice.
If you want to build the project using the .NET CLI, run the following command from the project's root folder:
dotnet build
The project contains some unit and integration tests. You can run them with the following command:
dotnet test
The project is structured in the following way:
src/Pho.Web
- The ASP.NET Core Web API project, i.e. the entry point of the servicesrc/Pho.Core
- The core library, containing the business logic and the domain modelsrc/Pho.Infrastructure
- The infrastructure library, containing the implementation of the external HTTP calls to the Nasa API
Tests are in the test
folder, following the same structure as the source code.
A few things I wanted to do, but didn't have enough time:
- Add a cache layer, since the information returned by the API rarely changes and the third-party Nasa API has Rate Limits
- Add Integration tests, by mocking the third-party Nasa API
- Add retry and circuit breaker policies to external HTTP calls
- Hide the Nasa API key (which is in the querystring) from logs
- Move all NuGet packages versions to the
Directory.Packages.props
file - Allow days range bigger than 7 days, by making multiple calls to the Nasa API (the API allows only 7 days at a time)
- Nasa NeoWs - Nasa's RESTful web service for near earth Asteroid information
- Asteroid image - Artist’s impression of exiled asteroid 2004 EW95 CC BY 4.0 by ESO/M. Kornmesser