Create a Django application that integrates with a public API, retrieves data, and stores it in a PostgreSQL database. The application should expose endpoints to fetch the stored data and perform basic CRUD operations.
- Use a public API (e.g., OpenWeatherMap API) to fetch weather data for a given city.
- Store the fetched data in a PostgreSQL database.
- Create a PostgreSQL database schema to store the weather data. The schema should include fields such as city name, temperature, weather description, and timestamp.
- GET /weather/{city}: Fetch and return the latest weather data for the specified city from the database.
- POST /weather/{city}: Fetch the latest weather data from the external API for the specified city and store it in the database.
- GET /weather: Return all stored weather data.
- PUT /weather/{id}: Update the weather data entry with the specified ID.
- DELETE /weather/{id}: Delete the weather data entry with the specified ID.
- Implement a background job using Celery to periodically fetch and update the weather data for a list of cities (
London
andNew York
).
- Ensure the code is clean, well-documented, and follows best practices (e.g., PEP 8 guidelines).
- Include error handling and validation.
- Django application code.
- PostgreSQL database schema.
- Documentation on how to set up and run the application.
- Example requests and responses for the endpoints.
- Dockerfile and docker-compose.yml for easy setup (optional but preferred).
- Swagger (OpenAPI) docs (optional but preferred).
Create an API token for the OpenWeatherMap API ((link)[https://openweathermap.org/api]) and use it to fetch weather data for a city.
Fetch weather data:
GET /weather/{city}
Response:
{
"city": "London",
"temperature": 15.5,
"description": "Clear sky",
"timestamp": "2024-07-09T12:00:00Z"
}
Store weather data for a city:
GET /weather/{city}
Response:
{
"message": "Weather data for London has been updated."
}
Fetch all weather data:
GET /weather
Response:
[
{
"city": "London",
"temperature": 15.5,
"description": "Clear sky",
"timestamp": "2024-07-09T12:00:00Z"
},
{
"city": "New York",
"temperature": 25.0,
"description": "Sunny",
"timestamp": "2024-07-09T12:00:00Z"
}
]
Update weather data:
PUT /weather/{id}
Response:
{
"temperature": 16.0,
"description": "Partly cloudy"
}
Delete weather data:
DELETE /weather/{id}
Response:
{
"message": "Weather data with ID 1 has been deleted."
}