Skip to content

Instantly share code, notes, and snippets.

@coderberry
Last active July 20, 2024 11:00
Show Gist options
  • Save coderberry/0c5d4d866135b13d54b6f3f799f4c533 to your computer and use it in GitHub Desktop.
Save coderberry/0c5d4d866135b13d54b6f3f799f4c533 to your computer and use it in GitHub Desktop.
Berry Development Coding Exercise: FastAPI

Coding Task: Django Application with External API Integration, PostgreSQL, and Celery

Task Description:

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.

Requirements:

1. External API Integration:

  • Use a public API (e.g., OpenWeatherMap API) to fetch weather data for a given city.
  • Store the fetched data in a PostgreSQL database.

2. Database Schema:

  • Create a PostgreSQL database schema to store the weather data. The schema should include fields such as city name, temperature, weather description, and timestamp.

3. Endpoints:

  • 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.

4. Background Jobs:

  • Implement a background job using Celery to periodically fetch and update the weather data for a list of cities (London and New York).

5. Code Quality:

  • Ensure the code is clean, well-documented, and follows best practices (e.g., PEP 8 guidelines).
  • Include error handling and validation.

Deliverables:

  1. Django application code.
  2. PostgreSQL database schema.
  3. Documentation on how to set up and run the application.
  4. Example requests and responses for the endpoints.
  5. Dockerfile and docker-compose.yml for easy setup (optional but preferred).
  6. Swagger (OpenAPI) docs (optional but preferred).

Weather API:

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."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment