HackerAPI is a .NET 8 web API that provides access to top stories from a hacker news-like service. The API uses caching to improve performance and reduce the number of requests to the external service.
- Fetch top stories
- Fetch story details by ID
- Caching to improve performance
- Unit tests with NUnit and Moq
- Clone the repository:
git clone https://github.com/yourusername/HackerAPI.git
cd HackerAPI
- Restore the NuGet packages:
dotnet restore
- Build the solution:
dotnet build
- Run the application:
dotnet run --project HackerAPI
- The API will be available at
https://localhost:5001.
GET /HackerAPI?count={count}: Fetch the top{count}stories.
The API base URI and endpoints are configured in the ApiUris class located in the SantanderTest.HackerAPI.Services.Constants namespace.
The project includes unit tests using NUnit and Moq. To run the tests, use the following command:
dotnet test
Test data is provided by the TestDataProvider class located in the HackerAPI.Tests namespace.
HackerAPI: Main project containing the API implementation.HackerAPI.Model: Contains the data models.HackerAPI.Services: Contains the service classes and constants.HackerAPI.Tests: Contains the unit tests.
- .NET 8
- Microsoft.Extensions.Caching.Memory
- Microsoft.Extensions.Logging
- Moq
- NUnit
- FluentAssertions
The retry policy is implemented using Polly to handle transient errors. It retries the request up to 3 times with an exponential backoff strategy.
The rate limit policy is implemented using Polly to prevent API overload. It limits the requests to 250 requests per 3 seconds with a maximum burst of 200 requests at once.
The HackerController includes detailed exception handling to provide meaningful error messages and appropriate HTTP status codes.
- RateLimitRejectedException: Thrown when the rate limit is exceeded. Returns a
429 Too Many Requestsstatus code. - HttpRequestException: Thrown for HTTP request errors. Returns a
503 Service Unavailablestatus code. - Exception: Catches any other unexpected exceptions. Returns a
500 Internal Server Errorstatus code.
The service logs important information such as the number of requests made, cache hits, and errors encountered. This helps in monitoring the rate limit usage and debugging issues.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.