import certifi import pymongo import os from dotenv import load_dotenv load_dotenv() def get_mongo_client(mongo_url): """Establish connection to the MongoDB.""" if not mongo_url: print("MONGO_URI not set in environment variables") try: client = pymongo.MongoClient(mongo_url, tlsCAFile=certifi.where()) print("Connection to MongoDB successful") return client except pymongo.errors.ConnectionFailure as e: print(f"Connection failed: {e}") return None def get_mongo_url(): username = os.environ["MONGO_USERNAME"] password = os.environ["MONGO_PW"] mongo_url = f"mongodb+srv://{username}:{password}@cluster0.62unmco.mongodb.net/" return mongo_url