Main Dish (Idea): A multilingual appetizer providing conversions between month names/IDs and country names/IDs, served two ways: a quick in-memory version and a more robust database-backed version.
Ingredients (Concepts & Components):
- Base Data: CSV file containing country names and IDs, and a hardcoded month name to ID dictionary.
- FastAPI Framework: The chef’s knife, providing structure and quick handling of requests.
- API Endpoints: Four separate serving plates, each offering a different conversion.
- SQLAlchemy: A robust tool to interact with a PostgreSQL database.
- PostgreSQL database: Table with Country IDs to Country Names
- Error Handling: A dash of
try...exceptto prevent the kitchen from burning down. - Query Parameters: Served as optional toppings, to give the end user more flexibility.
Cooking Process (How It Works):
- Setting the Stage (Initialization): The FastAPI application is initialized, and the in-memory data (month dictionary, countries.csv) is loaded. A connection is stablished with a Postgres Database.
- The In-Memory Conversions (Quick Stir-Fry):
- When a request hits a
/convert_month_to_id/endpoint, it first checks for valid month. If the month name is valid, it’s looked up in the month dictionary, and a month ID is calculated based on the provided year and a base year. - When a request hits a
/convert_id_to_month_year/endpoint, the month ID is validated, the month name is calculated based on a base year. - When a request hits a
/convert_country_name_to_id/endpoint, it searches the countries.csv for a matching country. If the country name is valid, it’s ID is extracted and passed on. - When a request hits a
/convert_id_to_country_name/endpoint, it searches the countries.csv for a matching ID. If the ID is valid, it’s name is extracted and passed on.
- When a request hits a
- The Database Conversions (Slow Simmer):
- When a request hits a
/convert_country_name_to_id_db/endpoint, a SQL query is constructed to find the country ID matching the provided name. - When a request hits a
/convert_country_id_to_name_db/endpoint, a SQL query is constructed to find the country name matching the provided ID. - SQLAlchemy then executes this query against the PostgreSQL database.
- When a request hits a
- The Plating (Response):
- For successful conversions, the resulting ID or name is neatly packaged into a JSON response.
- If anything goes wrong (invalid input, database issues), a formatted error message (HTTPException) is served instead.
Serving Suggestion (Outcome):
This project provides an easy-to-use and efficient API for converting between month/country names and IDs. The in-memory version offers speed, while the database version provides scalability and data persistence. It’s a versatile dish, useful for applications that need to handle multilingual data or work with external datasets.

