This project is a .NET application that retrieves and stores data from NASA's Astronomy Picture of the Day (APOD) API. It provides endpoints to access APOD data for specific dates or date ranges, with local storage in a MongoDB database to enhance performance and avoid duplicate requests. When querying for a large date range, requests to the official APOD API can take a considerable amount of time. By caching the data in the MongoDB database, this service provides much faster responses once the data is stored.
- Fetch APOD data for today, a specific date, or a range of dates.
- Store retrieved data locally in a MongoDB database.
- Avoid duplicate entries by checking the local database before making API requests.
- Automatic Data Fetching: APOD data is automatically fetched every day at 10 AM UTC.
- C#: The primary programming language for the application.
- ASP.NET Core: Framework for building the web API.
- MongoDB: NoSQL database for local storage.
- HttpClient: To make HTTP requests to NASA's APOD API.
- Newtonsoft.Json: For JSON serialization and deserialization.
To install and run this project locally, follow these steps:
-
Clone the Repository:
git clone https://github.com/HugoB07/NasaApodApi.git cd NasaApodApi/NasaApodApi -
Install Dependencies: Make sure you have the .NET SDK installed. Then run the following command to restore the project dependencies:
dotnet restore
-
Set Up MongoDB: Ensure you have a MongoDB instance running. You can install MongoDB locally or use a cloud instance. Update the
ConnectionStringinappsettings.jsonaccordingly. -
Configure appsettings.json: Update the configuration settings as described in the Setup section.
-
Run the Application: To start the application, use the following command:
dotnet run
-
Access the API: The API will be running at
http://localhost:5269/swagger.
The application includes a background service that fetches the latest APOD data daily at 10 AM UTC. You don't need to perform any additional configuration for this feature; simply running the application will ensure that the latest data is always available.
To configure the application, you will need to set up an appsettings.json file. Below is a sample configuration:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MongoDb": {
"ConnectionString": "mongodb://localhost:27017",
"DbName": "nasa",
"ApodCollectionName": "apod"
},
"NASA": {
"ApiKey": "DEMO_KEY" // Replace with your actual API key from NASA
}
}-
MongoDB Configuration:
ConnectionString: Set the connection string for your MongoDB instance (default ismongodb://localhost:27017).DbName: Specify the name of the database (e.g.,nasa).ApodCollectionName: Name of the collection to store APOD data (e.g.,apod).
-
NASA API Key:
- Replace
DEMO_KEYwith your actual NASA API key, which you can obtain from the NASA API portal.
- Replace
- Endpoint:
GET /apod/today - Description: Retrieves the Astronomy Picture of the Day for today.
- Response: Returns the APOD data or a 404 error if not found.
- Endpoint:
GET /apod/by_date - Query Parameter:
date(format: YYYY-MM-DD) - Description: Retrieves the APOD data for a specified date.
- Response: Returns the APOD data or a 404 error if not found. Returns a 400 error if the date format is invalid.
- Endpoint:
GET /apod/by_range_date - Query Parameters:
startDate(format: YYYY-MM-DD)endDate(format: YYYY-MM-DD)
- Description: Retrieves APOD data for a specified range of dates.
- Response: Returns a list of APOD entries or a 404 error if not found. Returns a 400 error if the date formats are invalid.
Contributions are welcome! Please feel free to submit a pull request or open an issue if you find any bugs or have suggestions for improvements.
This project is licensed under the MIT License - see the LICENSE file for details.