Simple ASP.NET Core Web API created as a recruitment task.
The application retrieves a random cat fact from:
Each retrieved response is appended as a new line to a local .txt file.
- .NET 10
- ASP.NET Core Web API
- Dependency Injection
- HttpClientFactory
- xUnit
- Moq
GET /api/catfactsExample response:
{
"fact": "Cats sleep for around 16 hours a day.",
"length": 41
}Each response is also appended to:
CatFacts.Api/data/catfacts.txt
The external API URL, timeout and file path are stored in appsettings.json.
Example:
{
"CatFactApi": {
"BaseUrl": "https://catfact.ninja/",
"TimeoutSeconds": 10
},
"FileStorage": {
"Path": "data/catfacts.txt"
}
}dotnet restore
dotnet build
dotnet run --project CatFacts.ApiThen call:
GET /api/catfactsRun tests with:
dotnet testThe project contains a unit test for CatFactService using Moq.
HttpClientFactoryis used for communication with the external API.CancellationTokenis propagated through the request flow.SemaphoreSlimprotects concurrent writes to the local file.ILogger<T>is used for structured logging.IExceptionHandlerandProblemDetailsprovide centralized error handling.- Configuration values are stored in
appsettings.json.
The project intentionally does not use CQRS, MediatR, a database or additional architectural layers because they are not required for this small use case.